88 lines
2.4 KiB
Plaintext
Raw Normal View History

2022-03-17 21:18:52 -07:00
#!/usr/bin/python3
"""Upload a gist containing packages installed on the system.
Dependencies
============
* pacman
* gist
"""
import argparse
import configparser
import logging
import subprocess
2022-03-17 22:42:58 -07:00
import sys
2022-03-17 21:18:52 -07:00
import tempfile
import packaging_scripts.pacman as pacman
# ========== Constants ==========
# Commands
PACMAN_CMD = "/usr/bin/pacman"
2022-03-17 21:18:52 -07:00
LOCALE = "utf-8"
# Paths
2022-03-17 22:42:58 -07:00
CONFIG_FILE = "/etc/packaging-scripts.conf"
DEFAULT_FILENAME = "pacman-packages.txt"
# Config file options
CONFIG_SECTION = "Main"
ENABLE_CONFIG_OPTION = "GIST_PUG_ENABLE"
2022-03-17 21:18:52 -07:00
# ========== 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():
2022-03-17 22:42:58 -07:00
"""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)
2022-03-17 21:18:52 -07:00
# ========== Main Script ==========
if __name__ == "__main__":
2022-03-17 22:42:58 -07:00
if not configured_to_run():
# Silently exit, we don't need to cause an error message with pacman
sys.exit(0)
2022-03-17 21:18:52 -07:00
# Parse any arguments
parser = argparse.ArgumentParser()
2022-03-17 22:42:58 -07:00
parser.add_argument(
"-f",
"--filename",
dest="filename",
type=str,
default=DEFAULT_FILENAME,
help="alternative filename to upload as",
)
2022-03-17 21:18:52 -07:00
args = parser.parse_args()
2022-03-17 22:42:58 -07:00
filename = args.filename
2022-03-17 21:18:52 -07:00
# Run process that lists packages
packages = subprocess.run([PACMAN_CMD, "-Qqem"], capture_output=True).stdout
2022-03-17 21:18:52 -07:00
# Write package list to file and perform upload
2022-03-17 22:42:58 -07:00
with tempfile.NamedTemporaryFile() as tmpfile:
2022-03-17 21:18:52 -07:00
tmpfile.write(packages)
2022-03-17 22:42:58 -07:00
# 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])