132 lines
3.6 KiB
Plaintext
Raw Normal View History

2019-01-28 08:10:42 -08:00
#!/usr/bin/python3
2019-01-27 20:26:50 -08:00
""" Delete packages from a repository.
2019-01-27 20:26:50 -08:00
Functions:
==========
* del_pkgfiles(cachedir, pkgs)
* repo_remove(db, pkgs, opts=None)
2019-01-27 20:26:50 -08:00
"""
import argparse
import logging
import shutil
2019-01-27 20:26:50 -08:00
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'
# ========== Exit codes ==========
E_REPO_REMOVEERR = 2
# ========== Logging setup ==========
2019-01-28 10:46:27 -08:00
console_formatter = logging.Formatter('==> %(levelname)s %(message)s')
2019-01-27 20:26:50 -08:00
syslog = logging.getLogger(__name__)
syslog.setLevel(logging.DEBUG)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.INFO)
stdout_handler.setFormatter(console_formatter)
stderr_handler = logging.StreamHandler()
stderr_handler.setLevel(logging.ERROR)
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: str, bytes, or path-like object
2019-01-27 20:26:50 -08:00
"""
shutil.remove(pkg)
syslog.info(f"Removed {pkg}")
2019-01-27 20:26:50 -08:00
def repo_remove(db, pkgs, opts=None):
2019-01-28 21:27:32 -08:00
"""Run repo-remove.
: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
2019-01-27 20:26:50 -08:00
"""
syslog.info('Removing packages from database')
syslog.debug(f"Options: {opts}")
syslog.debug(f"Packages: {pkgs}")
cmd = [REPO_REMOVE_CMD]
2019-01-27 20:26:50 -08:00
if opts is not None:
cmd.extend(opts)
cmd.append(db)
2019-01-27 20:26:50 -08:00
cmd.extend(pkgs)
syslog.debug(f"Final repo-remove command: {cmd}")
remove_process = subprocess.run(cmd,
check=True,
capture_output=True,
text=True)
syslog.debug(remove_process.stdout)
if remove_process.stderr:
syslog.error(remove_process.stderr)
syslog.info('Finished removing packages from database')
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',
default=None,
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}"
2019-01-27 20:26:50 -08:00
if args.verbose:
stdout_handler.setLevel(logging.DEBUG)
pkglist = pkgfiles.get_pkgfiles(directory=cachedir) +\
pkgfiles.get_pkgfiles(directory=cachedir, signatures_only=True)
map(lambda pkg: del_pkgfile(pkg), pkglist)
2019-01-27 20:26:50 -08:00
try:
repo_remove(repo, pkglist, opts)
2019-01-27 20:26:50 -08:00
except subprocess.CalledProcessError as e:
syslog.error(e)
exit(E_REPO_REMOVEERR)