Moved scripts to packaging_scripts.bin

This commit is contained in:
Eric Torres
2024-05-12 00:59:06 -07:00
parent 02a6acbeab
commit 418b304b12
4 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,122 @@
#!/usr/bin/python3
"""Add packages to a repository.
Functions:
==========
* adds(cachedir, pkgs)
* repo_add(db, pkgs, opts=None)
"""
import argparse
import logging
import sys
import packaging_scripts.pacman as pacman
import packaging_scripts.pkgfiles as pkgfiles
import packaging_scripts.repos as repos
from pathlib import Path
# ========== Constants ==========
DB_EXT = "db.tar.xz"
LOGFORMAT = "==> %(levelname)s %(message)s"
# ----- Exit codes -----
E_NOFILESERR = 1
E_REPO_ADDERR = 2
# ========== Logging setup ==========
console_formatter = logging.Formatter(LOGFORMAT)
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)
# ========== Main Script ==========
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--cachedir", help="alternative directory to use for moving package"
)
parser.add_argument(
"-d",
"--db-filename",
help="alternative filename for database without extension",
)
parser.add_argument(
"--files-only",
action="store_true",
help="only add the built files to the repository",
)
parser.add_argument(
"-s",
"--sign",
dest="opts",
action="append_const",
const="--sign",
help="sign repository file",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="increase script verbosity"
)
parser.add_argument(
"repository",
choices=pacman.list_configured_repos(),
help="the repository to operate on",
metavar="repo",
)
parser.add_argument("packages", default=None, nargs="*", help="packages to add")
args = parser.parse_args()
repo = args.repository
opts = [] if args.opts is None else args.opts
cachedir = (
Path(args.cachedir)
if args.cachedir
else Path("/var") / "cache" / "pacman" / repo
)
db = (
cachedir / f"{args.db_filename}.{DB_EXT}"
if args.db_filename
else cachedir / f"{repo}.{DB_EXT}"
)
if args.verbose:
stdout_handler.setLevel(logging.DEBUG)
if args.packages:
pkg_tarballs = pkgfiles.filter(args.packages)
sigfiles = pkgfiles.filter(args.packages, signatures_only=True)
else:
pkg_tarballs = list(pkgfiles.get())
sigfiles = list(pkgfiles.get(signatures_only=True))
if not pkg_tarballs:
syslog.critical("No package tarballs have been found, exiting")
exit(E_NOFILESERR)
if not args.files_only:
try:
repos.db_modify("add", db, *opts, *pkg_tarballs)
except repos.RepoAddError as e:
syslog.error(e)
exit(E_REPO_ADDERR)
for pkgfile in (*pkg_tarballs, *sigfiles):
pkgfiles.add(pkgfile, cachedir)
if __name__ == "__main__":
main()

View File

@ -0,0 +1,115 @@
#!/usr/bin/python3
"""Delete packages from a repository.
Functions:
==========
* deletes(cachedir, pkgs)
* repo_remove(db, pkgs, opts=None)
"""
import argparse
import logging
import sys
import packaging_scripts.pacman as pacman
import packaging_scripts.pkgfiles as pkgfiles
import packaging_scripts.repos as repos
from pathlib import Path
# ========== Constants ==========
DB_EXT = "db.tar.xz"
# ----- Exit codes -----
E_REPO_REMOVEERR = 2
# ========== 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)
# ========== Main Script ==========
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--cachedir", help="alternative directory to use for deleting package"
)
parser.add_argument(
"-d",
"--db-filename",
help="alternative filename for database without extension",
)
parser.add_argument(
"--files-only",
action="store_true",
help="only add the built files to the repository",
)
parser.add_argument(
"-s",
"--sign",
dest="opts",
action="append_const",
const="--sign",
help="sign repository file",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="increase script verbosity"
)
parser.add_argument(
"repository",
choices=pacman.list_configured_repos(),
help="the repository to operate on",
metavar="repo",
)
parser.add_argument("packages", nargs="+", help="packages to remove")
args = parser.parse_args()
repo = args.repository
pkgs = args.packages
opts = [] if args.opts is None else args.opts
cachedir = (
Path(args.cachedir)
if args.cachedir
else Path("/var") / "cache" / "pacman" / repo
)
db = (
cachedir / f"{args.db_filename}.{DB_EXT}"
if args.db_filename
else cachedir / f"{repo}.{DB_EXT}"
)
if args.verbose:
stdout_handler.setLevel(logging.DEBUG)
for pkg in pkgs:
pkg_tarballs = list(pkgfiles.get(query=pkg, directory=cachedir))
sigfiles = list(
pkgfiles.get(query=pkg, directory=cachedir, signatures_only=True)
)
for pkgfile in (*pkg_tarballs, *sigfiles):
pkgfiles.delete(pkgfile)
if not args.files_only:
try:
repos.db_modify("remove", db, *opts, *pkgs)
except repos.RepoAddError as e:
syslog.error(e)
if __name__ == "__main__":
main()

72
packaging_scripts/bin/fqo Normal file
View File

@ -0,0 +1,72 @@
#!/usr/bin/python3
"""Fuzzy-find a file and check which package owns it.
Dependencies
============
* fzf
* mlocate
"""
import argparse
import subprocess
# ========== Constants ==========
# Commands
PACMAN_CMD = "/usr/bin/pacman"
FZF_CMD = "/usr/bin/fzf"
FZF_OPTS = ["--read0", "--select-1", "--exit-0", "--print0"]
LOCATE_CMD = "/usr/bin/locate"
LOCATE_OPTS = ["--all", "--ignore-case", "--null"]
LOCALE = "utf-8"
# ========== Functions ==========
def run_fzf(files):
"""Run fzf on a stream of searched files for the user to select.
:param files: stream of null-terminated files to read
:type files: bytes stream (stdout of a completed process)
:returns: selected file
:rtype: str
"""
selected_file = subprocess.run(
[FZF_CMD] + FZF_OPTS, input=files, stdout=subprocess.PIPE
).stdout
return selected_file.decode(LOCALE).strip("\x00")
def locate_files(patterns):
"""Use a locate-based program to locate files, then pass to fzf.
:param patterns: patterns to pass to locate
:type patterns: list
:returns: path of user-selected file
:rtype: bytes
"""
cmd = [LOCATE_CMD] + LOCATE_OPTS
cmd.extend(patterns)
return subprocess.run(cmd, capture_output=True).stdout
# ========== Main Script ==========
def main():
parser = argparse.ArgumentParser()
parser.add_argument("patterns", nargs="+", help="file pattern to search for")
args = parser.parse_args()
try:
files = locate_files(args.patterns)
# Locate binary is optional for package, but required for script
except FileNotFoundError:
print("locate binary not found, install and re-run script")
else:
selected_file = run_fzf(files)
subprocess.run([PACMAN_CMD, "-Qo", selected_file])
if __name__ == "__main__":
main()

152
packaging_scripts/bin/pug2 Normal file
View File

@ -0,0 +1,152 @@
#!/usr/bin/python3
"""Upload a gist containing packages installed on the system.
Dependencies
============
* pacman
* gist
"""
import argparse
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 = "RUN_ON_PACMAN_HOOK"
CONFIG_OPTION_FILENAME = "GIST_FILENAME"
CONFIG_OPTION_ID = "GIST_ID"
DESCRIPTION = "Send a list of explicitly installed pacman packages to a gist"
# ========== Functions ==========
def extract_gist_id(url):
"""Extract the gist id from a gist URL.
Normalizes URLs from
* http(s)?://<subdomain>.<second level domain>.<top level domain>/<username>/<gist ID>
* <username>/<gist ID>
* <gist ID>
to one of
* <username>/<gist ID>
* <gist ID>
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)
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
"""
return subprocess.run(
["gist", "--read", gist_id], capture_output=True, text=True
).stdout
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
# ========== Main Script ==========
def main():
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",
)
parser.add_argument(
"-f",
"--force-update",
help="force update regardless if package lists match",
action="store_true",
)
args = parser.parse_args()
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
):
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)
gist_opts = ["--filename", gist_filename, "--description", gist_description]
packages = subprocess.run(
["pacman", "-Qqen"], capture_output=True, text=True
).stdout
if gist_id == "":
try:
print("No gist ID detected, creating new.")
gist_process = subprocess.run(
["gist", *gist_opts],
input=packages,
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.")
elif package_lists_match(gist_id, packages) and not args.force_update:
print("Package lists match, no update necessary.")
sys.exit(0)
else:
try:
print("Updating package list.")
subprocess.run(
["gist", "--update", gist_id, *gist_opts],
input=packages,
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()