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-28 08:08:51 -08:00
|
|
|
|
2019-01-27 20:26:50 -08:00
|
|
|
Functions:
|
2019-01-28 08:08:51 -08:00
|
|
|
==========
|
2019-01-29 14:33:12 -08:00
|
|
|
* del_pkgfiles(cachedir, pkgs)
|
|
|
|
* repo_remove(db, pkgs, opts=None)
|
2019-01-27 20:26:50 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import packaging_scripts.pacmanconf as pacmanconf
|
|
|
|
import packaging_scripts.pkgfiles as pkgfiles
|
2019-01-30 15:54:51 -08:00
|
|
|
import packaging_scripts.repos as repos
|
2019-01-27 20:26:50 -08:00
|
|
|
|
2019-01-28 08:08:51 -08:00
|
|
|
# ========== Constants ==========
|
|
|
|
REPO_REMOVE_CMD = '/usr/bin/repo-remove'
|
|
|
|
DB_EXT = 'db.tar.xz'
|
|
|
|
|
2019-01-29 14:33:12 -08:00
|
|
|
# ========== Exit codes ==========
|
|
|
|
E_REPO_REMOVEERR = 2
|
|
|
|
|
2019-01-28 08:08:51 -08:00
|
|
|
# ========== Logging setup ==========
|
2019-01-28 10:46:27 -08:00
|
|
|
console_formatter = logging.Formatter('==> %(levelname)s %(message)s')
|
2019-01-30 17:06:25 -08:00
|
|
|
syslog = logging.getLogger('packaging_scripts')
|
|
|
|
syslog.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
console_handler = logging.StreamHandler()
|
2019-01-30 17:09:19 -08:00
|
|
|
console_handler.setLevel(logging.INFO)
|
2019-01-30 17:06:25 -08:00
|
|
|
console_handler.setFormatter(console_formatter)
|
|
|
|
|
|
|
|
syslog.addHandler(console_handler)
|
2019-01-27 20:26:50 -08:00
|
|
|
|
|
|
|
|
2019-01-28 08:08:51 -08:00
|
|
|
# ========== Functions ==========
|
2019-01-29 14:33:12 -08:00
|
|
|
def del_pkgfile(pkg):
|
|
|
|
"""Remove package file.
|
2019-01-28 08:08:51 -08:00
|
|
|
|
2019-01-29 14:33:12 -08:00
|
|
|
:param pkg: path of package to remove
|
2019-01-30 15:54:51 -08:00
|
|
|
:type pkg: pathlib.Path object
|
2019-01-27 20:26:50 -08:00
|
|
|
"""
|
2019-01-30 15:54:51 -08:00
|
|
|
pkg.unlink()
|
2019-01-30 17:06:25 -08:00
|
|
|
syslog.info(f"Removed {pkg}")
|
2019-01-27 20:26:50 -08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2019-01-28 08:08:51 -08:00
|
|
|
|
|
|
|
cachedir = f"/var/cache/pacman/{repo}"
|
|
|
|
# this assumes that the db file for the repo
|
|
|
|
# has the same name as that repo
|
2019-01-29 14:33:12 -08:00
|
|
|
db = f"{cachedir}/{repo}.{DB_EXT}"
|
2019-01-27 20:26:50 -08:00
|
|
|
|
2019-01-30 17:06:25 -08:00
|
|
|
if args.verbose:
|
|
|
|
stdout_handler.setLevel(logging.DEBUG)
|
2019-01-30 15:54:51 -08:00
|
|
|
|
|
|
|
for pkg in pkgs:
|
|
|
|
pkg_tarballs = pkgfiles.get_pkgfiles(query=pkg,
|
|
|
|
directory=cachedir)
|
|
|
|
sigfiles = pkgfiles.get_pkgfiles(query=pkg,
|
|
|
|
directory=cachedir,
|
|
|
|
signatures_only=True)
|
2019-01-27 20:26:50 -08:00
|
|
|
|
2019-01-30 15:54:51 -08:00
|
|
|
for pkg_tarball in pkg_tarballs:
|
|
|
|
del_pkgfile(pkg_tarball)
|
2019-01-29 14:33:12 -08:00
|
|
|
|
2019-01-30 15:54:51 -08:00
|
|
|
for sigfile in sigfiles:
|
|
|
|
del_pkgfile(sigfile)
|
2019-01-27 20:26:50 -08:00
|
|
|
|
2019-01-30 15:54:51 -08:00
|
|
|
try:
|
|
|
|
repos.repo_add('remove', db, pkg, opts=opts)
|
|
|
|
except repos.RepoAddError as e:
|
2019-01-30 17:06:25 -08:00
|
|
|
syslog.error(e)
|