diff --git a/bin/delpkg b/bin/delpkg index 60c7a0e..5c07648 100644 --- a/bin/delpkg +++ b/bin/delpkg @@ -1,20 +1,25 @@ #!/usr/bin/env python """ Delete packages from a repository. + Functions: - move_pkgfiles(repo, files=None) - repo_add(repo, opts, filelist) - clear_cachedir(repo) +========== + del_pkgfiles(cachedir, pkgs) + repo_remove(db, pkgs, opts=None) """ import argparse import logging -import pathlib import subprocess import sys import packaging_scripts.pacmanconf as pacmanconf import packaging_scripts.pkgfiles as pkgfiles +# ========== Constants ========== +REPO_REMOVE_CMD = '/usr/bin/repo-remove' +DB_EXT = 'db.tar.xz' + +# ========== Logging setup ========== console_formatter = logging.Formatter('==> %(level)s %(message)s') syslog = logging.getLogger(__name__) syslog.setLevel(logging.DEBUG) @@ -31,43 +36,47 @@ syslog.addHandler(stdout_handler) syslog.addHandler(stderr_handler) -def del_pkgfiles(repo, pkgs): +# ========== Functions ========== +def del_pkgfiles(cachedir, pkgs): """ Remove package files from the repository directory. - Parameters: - repo: the repository to delete from - pkgs: a list of packages to remove + + :param cachedir: directory to operate on + :type cachedir: str, bytes, or path-like object + :param pkgs: names of packages to remove + :type pkgs: any container object """ syslog.info('Removing package files from cachedir') - cachedir = pathlib.Path(f'/var/cache/pacman/{repo}') syslog.debug(f"Cache directory: {cachedir}") syslog.debug(f"Packages: {pkgs}") for pkg in pkgs: - for pkgfile in cachedir.glob(f"{pkg}*.pkg.tar.xz*"): + for pkgfile in pkgfiles.get_pkgfiles(directory=cachedir, + signatures=True): pkgfile.unlink() syslog.info(f"Removed {pkgfile}") -def repo_remove(repo, pkgs, opts=None): +def repo_remove(db, pkgs, opts=None): """ Run repo-remove. - Parameters: - repo: the repository to remove from - pkgs: the names of the packages to remove - opts: the list of options to pass to repo-remove - Raises: - subprocess.CalledProcessError if repo-add failed + + :param repo: the repository to remove from + :type repo: str + :param pkgs: the names of the packages to remove + :type pkgs: any iterable + :param opts: extra options to pass to repo-remove + :type opts: list + :raises: subprocess.CalledProcessError if repo-remove failed """ syslog.info('Removing packages from database') syslog.debug(f"Options: {opts}") syslog.debug(f"Packages: {pkgs}") - db = pathlib.Path(f"/var/cache/pacman/{repo}/{repo}.db.tar.xz") - cmd = ['repo-remove'] + cmd = [REPO_REMOVE_CMD] if opts is not None: cmd.extend(opts) - cmd.append(str(db)) + cmd.append(db) cmd.extend(pkgs) syslog.debug(f"Final repo-remove command: {cmd}") @@ -83,22 +92,8 @@ def repo_remove(repo, pkgs, opts=None): syslog.info('Finished removing packages from database') -def clean_cachedir(repo): - """ Run paccache """ - syslog.info('Running paccache') - subprocess.run(['paccache', - '--remove', - '--verbose', - '--keep=1', - f"--cachedir=/var/cache/pacman/{repo}"]) - syslog.info('Finished running paccache') - - if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('-c', '--clean-cachedir', - action='store_true', - help='use paccache to clean the cache directory') parser.add_argument('-s', '--sign', dest='opts', action='append_const', @@ -120,14 +115,15 @@ if __name__ == '__main__': repo = args.repository pkgs = args.packages opts = args.opts - clean = args.clean_cachedir + + cachedir = f"/var/cache/pacman/{repo}" + # this assumes that the db file for the repo + # has the same name as that repo + db = f"{cachedir}/{repo}/.{DB_EXT}" if args.verbose: stdout_handler.setLevel(logging.DEBUG) - if not pathlib.Path(f"/var/cache/pacman/{args.repository}").is_dir(): - syslog.critical(f"Not a repository: {repo}") - del_pkgfiles(repo, pkgs) try: @@ -135,6 +131,3 @@ if __name__ == '__main__': except subprocess.CalledProcessError as e: syslog.error(e) exit(2) - - if clean: - clean_cachedir(repo)