From d97d1648b951b8e2c8d9eab68139a9d38a9d1d81 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Wed, 30 Jan 2019 17:06:25 -0800 Subject: [PATCH] Logging and code cleanup --- bin/addpkg | 36 ++++++++++++++++++++++++++---------- bin/delpkg | 32 +++++++++++--------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/bin/addpkg b/bin/addpkg index a793b35..263762c 100644 --- a/bin/addpkg +++ b/bin/addpkg @@ -9,7 +9,9 @@ Functions: import argparse import logging -import os +import pathlib +import shutil +import sys import packaging_scripts.pacmanconf as pacmanconf import packaging_scripts.pkgfiles as pkgfiles @@ -21,12 +23,19 @@ DB_EXT = 'db.tar.xz' LOGFORMAT = '==> %(levelname)s %(message)s' # ========== Exit codes ========== +E_NOFILESERR = 1 E_REPO_ADDERR = 2 # ========== Logging setup ========== console_formatter = logging.Formatter(LOGFORMAT) -logging.basicConfig(format=LOGFORMAT, - level=logging.DEBUG) +syslog = logging.getLogger('packaging_scripts') +syslog.setLevel(logging.DEBUG) + +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.ERROR) +console_handler.setFormatter(console_formatter) + +syslog.addHandler(console_handler) # ========== Functions ========== @@ -34,12 +43,15 @@ def add_pkgfile(pkg, cachedir): """Add package file to the repository directory. :param pkg: path of package to add - :type pkg: pathlib.Path object + :type pkg: str, bytes, or path-like object :param cachedir: cache directory to move package to - :type cachedir: pathlib.Path object + :type cachedir: str, bytes, or path-like object """ - logging.info(f"Adding {pkg} to {cachedir}") - os.rename(pkg, cachedir.joinpath(pkg.name)) + syslog.info(f"Adding {pkg} to {cachedir}") + + pkgpath = pathlib.Path(pkg) + cachepath = pathlib.Path(cachedir) + shutil.move(str(pkgpath), str(cachepath.joinpath(pkg.name))) if __name__ == '__main__': @@ -71,8 +83,8 @@ if __name__ == '__main__': # has the same name as that repo db = f"{cachedir}/{repo}.{DB_EXT}" -# if args.verbose: -# stdout_handler.setLevel(logging.DEBUG) + if args.verbose: + stdout_handler.setLevel(logging.DEBUG) if pkgs: pkg_tarballs = pkgs @@ -81,10 +93,14 @@ if __name__ == '__main__': pkg_tarballs = list(pkgfiles.get_pkgfiles()) sigfiles = list(pkgfiles.get_pkgfiles(signatures_only=True)) + if not pkg_tarballs: + syslog.critical('No package tarballs have been found, exiting') + exit(E_NOFILESERR) + try: repos.repo_add('add', db, *pkg_tarballs, opts=opts) except repos.RepoAddError as e: - logging.error(e) + syslog.error(e) exit(E_REPO_ADDERR) for pkg_tarball in pkg_tarballs: diff --git a/bin/delpkg b/bin/delpkg index a93b93b..7dee23a 100644 --- a/bin/delpkg +++ b/bin/delpkg @@ -9,7 +9,6 @@ Functions: import argparse import logging -import sys import packaging_scripts.pacmanconf as pacmanconf import packaging_scripts.pkgfiles as pkgfiles @@ -24,20 +23,14 @@ E_REPO_REMOVEERR = 2 # ========== Logging setup ========== console_formatter = logging.Formatter('==> %(levelname)s %(message)s') -#logging = logging.getLogger(__name__) -logging.basicConfig(format='==> %(levelname)s %(message)s', - level=logging.DEBUG) +syslog = logging.getLogger('packaging_scripts') +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) -# -#logging.addHandler(stdout_handler) -#logging.addHandler(stderr_handler) +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.ERROR) +console_handler.setFormatter(console_formatter) + +syslog.addHandler(console_handler) # ========== Functions ========== @@ -48,7 +41,7 @@ def del_pkgfile(pkg): :type pkg: pathlib.Path object """ pkg.unlink() - logging.info(f"Removed {pkg}") + syslog.info(f"Removed {pkg}") if __name__ == '__main__': @@ -79,10 +72,8 @@ if __name__ == '__main__': # has the same name as that repo db = f"{cachedir}/{repo}.{DB_EXT}" -# if args.verbose: -# stdout_handler.setLevel(logging.DEBUG) - - logging.debug(f"Packages: {pkgs}") + if args.verbose: + stdout_handler.setLevel(logging.DEBUG) for pkg in pkgs: pkg_tarballs = pkgfiles.get_pkgfiles(query=pkg, @@ -100,5 +91,4 @@ if __name__ == '__main__': try: repos.repo_add('remove', db, pkg, opts=opts) except repos.RepoAddError as e: - logging.error(e) - pass + syslog.error(e)