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

View File

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