Fully implement pug2 script

This commit is contained in:
etorres4 2022-03-19 00:18:29 -07:00
parent b621eebbc8
commit 6388855727
4 changed files with 75 additions and 60 deletions

View File

@ -1,6 +1,15 @@
Changelog for packaging-scripts Changelog for packaging-scripts
=============================== ===============================
Version 1.5.2
-------------
* pug2
* Initial full implementation of the pug2 script
* Create config file section for pug2 options
Version 1.5.1 Version 1.5.1
------------- -------------

View File

@ -1,7 +1,7 @@
# Maintainer: Eric Torres <erictorres4@protonmail.com> # Maintainer: Eric Torres <erictorres4@protonmail.com>
pkgname=packaging-scripts pkgname=packaging-scripts
pkgver=1.5.1 pkgver=1.5.2
pkgrel=2 pkgrel=1
pkgdesc="A set of helper scripts for handling Arch Linux packages" pkgdesc="A set of helper scripts for handling Arch Linux packages"
arch=('any') arch=('any')
license=('MIT') license=('MIT')

106
bin/pug2
View File

@ -7,81 +7,73 @@ Dependencies
* gist * gist
""" """
import argparse
import configparser import configparser
import logging import re
import subprocess import subprocess
import sys import sys
import tempfile
import packaging_scripts.pacman as pacman
# ========== Constants ========== # ========== Constants ==========
# Commands
PACMAN_CMD = "/usr/bin/pacman"
LOCALE = "utf-8"
# Paths # Paths
CONFIG_FILE = "/etc/packaging-scripts.conf" CONFIG_FILE = "/etc/packaging-scripts.conf"
DEFAULT_FILENAME = "pacman-packages.txt" DEFAULT_FILENAME = "pacman-packages.txt"
# Config file options # Config file options
CONFIG_SECTION = "Main" CONFIG_SECTION = "pug2"
ENABLE_CONFIG_OPTION = "GIST_PUG_ENABLE" CONFIG_OPTION_DESCRIPTION = "GIST_DESCRIPTION"
CONFIG_OPTION_ENABLE = "GIST_PUG_ENABLE"
# ========== Logging setup ========== CONFIG_OPTION_FILENAME = "GIST_FILENAME"
console_formatter = logging.Formatter("==> %(levelname)s %(message)s") CONFIG_OPTION_ID = "GIST_ID"
syslog = logging.getLogger("packaging_scripts")
syslog.setLevel(logging.DEBUG)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.INFO)
stdout_handler.setFormatter(console_formatter)
stdout_handler.addFilter(lambda record: record.levelno <= logging.INFO)
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.WARNING)
stderr_handler.setFormatter(console_formatter)
syslog.addHandler(stdout_handler)
syslog.addHandler(stderr_handler)
# ========== Functions ========== # ========== Functions ==========
def configured_to_run(): def extract_gist_id(url):
"""Verify the script is enabled to run in the config file. """Extract the gist id from a gist URL.
:returns: true if enabled in config, false if otherwise
Normalizes URLs from
* http(s)?://<subdomain>.<second level domain>.<top level domain>/<username>/<gist ID>
* <username>/<gist ID>
* <gist ID>
to one of
* <username>/<gist ID>
* <gist ID>
both of which are valid for use with gist
:param url: a valid URL containing the gist id
:type url: str
:returns: the valid gist ID
:rtype: str
""" """
config = configparser.ConfigParser() return re.sub("^http(s)?://[\w]*.[\w]*.[\w]/", "", url)
config.read(CONFIG_FILE)
return config.getboolean(CONFIG_SECTION, ENABLE_CONFIG_OPTION)
# ========== Main Script ========== # ========== Main Script ==========
if __name__ == "__main__": if __name__ == "__main__":
if not configured_to_run(): config = configparser.ConfigParser()
# Silently exit, we don't need to cause an error message with pacman config.read(CONFIG_FILE)
# Check if script is enabled to run; if not, exit silently
if not config.getboolean(CONFIG_SECTION, CONFIG_OPTION_ENABLE):
sys.exit(0) sys.exit(0)
# Parse any arguments gist_description = config.get(CONFIG_SECTION, CONFIG_OPTION_DESCRIPTION)
parser = argparse.ArgumentParser() gist_filename = config.get(CONFIG_SECTION, CONFIG_OPTION_FILENAME)
parser.add_argument( gist_id = config.get(CONFIG_SECTION, CONFIG_OPTION_ID)
"-f",
"--filename",
dest="filename",
type=str,
default=DEFAULT_FILENAME,
help="alternative filename to upload as",
)
args = parser.parse_args()
filename = args.filename if gist_id == "":
# run gist, then extract url and save to config file
# Run process that lists packages gist_process = subprocess.run(
packages = subprocess.run([PACMAN_CMD, "-Qqen"], capture_output=True).stdout f"pacman -Qqen | gist --filename {gist_filename} --description {gist_description}",
text=True,
# Write package list to file and perform upload capture_output=True,
with tempfile.NamedTemporaryFile() as tmpfile: shell=True,
tmpfile.write(packages) )
# We use the filename in the command, not in the tempfile so gist knows what to upload as config[CONFIG_SECTION][CONFIG_OPTION_ID] = extract_gist_id(gist_process.stdout)
subprocess.run(["gist", "-f", args.filename, tmpfile.name]) else:
subprocess.run(
f"pacman -Qqen | gist --update {gist_id} --filename {gist_filename} --description {gist_description}",
capture_output=True,
shell=True,
)

View File

@ -1,3 +1,17 @@
[Main] [pug2]
# Activate pug script upon running pug ALPM hook # Activate pug script upon running pug ALPM hook
GIST_PUG_ENABLE = no GIST_PUG_ENABLE = no
# Name for gist
# Default: pacman-packages.txt
GIST_FILENAME = pacman-packages.txt
# Gist ID to prevent creating new gists
# This key is automatically given a value if empty
# This settings accepts IDs in 2 formats:
# <gist username>/<gist ID>
# <gist ID>
GIST_ID = ""
# Description for the gist
GIST_DESCRIPTION = "Pacman package list"