#!/usr/bin/python3 """ Add packages to a repository. Functions: ========== * add_pkgfiles(cachedir, pkgs) * repo_add(db, pkgs, opts=None) """ import argparse import logging import shutil import sys import packaging_scripts.pacmanconf as pacmanconf import packaging_scripts.pkgfiles as pkgfiles import packaging_scripts.repos as repos from pathlib import Path # ========== Constants ========== DB_EXT = "db.tar.xz" LOGFORMAT = "==> %(levelname)s %(message)s" # ========== Exit codes ========== E_NOFILESERR = 1 E_REPO_ADDERR = 2 # ========== Logging setup ========== console_formatter = logging.Formatter(LOGFORMAT) 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) # ========== Main Script ========== if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "-c", "--cachedir", help="alternative directory to use for moving package" ) parser.add_argument( "-d", "--db-filename", 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", choices=pacmanconf.list_configured_repos(), help="the repository to operate on", metavar="repo", ) parser.add_argument("packages", default=None, nargs="*", help="packages to add") 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}" if args.verbose: stdout_handler.setLevel(logging.DEBUG) if pkgs: pkg_tarballs = pkgs sigfiles = [] else: pkg_tarballs = pkgfiles.get_pkgfiles() sigfiles = pkgfiles.get_pkgfiles(signatures_only=True) if not pkg_tarballs: syslog.critical("No package tarballs have been found, exiting") exit(E_NOFILESERR) if not args.files_only: try: repos.db_modify("add", db, *opts, *pkg_tarballs) except repos.RepoAddError as e: syslog.error(e) exit(E_REPO_ADDERR) for pkgfile in (*pkg_tarballs, *sigfiles): pkgfiles.add_pkgfile(pkgfile, cachedir)