71 lines
2.3 KiB
Python
Raw Permalink Normal View History

2024-05-09 21:19:18 -07:00
"""Module for pacman repository helper functions."""
2019-01-29 19:55:59 -08:00
import logging
import subprocess
# ========== Logging setup ==========
syslog = logging.getLogger(__name__)
# ========== Classes ==========
class RepoAddError(BaseException):
"""Exception when the repo-add or repo-remove script fails."""
# ========== Functions ==========
def db_modify(operation, db, *args):
"""Run either repo-add or repo-remove.
2019-01-29 19:55:59 -08:00
Since both the repo-add and repo-remove scripts have the
same command-line usage, it is simpler to generalize both
operations using a single function.
:param operation: run repo-add or repo-remove, can be
either 'add' or 'remove'
:type operation: str
:param db: the database file to operate on
:type db: str, bytes, or path-like object
:param args: These arguments include two things:
* Extra options to pass to repo-add
* Package names
Package names can further be divided into two types depending
on the operation. Adding requires that the paths to the package
files are passed. Removing requires that the names of the packages
are passed.
:type args: str
2019-04-14 08:51:38 -07:00
:raises RepoAddError: if repo-add failed
:raises ValueError: if an invalid operation was specified
2019-01-29 19:55:59 -08:00
"""
if operation == "add":
syslog.info("Adding packages to database")
elif operation == "remove":
syslog.info("Removing packages from database")
else:
raise ValueError(f"Invalid operation specified: {operation}")
2019-01-29 19:55:59 -08:00
syslog.debug("Database: %s", db)
syslog.debug("Arguments: %s", args)
2019-01-29 19:55:59 -08:00
try:
2019-03-25 14:45:21 -07:00
process = _run_script(operation, str(db), *args)
except subprocess.CalledProcessError as e:
raise RepoAddError(e) from e
2019-01-29 19:55:59 -08:00
else:
syslog.debug(process.stdout)
syslog.info("Database operation complete")
def _run_script(operation, *args):
"""Run either the repo-add or the repo-remove script.
:param operation: run either repo-add or repo-remove; can be either
'add' or 'remove'
:type operation: str
:param args: arguments to pass to repo-add script; can be both
options and packages
:type args: str
"""
return subprocess.run(
(f"repo-{operation}", *args), check=True, capture_output=True, text=True
)