80 lines
2.1 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 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 = "GIST_PUG_ENABLE"
CONFIG_OPTION_FILENAME = "GIST_FILENAME"
CONFIG_OPTION_ID = "GIST_ID"
2022-03-17 21:18:52 -07: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
"""
2022-03-19 00:18:29 -07:00
return re.sub("^http(s)?://[\w]*.[\w]*.[\w]/", "", url)
2022-03-17 22:42:58 -07:00
2022-03-17 21:18:52 -07:00
# ========== Main Script ==========
if __name__ == "__main__":
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 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)
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,
)