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-01-30 16:20:13 -08:00
|
|
|
import os
|
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-01-29 14:20:59 -08:00
|
|
|
REPO_ADD_CMD = '/usr/bin/repo-add'
|
2019-01-28 21:13:01 -08:00
|
|
|
DB_EXT = 'db.tar.xz'
|
2019-01-30 16:20:13 -08:00
|
|
|
LOGFORMAT = '==> %(levelname)s %(message)s'
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-01-29 14:20:59 -08:00
|
|
|
# ========== Exit codes ==========
|
|
|
|
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)
|
|
|
|
logging.basicConfig(format=LOGFORMAT,
|
2019-01-30 15:54:51 -08:00
|
|
|
level=logging.DEBUG)
|
2019-01-28 21:13:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
# ========== Functions ==========
|
2019-01-29 14:20:59 -08:00
|
|
|
def add_pkgfile(pkg, cachedir):
|
|
|
|
"""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 16:20:13 -08:00
|
|
|
:type pkg: pathlib.Path object
|
2019-01-29 14:20:59 -08:00
|
|
|
:param cachedir: cache directory to move package to
|
2019-01-30 16:20:13 -08:00
|
|
|
:type cachedir: pathlib.Path object
|
2019-01-28 21:13:01 -08:00
|
|
|
"""
|
2019-01-30 15:54:51 -08:00
|
|
|
logging.info(f"Adding {pkg} to {cachedir}")
|
2019-01-30 16:20:13 -08:00
|
|
|
os.rename(pkg, cachedir.joinpath(pkg.name))
|
2019-01-28 21:13:01 -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',
|
|
|
|
default=None,
|
2019-01-28 21:18:19 -08:00
|
|
|
nargs='*',
|
|
|
|
help='packages to add')
|
2019-01-28 21:13:01 -08:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
repo = args.repository
|
|
|
|
pkgs = args.packages
|
|
|
|
opts = args.opts
|
|
|
|
|
|
|
|
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:20:59 -08:00
|
|
|
db = f"{cachedir}/{repo}.{DB_EXT}"
|
2019-01-28 21:13:01 -08:00
|
|
|
|
2019-01-30 15:54:51 -08:00
|
|
|
# if args.verbose:
|
|
|
|
# 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-01-30 15:54:51 -08:00
|
|
|
pkg_tarballs = list(pkgfiles.get_pkgfiles())
|
|
|
|
sigfiles = list(pkgfiles.get_pkgfiles(signatures_only=True))
|
2019-01-28 21:13:01 -08:00
|
|
|
|
|
|
|
try:
|
2019-01-30 15:54:51 -08:00
|
|
|
repos.repo_add('add', db, *pkg_tarballs, opts=opts)
|
2019-01-29 20:06:00 -08:00
|
|
|
except repos.RepoAddError as e:
|
2019-01-30 15:54:51 -08:00
|
|
|
logging.error(e)
|
2019-01-29 14:20:59 -08:00
|
|
|
exit(E_REPO_ADDERR)
|
|
|
|
|
2019-01-30 15:54:51 -08:00
|
|
|
for pkg_tarball in pkg_tarballs:
|
|
|
|
add_pkgfile(pkg_tarball, cachedir)
|
|
|
|
|
|
|
|
for sigfile in sigfiles:
|
|
|
|
add_pkgfile(sigfile, cachedir)
|