Code cleanup and add support for generator functions

This commit is contained in:
Eric Torres 2019-03-25 14:45:38 -07:00
parent bd0030f576
commit 0ef38d5b1a
2 changed files with 34 additions and 40 deletions

View File

@ -3,13 +3,12 @@
Functions: Functions:
========== ==========
* add_pkgfiles(cachedir, pkgs) * adds(cachedir, pkgs)
* repo_add(db, pkgs, opts=None) * repo_add(db, pkgs, opts=None)
""" """
import argparse import argparse
import logging import logging
import shutil
import sys import sys
import packaging_scripts.pacmanconf as pacmanconf import packaging_scripts.pacmanconf as pacmanconf
@ -22,7 +21,7 @@ from pathlib import Path
DB_EXT = "db.tar.xz" DB_EXT = "db.tar.xz"
LOGFORMAT = "==> %(levelname)s %(message)s" LOGFORMAT = "==> %(levelname)s %(message)s"
# ========== Exit codes ========== # ----- Exit codes -----
E_NOFILESERR = 1 E_NOFILESERR = 1
E_REPO_ADDERR = 2 E_REPO_ADDERR = 2
@ -84,8 +83,16 @@ if __name__ == "__main__":
pkgs = args.packages pkgs = args.packages
opts = [] if args.opts is None else args.opts opts = [] if args.opts is None else args.opts
cachedir = Path(args.cachedir) if args.cachedir else Path('/var') / "cache" / "pacman" / repo cachedir = (
db = cachedir / f"{args.db_filename}.{DB_EXT}" if args.db_filename else cachedir / f"{repo}.{DB_EXT}" 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: if args.verbose:
stdout_handler.setLevel(logging.DEBUG) stdout_handler.setLevel(logging.DEBUG)
@ -94,8 +101,8 @@ if __name__ == "__main__":
pkg_tarballs = pkgs pkg_tarballs = pkgs
sigfiles = [] sigfiles = []
else: else:
pkg_tarballs = pkgfiles.get_pkgfiles() pkg_tarballs = list(pkgfiles.get())
sigfiles = pkgfiles.get_pkgfiles(signatures_only=True) sigfiles = pkgfiles.get(signatures_only=True)
if not pkg_tarballs: if not pkg_tarballs:
syslog.critical("No package tarballs have been found, exiting") syslog.critical("No package tarballs have been found, exiting")
@ -109,4 +116,4 @@ if __name__ == "__main__":
exit(E_REPO_ADDERR) exit(E_REPO_ADDERR)
for pkgfile in (*pkg_tarballs, *sigfiles): for pkgfile in (*pkg_tarballs, *sigfiles):
pkgfiles.add_pkgfile(pkgfile, cachedir) pkgfiles.add(pkgfile, cachedir)

View File

@ -3,23 +3,24 @@
Functions: Functions:
========== ==========
* del_pkgfiles(cachedir, pkgs) * deletes(cachedir, pkgs)
* repo_remove(db, pkgs, opts=None) * repo_remove(db, pkgs, opts=None)
""" """
import argparse import argparse
import logging import logging
import os
import sys import sys
import packaging_scripts.pacmanconf as pacmanconf import packaging_scripts.pacmanconf as pacmanconf
import packaging_scripts.pkgfiles as pkgfiles import packaging_scripts.pkgfiles as pkgfiles
import packaging_scripts.repos as repos import packaging_scripts.repos as repos
from pathlib import Path
# ========== Constants ========== # ========== Constants ==========
DB_EXT = "db.tar.xz" DB_EXT = "db.tar.xz"
# ========== Exit codes ========== # ----- Exit codes -----
E_REPO_REMOVEERR = 2 E_REPO_REMOVEERR = 2
# ========== Logging setup ========== # ========== Logging setup ==========
@ -40,17 +41,6 @@ syslog.addHandler(stdout_handler)
syslog.addHandler(stderr_handler) syslog.addHandler(stderr_handler)
# ========== Functions ==========
def del_pkgfile(pkg):
"""Remove package file.
:param pkg: path of package to remove
:type pkg: str
"""
os.remove(pkg)
syslog.info(f"Removed {pkg}")
# ========== Main Script ========== # ========== Main Script ==========
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -90,32 +80,29 @@ if __name__ == "__main__":
repo = args.repository repo = args.repository
pkgs = args.packages pkgs = args.packages
if args.opts is None: opts = [] if args.opts is None else args.opts
opts = [] cachedir = (
else: Path(args.cachedir)
opts = args.opts if args.cachedir
else Path("/var") / "cache" / "pacman" / repo
if args.cachedir is not None: )
cachedir = args.cachedir db = (
else: cachedir / f"{args.db_filename}.{DB_EXT}"
cachedir = os.path.join("/var", "cache", "pacman", repo) if args.db_filename
else cachedir / f"{repo}.{DB_EXT}"
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}")
if args.verbose: if args.verbose:
stdout_handler.setLevel(logging.DEBUG) stdout_handler.setLevel(logging.DEBUG)
for pkg in pkgs: for pkg in pkgs:
pkg_tarballs = pkgfiles.get_pkgfiles(query=pkg, directory=cachedir) pkg_tarballs = list(pkgfiles.get(query=pkg, directory=cachedir))
sigfiles = pkgfiles.get_pkgfiles( sigfiles = list(
query=pkg, directory=cachedir, signatures_only=True pkgfiles.get(query=pkg, directory=cachedir, signatures_only=True)
) )
for pkgfile in (*pkg_tarballs, *sigfiles): for pkgfile in (*pkg_tarballs, *sigfiles):
del_pkgfile(pkgfile) pkgfiles.delete(pkgfile)
if not args.files_only: if not args.files_only:
try: try: