#!/usr/bin/python3 """Upload a gist containing packages installed on the system. Dependencies ============ * pacman * gist """ import configparser import re import subprocess import sys # ========== Constants ========== # Paths CONFIG_FILE = "/etc/packaging-scripts.conf" DEFAULT_FILENAME = "pacman-packages.txt" # Config file options CONFIG_SECTION = "pug2" CONFIG_OPTION_DESCRIPTION = "GIST_DESCRIPTION" CONFIG_OPTION_ENABLE = "GIST_PUG_ENABLE" CONFIG_OPTION_FILENAME = "GIST_FILENAME" CONFIG_OPTION_ID = "GIST_ID" # ========== Functions ========== def extract_gist_id(url): """Extract the gist id from a gist URL. Normalizes URLs from * http(s)?://..// * / * to one of * / * 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 """ return re.sub("^http(s)?://[\w]*.[\w]*.[\w]/", "", url) # ========== Main Script ========== if __name__ == "__main__": config = configparser.ConfigParser() 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) gist_description = config.get(CONFIG_SECTION, CONFIG_OPTION_DESCRIPTION) gist_filename = config.get(CONFIG_SECTION, CONFIG_OPTION_FILENAME) gist_id = config.get(CONFIG_SECTION, CONFIG_OPTION_ID) if gist_id == "": # run gist, then extract url and save to config file gist_process = subprocess.run( f"pacman -Qqen | gist --filename {gist_filename} --description {gist_description}", text=True, capture_output=True, shell=True, ) config[CONFIG_SECTION][CONFIG_OPTION_ID] = extract_gist_id(gist_process.stdout) else: subprocess.run( f"pacman -Qqen | gist --update {gist_id} --filename {gist_filename} --description {gist_description}", capture_output=True, shell=True, )