2019-01-28 21:13:01 -08:00
|
|
|
#!/usr/bin/python3
|
|
|
|
""" Add packages to a repository.
|
|
|
|
|
|
|
|
Functions:
|
|
|
|
==========
|
2019-03-25 14:45:38 -07:00
|
|
|
* adds(cachedir, pkgs)
|
2019-01-29 20:06:00 -08:00
|
|
|
* repo_add(db, pkgs, opts=None)
|
2019-01-28 21:13:01 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
2019-01-30 17:06:25 -08:00
|
|
|
import sys
|
2019-01-28 21:13:01 -08:00
|
|
|
|
|
|
|
import packaging_scripts.pacmanconf as pacmanconf
|
|
|
|
import packaging_scripts.pkgfiles as pkgfiles
|
2019-01-29 20:06:00 -08:00
|
|
|
import packaging_scripts.repos as repos
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-03-25 14:20:36 -07:00
|
|
|
from pathlib import Path
|
|
|
|
|
2019-01-28 21:13:01 -08:00
|
|
|
# ========== Constants ==========
|
2019-03-06 23:41:05 -08:00
|
|
|
DB_EXT = "db.tar.xz"
|
|
|
|
LOGFORMAT = "==> %(levelname)s %(message)s"
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-03-25 14:45:38 -07:00
|
|
|
# ----- Exit codes -----
|
2019-01-30 17:06:25 -08:00
|
|
|
E_NOFILESERR = 1
|
2019-01-29 14:20:59 -08:00
|
|
|
E_REPO_ADDERR = 2
|
|
|
|
|
2019-01-28 21:13:01 -08:00
|
|
|
# ========== Logging setup ==========
|
2019-01-30 16:20:13 -08:00
|
|
|
console_formatter = logging.Formatter(LOGFORMAT)
|
2019-03-06 23:41:05 -08:00
|
|
|
syslog = logging.getLogger("packaging_scripts")
|
2019-01-30 17:06:25 -08:00
|
|
|
syslog.setLevel(logging.DEBUG)
|
|
|
|
|
2019-02-20 02:30:17 -08:00
|
|
|
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
|
|
|
|
2019-02-20 02:30:17 -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-28 21:13:01 -08:00
|
|
|
|
|
|
|
|
2019-03-25 14:20:36 -07:00
|
|
|
# ========== Main Script ==========
|
2019-03-06 23:41:05 -08:00
|
|
|
if __name__ == "__main__":
|
2019-01-28 21:13:01 -08:00
|
|
|
parser = argparse.ArgumentParser()
|
2019-03-10 21:04:31 -07:00
|
|
|
parser.add_argument(
|
2019-03-10 21:07:15 -07:00
|
|
|
"-c", "--cachedir", help="alternative directory to use for moving package"
|
2019-03-10 21:04:31 -07:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--db-filename",
|
2019-03-10 21:07:15 -07:00
|
|
|
help="alternative filename for database without extension",
|
2019-03-10 21:04:31 -07:00
|
|
|
)
|
2019-03-24 22:36:06 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"--files-only",
|
|
|
|
action="store_true",
|
|
|
|
help="only add the built files to the repository",
|
|
|
|
)
|
2019-03-06 23:41:05 -08:00
|
|
|
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")
|
2019-01-28 21:13:01 -08:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
repo = args.repository
|
2019-02-23 14:38:10 -08:00
|
|
|
|
2019-03-25 14:20:36 -07:00
|
|
|
opts = [] if args.opts is None else args.opts
|
2019-03-25 14:45:38 -07:00
|
|
|
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-28 21:13:01 -08:00
|
|
|
|
2019-01-30 17:06:25 -08:00
|
|
|
if args.verbose:
|
2019-02-20 02:30:17 -08:00
|
|
|
stdout_handler.setLevel(logging.DEBUG)
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-03-25 17:02:23 -07:00
|
|
|
if args.packages:
|
|
|
|
pkg_tarballs = args.packages
|
2019-01-30 15:54:51 -08:00
|
|
|
sigfiles = []
|
2019-01-29 14:20:59 -08:00
|
|
|
else:
|
2019-03-25 14:45:38 -07:00
|
|
|
pkg_tarballs = list(pkgfiles.get())
|
2019-03-25 17:02:23 -07:00
|
|
|
sigfiles = list(pkgfiles.get(signatures_only=True))
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-01-30 17:06:25 -08:00
|
|
|
if not pkg_tarballs:
|
2019-03-06 23:41:05 -08:00
|
|
|
syslog.critical("No package tarballs have been found, exiting")
|
2019-01-30 17:06:25 -08:00
|
|
|
exit(E_NOFILESERR)
|
|
|
|
|
2019-03-24 22:36:06 -07:00
|
|
|
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)
|
2019-01-29 14:20:59 -08:00
|
|
|
|
2019-02-20 15:22:18 -08:00
|
|
|
for pkgfile in (*pkg_tarballs, *sigfiles):
|
2019-03-25 14:45:38 -07:00
|
|
|
pkgfiles.add(pkgfile, cachedir)
|