2019-02-20 02:50:41 -08:00

102 lines
3.0 KiB
Python

#!/usr/bin/python3
""" Delete packages from a repository.
Functions:
==========
* del_pkgfiles(cachedir, pkgs)
* repo_remove(db, pkgs, opts=None)
"""
import argparse
import logging
import sys
import packaging_scripts.pacmanconf as pacmanconf
import packaging_scripts.pkgfiles as pkgfiles
import packaging_scripts.repos as repos
# ========== Constants ==========
REPO_REMOVE_CMD = '/usr/bin/repo-remove'
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)
# ========== Functions ==========
def del_pkgfile(pkg):
"""Remove package file.
:param pkg: path of package to remove
:type pkg: pathlib.Path object
"""
pkg.unlink()
syslog.info(f"Removed {pkg}")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
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=pacmanconf.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 = args.opts
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)
for pkg in pkgs:
pkg_tarballs = pkgfiles.get_pkgfiles(query=pkg,
directory=cachedir)
sigfiles = pkgfiles.get_pkgfiles(query=pkg,
directory=cachedir,
signatures_only=True)
for pkg_tarball in pkg_tarballs:
del_pkgfile(pkg_tarball)
for sigfile in sigfiles:
del_pkgfile(sigfile)
try:
repos.repo_add('remove', db, pkg, opts=opts)
except repos.RepoAddError as e:
syslog.error(e)