153 lines
4.2 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
2022-03-17 21:18:52 -07:00
import configparser
2022-03-19 00:18:29 -07:00
import re
2022-03-17 21:18:52 -07:00
import subprocess
2022-03-17 22:42:58 -07:00
import sys
2022-03-17 21:18:52 -07:00
# ========== Constants ==========
# Paths
2022-03-17 22:42:58 -07:00
CONFIG_FILE = "/etc/packaging-scripts.conf"
DEFAULT_FILENAME = "pacman-packages.txt"
# Config file options
2022-03-19 00:18:29 -07:00
CONFIG_SECTION = "pug2"
CONFIG_OPTION_DESCRIPTION = "GIST_DESCRIPTION"
CONFIG_OPTION_ENABLE = "RUN_ON_PACMAN_HOOK"
2022-03-19 00:18:29 -07:00
CONFIG_OPTION_FILENAME = "GIST_FILENAME"
CONFIG_OPTION_ID = "GIST_ID"
2022-03-17 21:18:52 -07:00
2022-03-24 21:56:26 -07:00
DESCRIPTION = "Send a list of explicitly installed pacman packages to a gist"
2022-03-17 21:18:52 -07:00
2024-03-02 17:42:26 -08:00
2022-03-19 00:18:29 -07:00
# ========== Functions ==========
def extract_gist_id(url):
"""Extract the gist id from a gist URL.
2022-03-17 21:18:52 -07:00
2022-03-19 00:18:29 -07:00
Normalizes URLs from
* http(s)?://<subdomain>.<second level domain>.<top level domain>/<username>/<gist ID>
* <username>/<gist ID>
* <gist ID>
2022-03-17 21:18:52 -07:00
2022-03-19 00:18:29 -07:00
to one of
2022-03-17 21:18:52 -07:00
2022-03-19 00:18:29 -07:00
* <username>/<gist ID>
* <gist ID>
2022-03-17 21:18:52 -07:00
2022-03-19 00:18:29 -07:00
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
2022-03-17 22:42:58 -07:00
"""
return re.sub("^http(s)?://[\\w]*.[\\w]*.[\\w]/", "", url)
def retrieve_gist_info(gist_id):
"""Retrieve info from gist ID that is specified in the config file.
:param gist_id: string id to read gist info
:returns: data read from gist
:rtype: bytes
"""
2022-03-24 22:01:00 -07:00
return subprocess.run(
["gist", "--read", gist_id], capture_output=True, text=True
).stdout
2022-03-24 22:01:00 -07:00
def package_lists_match(gist_id, package_list):
"""Compare local package list to that of pacman and gist, return if they match.
:param gist_id: ID of the gist to read from
:param package_list: Newline separated list of packages installed on the system
:type gist_id: str
:type package_list: str
"""
return retrieve_gist_info(gist_id) == package_list
2022-03-17 22:42:58 -07:00
2022-03-17 21:18:52 -07:00
# ========== Main Script ==========
def main():
2022-03-24 21:56:26 -07:00
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument(
"-c",
"--check-if-enabled",
dest="check_enabled",
help="check if enabled in config file",
action="store_true",
)
2022-03-24 21:58:53 -07:00
parser.add_argument(
"-f",
"--force-update",
help="force update regardless if package lists match",
action="store_true",
)
args = parser.parse_args()
2022-03-19 00:18:29 -07:00
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
# Check if script is enabled to run; if not, exit silently
if args.check_enabled and not config.getboolean(
CONFIG_SECTION, CONFIG_OPTION_ENABLE
):
2022-03-17 22:42:58 -07:00
sys.exit(0)
2022-03-17 21:18:52 -07:00
2022-03-19 00:18:29 -07:00
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)
2022-03-24 22:01:00 -07:00
gist_opts = ["--filename", gist_filename, "--description", gist_description]
packages = subprocess.run(
["pacman", "-Qqen"], capture_output=True, text=True
).stdout
2022-03-19 00:18:29 -07:00
if gist_id == "":
2022-03-24 13:01:28 -07:00
try:
print("No gist ID detected, creating new.")
gist_process = subprocess.run(
2022-03-24 22:01:00 -07:00
["gist", *gist_opts],
input=packages,
2022-03-24 13:01:28 -07:00
capture_output=True,
text=True,
check=True,
)
except subprocess.CalledProcessError as e:
print(e.stdout.strip(), file=sys.stderr)
sys.exit(e.returncode)
else:
config[CONFIG_SECTION][CONFIG_OPTION_ID] = extract_gist_id(
gist_process.stdout
)
print("Gist creation complete.")
2022-03-24 21:58:53 -07:00
elif package_lists_match(gist_id, packages) and not args.force_update:
print("Package lists match, no update necessary.")
sys.exit(0)
2022-03-19 00:18:29 -07:00
else:
2022-03-24 13:01:28 -07:00
try:
2022-03-24 22:01:00 -07:00
print("Updating package list.")
2022-03-24 13:01:28 -07:00
subprocess.run(
2022-03-24 22:01:00 -07:00
["gist", "--update", gist_id, *gist_opts],
input=packages,
2022-03-24 13:01:28 -07:00
capture_output=True,
text=True,
check=True,
)
except subprocess.CalledProcessError as e:
print(e.stdout.strip(), file=sys.stderr)
sys.exit(e.returncode)
else:
print("Update complete.")
if __name__ == "__main__":
main()