2019-01-28 21:13:01 -08:00
|
|
|
#!/usr/bin/python3
|
|
|
|
""" Add packages to a repository.
|
|
|
|
|
|
|
|
Functions:
|
|
|
|
==========
|
2019-01-29 20:06:00 -08:00
|
|
|
* add_pkgfiles(cachedir, pkgs)
|
|
|
|
* repo_add(db, pkgs, opts=None)
|
2019-01-28 21:13:01 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
2019-02-20 15:52:33 -08:00
|
|
|
import os.path
|
2019-01-30 17:06:25 -08:00
|
|
|
import shutil
|
|
|
|
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
|
|
|
|
|
|
|
# ========== 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-01-29 14:20:59 -08: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
|
|
|
|
|
|
|
|
|
|
|
# ========== Functions ==========
|
2019-02-20 15:52:33 -08:00
|
|
|
def add_pkgfile(pkgfile, cachedir):
|
2019-01-29 14:20:59 -08:00
|
|
|
"""Add package file to the repository directory.
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-01-29 14:20:59 -08:00
|
|
|
:param pkg: path of package to add
|
2019-01-30 17:06:25 -08:00
|
|
|
:type pkg: str, bytes, or path-like object
|
2019-01-29 14:20:59 -08:00
|
|
|
:param cachedir: cache directory to move package to
|
2019-01-30 17:06:25 -08:00
|
|
|
:type cachedir: str, bytes, or path-like object
|
2019-01-28 21:13:01 -08:00
|
|
|
"""
|
2019-02-20 15:52:33 -08:00
|
|
|
syslog.info(f"Adding {pkgfile} to {cachedir}")
|
|
|
|
shutil.move(pkgfile, os.path.join(cachedir, os.path.basename(pkgfile)))
|
2019-01-28 21:13:01 -08:00
|
|
|
|
|
|
|
|
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(
|
|
|
|
"-c",
|
|
|
|
"--cachedir",
|
|
|
|
help="alternative directory to use for moving package"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--db-filename",
|
|
|
|
help="alternative filename for database without extension"
|
|
|
|
)
|
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
|
|
|
|
pkgs = args.packages
|
2019-02-23 14:38:10 -08:00
|
|
|
|
|
|
|
if args.opts is None:
|
|
|
|
opts = []
|
|
|
|
else:
|
|
|
|
opts = args.opts
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-03-10 21:04:31 -07:00
|
|
|
if args.cachedir is not None:
|
|
|
|
cachedir = args.cachedir
|
|
|
|
else:
|
|
|
|
cachedir = os.path.join("/var", "cache", "pacman", repo)
|
|
|
|
|
|
|
|
if args.db-filename is not None:
|
|
|
|
db = os.path.join(cachedir, f"{args.db-filename}.{DB_EXT}")
|
|
|
|
else:
|
|
|
|
db = os.path.join(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-01-29 14:20:59 -08:00
|
|
|
if pkgs:
|
2019-01-30 15:54:51 -08:00
|
|
|
pkg_tarballs = pkgs
|
|
|
|
sigfiles = []
|
2019-01-29 14:20:59 -08:00
|
|
|
else:
|
2019-02-22 07:34:51 -08:00
|
|
|
pkg_tarballs = pkgfiles.get_pkgfiles()
|
|
|
|
sigfiles = pkgfiles.get_pkgfiles(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-01-28 21:13:01 -08:00
|
|
|
try:
|
2019-03-06 23:41:05 -08:00
|
|
|
repos.db_modify("add", db, *opts, *pkg_tarballs)
|
2019-01-29 20:06:00 -08:00
|
|
|
except repos.RepoAddError as e:
|
2019-01-30 17:06:25 -08:00
|
|
|
syslog.error(e)
|
2019-01-29 14:20:59 -08:00
|
|
|
exit(E_REPO_ADDERR)
|
|
|
|
|
2019-02-20 15:22:18 -08:00
|
|
|
for pkgfile in (*pkg_tarballs, *sigfiles):
|
|
|
|
add_pkgfile(pkgfile, cachedir)
|