#!/usr/bin/python3 """Upload a gist containing packages installed on the system. Dependencies ============ * pacman * gist """ import argparse import configparser import logging import subprocess import sys import tempfile import packaging_scripts.pacman as pacman # ========== Constants ========== # Commands PACMAN_CMD = "/usr/bin/pacman" LOCALE = "utf-8" # Paths CONFIG_FILE = "/etc/packaging-scripts.conf" DEFAULT_FILENAME = "pacman-packages.txt" # Config file options CONFIG_SECTION = "Main" ENABLE_CONFIG_OPTION = "GIST_PUG_ENABLE" # ========== Logging setup ========== console_formatter = logging.Formatter("==> %(levelname)s %(message)s") 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 ========== def configured_to_run(): """Verify the script is enabled to run in the config file. :returns: true if enabled in config, false if otherwise """ config = configparser.ConfigParser() config.read(CONFIG_FILE) return config.getboolean(CONFIG_SECTION, ENABLE_CONFIG_OPTION) # ========== Main Script ========== if __name__ == "__main__": if not configured_to_run(): # Silently exit, we don't need to cause an error message with pacman sys.exit(0) # Parse any arguments parser = argparse.ArgumentParser() parser.add_argument( "-f", "--filename", dest="filename", type=str, default=DEFAULT_FILENAME, help="alternative filename to upload as", ) args = parser.parse_args() filename = args.filename # Run process that lists packages packages = subprocess.run([PACMAN_CMD, "-Qqem"], capture_output=True).stdout # Write package list to file and perform upload with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(packages) # We use the filename in the command, not in the tempfile so gist knows what to upload as subprocess.run(["gist", "-f", args.filename, tmpfile.name])