Remove repository check, modularize code away from script, code cleanup

This commit is contained in:
Eric Torres 2019-01-28 08:08:51 -08:00
parent c2a362023e
commit a2ba5ffe69

View File

@ -1,20 +1,25 @@
#!/usr/bin/env python #!/usr/bin/env python
""" Delete packages from a repository. """ Delete packages from a repository.
Functions: Functions:
move_pkgfiles(repo, files=None) ==========
repo_add(repo, opts, filelist) del_pkgfiles(cachedir, pkgs)
clear_cachedir(repo) repo_remove(db, pkgs, opts=None)
""" """
import argparse import argparse
import logging import logging
import pathlib
import subprocess import subprocess
import sys import sys
import packaging_scripts.pacmanconf as pacmanconf import packaging_scripts.pacmanconf as pacmanconf
import packaging_scripts.pkgfiles as pkgfiles 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') console_formatter = logging.Formatter('==> %(level)s %(message)s')
syslog = logging.getLogger(__name__) syslog = logging.getLogger(__name__)
syslog.setLevel(logging.DEBUG) syslog.setLevel(logging.DEBUG)
@ -31,43 +36,47 @@ syslog.addHandler(stdout_handler)
syslog.addHandler(stderr_handler) syslog.addHandler(stderr_handler)
def del_pkgfiles(repo, pkgs): # ========== Functions ==========
def del_pkgfiles(cachedir, pkgs):
""" Remove package files from the repository directory. """ Remove package files from the repository directory.
Parameters:
repo: the repository to delete from :param cachedir: directory to operate on
pkgs: a list of packages to remove :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') syslog.info('Removing package files from cachedir')
cachedir = pathlib.Path(f'/var/cache/pacman/{repo}')
syslog.debug(f"Cache directory: {cachedir}") syslog.debug(f"Cache directory: {cachedir}")
syslog.debug(f"Packages: {pkgs}") syslog.debug(f"Packages: {pkgs}")
for pkg in 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() pkgfile.unlink()
syslog.info(f"Removed {pkgfile}") syslog.info(f"Removed {pkgfile}")
def repo_remove(repo, pkgs, opts=None): def repo_remove(db, pkgs, opts=None):
""" Run repo-remove. """ Run repo-remove.
Parameters:
repo: the repository to remove from :param repo: the repository to remove from
pkgs: the names of the packages to remove :type repo: str
opts: the list of options to pass to repo-remove :param pkgs: the names of the packages to remove
Raises: :type pkgs: any iterable
subprocess.CalledProcessError if repo-add failed :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.info('Removing packages from database')
syslog.debug(f"Options: {opts}") syslog.debug(f"Options: {opts}")
syslog.debug(f"Packages: {pkgs}") 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: if opts is not None:
cmd.extend(opts) cmd.extend(opts)
cmd.append(str(db)) cmd.append(db)
cmd.extend(pkgs) cmd.extend(pkgs)
syslog.debug(f"Final repo-remove command: {cmd}") 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') 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__': if __name__ == '__main__':
parser = argparse.ArgumentParser() 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', parser.add_argument('-s', '--sign',
dest='opts', dest='opts',
action='append_const', action='append_const',
@ -120,14 +115,15 @@ if __name__ == '__main__':
repo = args.repository repo = args.repository
pkgs = args.packages pkgs = args.packages
opts = args.opts 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: if args.verbose:
stdout_handler.setLevel(logging.DEBUG) 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) del_pkgfiles(repo, pkgs)
try: try:
@ -135,6 +131,3 @@ if __name__ == '__main__':
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
syslog.error(e) syslog.error(e)
exit(2) exit(2)
if clean:
clean_cachedir(repo)