102 lines
2.9 KiB
Plaintext
Raw Normal View History

2019-01-28 21:13:01 -08:00
#!/usr/bin/python3
""" Add packages to a repository.
Functions:
==========
* add_pkgfiles(cachedir, pkgs)
* repo_add(db, pkgs, opts=None)
2019-01-28 21:13:01 -08:00
"""
import argparse
import logging
import shutil
2019-01-28 21:13:01 -08:00
import sys
import packaging_scripts.pacmanconf as pacmanconf
import packaging_scripts.pkgfiles as pkgfiles
import packaging_scripts.repos as repos
2019-01-28 21:13:01 -08:00
# ========== Constants ==========
REPO_ADD_CMD = '/usr/bin/repo-add'
2019-01-28 21:13:01 -08:00
DB_EXT = 'db.tar.xz'
# ========== Exit codes ==========
E_REPO_ADDERR = 2
2019-01-28 21:13:01 -08:00
# ========== Logging setup ==========
console_formatter = logging.Formatter('==> %(levelname)s %(message)s')
syslog = logging.getLogger(__name__)
syslog.setLevel(logging.DEBUG)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.INFO)
stdout_handler.setFormatter(console_formatter)
stderr_handler = logging.StreamHandler()
stderr_handler.setLevel(logging.ERROR)
stderr_handler.setFormatter(console_formatter)
syslog.addHandler(stdout_handler)
syslog.addHandler(stderr_handler)
# ========== Functions ==========
def add_pkgfile(pkg, cachedir):
"""Add package file to the repository directory.
2019-01-28 21:13:01 -08:00
:param pkg: path of package to add
:type pkg: str, bytes, or path-like object
:param cachedir: cache directory to move package to
:type cachedir: str, bytes, or path-like object
2019-01-28 21:13:01 -08:00
"""
syslog.info(f"Adding {pkg} to {cachedir}")
shutil.move(pkg, cachedir)
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
db = f"{cachedir}/{repo}.{DB_EXT}"
2019-01-28 21:13:01 -08:00
if args.verbose:
stdout_handler.setLevel(logging.DEBUG)
if pkgs:
pkglist = pkgs
siglist = []
else:
pkglist = pkgfiles.get_pkgfiles()
siglist = pkgfiles.get_pkgfiles(signatures_only=True)
2019-01-28 21:13:01 -08:00
try:
repos.repo_add('add', db, *pkglist, opts=opts)
except repos.RepoAddError as e:
2019-01-28 21:13:01 -08:00
syslog.error(e)
exit(E_REPO_ADDERR)
map(lambda pkg: add_pkgfile(pkg, cachedir), pkglist + siglist)