Reimplement repo_add as db_modify and clean code accordingly

This commit is contained in:
Eric Torres 2019-02-20 15:22:18 -08:00
parent 99492bbb4a
commit 387c5f7d33
3 changed files with 42 additions and 73 deletions

View File

@ -104,13 +104,10 @@ if __name__ == '__main__':
exit(E_NOFILESERR) exit(E_NOFILESERR)
try: try:
repos.repo_add('add', db, *pkg_tarballs, opts=opts) repos.db_modify('add', db, *opts, *pkg_tarballs)
except repos.RepoAddError as e: except repos.RepoAddError as e:
syslog.error(e) syslog.error(e)
exit(E_REPO_ADDERR) exit(E_REPO_ADDERR)
for pkg_tarball in pkg_tarballs: for pkgfile in (*pkg_tarballs, *sigfiles):
add_pkgfile(pkg_tarball, cachedir) add_pkgfile(pkgfile, cachedir)
for sigfile in sigfiles:
add_pkgfile(sigfile, cachedir)

View File

@ -89,13 +89,10 @@ if __name__ == '__main__':
directory=cachedir, directory=cachedir,
signatures_only=True) signatures_only=True)
for pkg_tarball in pkg_tarballs: for pkgfile in (*pkg_tarballs, *sigfiles):
del_pkgfile(pkg_tarball) del_pkgfile(pkgfile)
for sigfile in sigfiles:
del_pkgfile(sigfile)
try: try:
repos.repo_add('remove', db, pkg, opts=opts) repos.db_modify('remove', db, *opts, *pkgs)
except repos.RepoAddError as e: except repos.RepoAddError as e:
syslog.error(e) syslog.error(e)

View File

@ -4,10 +4,6 @@
import logging import logging
import subprocess import subprocess
# ========== Constants ==========
REPO_ADD_CMD = '/usr/bin/repo-add'
REPO_REMOVE_CMD = '/usr/bin/repo-remove'
# ========== Logging setup ========== # ========== Logging setup ==========
syslog = logging.getLogger(__name__) syslog = logging.getLogger(__name__)
@ -18,43 +14,9 @@ class RepoAddError(BaseException):
# ========== Functions ========== # ========== Functions ==========
def gen_cmdline(operation, db, opts, *pkgs): # TODO generalize this function
"""Generate a command suitable for invoking either repo-add or def db_modify(operation, db, *args):
repo-remove. """Run either repo-add or repo-remove.
:param operation: run either repo-add or repo-remove
:type operation: str
:param db: the database file to operate on
:type db: str, bytes, or path-like object
:param opts: extra options to pass to repo-add
:type opts: list
:param pkgs: these arguments depend on whether the operation is
an add or remove.
If operation is 'add', then pkgs is all of the paths of the
package files to add to the database. If operation is 'remove',
then pkgs is the name of the packages to remove from the database.
:type pkgs: any iterable
:returns: list of command-line arguments suitable for passing to
subprocess.run()
:rtype: list
:raises: ValueError if operation was invalid
"""
if not operation == 'add' or not operation == 'remove':
raise ValueError('Invalid operation was requested')
cmd = [f"/usr/bin/repo-{operation}"]
if opts is not None:
cmd.extend(opts)
cmd.append(db)
cmd.extend(pkgs)
return cmd
def repo_add(operation, db, *pkgs, opts=None):
"""Run repo-add.
Since both the repo-add and repo-remove scripts have the Since both the repo-add and repo-remove scripts have the
same command-line usage, it is simpler to generalize both same command-line usage, it is simpler to generalize both
@ -65,34 +27,47 @@ def repo_add(operation, db, *pkgs, opts=None):
:type operation: str :type operation: str
:param db: the database file to operate on :param db: the database file to operate on
:type db: str, bytes, or path-like object :type db: str, bytes, or path-like object
:param opts: extra options to pass to repo-add :param args: These arguments include two things:
:type opts: list * Extra options to pass to repo-add
:param pkgs: these arguments depend on whether the operation is * Package names
an add or remove. Package names can further be divided into two types depending
If operation is 'add', then pkgs is all of the paths of the on the operation. Adding requires that the paths to the package
package files to add to the database. If operation is 'remove', files are passed. Removing requires that the names of the packages
then pkgs is the name of the packages to remove from the database. are passed.
:type pkgs: any iterable :type args: str
:raises: RepoAddError if repo-add failed :raises: RepoAddError if repo-add failed
:raises: ValueError if an invalid operation was requested :raises: ValueError if an invalid operation was specified
""" """
if operation == 'add': if operation == 'add':
syslog.info('Adding packages to database') syslog.info('Adding packages to database')
else: elif operation == 'remove':
syslog.info('Removing packages from database') syslog.info('Removing packages from database')
else:
raise ValueError(f"Invalid operation specified: {operation}")
syslog.debug(f"Database: {db}") syslog.debug(f"Database: {db}")
syslog.debug(f"Options: {opts}") syslog.debug(f"Arguments: {args}")
syslog.debug(f"Packages: {pkgs}")
try: try:
cmd = gen_cmdline(operation, db, opts, *pkgs) process = _run_script(operation, *args)
process = subprocess.run(cmd,
check=True,
capture_output=True,
text=True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
raise RepoAddError(process.stderr) raise RepoAddError(process.stderr)
else: else:
syslog.debug(process.stdout) syslog.debug(process.stdout)
syslog.info('Database operation complete') 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)