112 lines
3.0 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:
==========
* deletes(cachedir, pkgs)
* repo_remove(db, pkgs, opts=None)
2019-01-27 20:26:50 -08:00
"""
import argparse
import logging
2019-02-20 02:50:41 -08:00
import sys
2019-01-27 20:26:50 -08:00
2022-03-17 21:15:33 -07:00
import packaging_scripts.pacman as pacman
2019-01-27 20:26:50 -08:00
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
from pathlib import Path
# ========== Constants ==========
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")
2019-01-30 17:06:25 -08:00
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)
2019-01-30 17:06:25 -08:00
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.WARNING)
stderr_handler.setFormatter(console_formatter)
syslog.addHandler(stdout_handler)
syslog.addHandler(stderr_handler)
2019-01-27 20:26:50 -08:00
# ========== Main Script ==========
if __name__ == "__main__":
2019-01-27 20:26:50 -08:00
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--cachedir", help="alternative directory to use for deleting package"
)
parser.add_argument(
"-d",
"--db-filename",
2019-03-10 21:07:15 -07:00
help="alternative filename for database without extension",
)
parser.add_argument(
"--files-only",
action="store_true",
help="only add the built files to the repository",
)
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",
2022-03-17 21:15:33 -07:00
choices=pacman.list_configured_repos(),
help="the repository to operate on",
metavar="repo",
)
parser.add_argument("packages", nargs="+", help="packages to remove")
2019-01-27 20:26:50 -08:00
args = parser.parse_args()
repo = args.repository
pkgs = args.packages
opts = [] if args.opts is None else args.opts
cachedir = (
Path(args.cachedir)
if args.cachedir
else Path("/var") / "cache" / "pacman" / repo
)
db = (
cachedir / f"{args.db_filename}.{DB_EXT}"
if args.db_filename
else cachedir / f"{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 = list(pkgfiles.get(query=pkg, directory=cachedir))
sigfiles = list(
pkgfiles.get(query=pkg, directory=cachedir, signatures_only=True)
)
2019-01-27 20:26:50 -08:00
for pkgfile in (*pkg_tarballs, *sigfiles):
pkgfiles.delete(pkgfile)
if not args.files_only:
try:
repos.db_modify("remove", db, *opts, *pkgs)
except repos.RepoAddError as e:
syslog.error(e)