Logging and code cleanup

This commit is contained in:
Eric Torres 2019-01-30 17:06:25 -08:00
parent 40f5f711ff
commit d97d1648b9
2 changed files with 37 additions and 31 deletions

View File

@ -9,7 +9,9 @@ Functions:
import argparse import argparse
import logging import logging
import os import pathlib
import shutil
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
@ -21,12 +23,19 @@ DB_EXT = 'db.tar.xz'
LOGFORMAT = '==> %(levelname)s %(message)s' LOGFORMAT = '==> %(levelname)s %(message)s'
# ========== Exit codes ========== # ========== Exit codes ==========
E_NOFILESERR = 1
E_REPO_ADDERR = 2 E_REPO_ADDERR = 2
# ========== Logging setup ========== # ========== Logging setup ==========
console_formatter = logging.Formatter(LOGFORMAT) console_formatter = logging.Formatter(LOGFORMAT)
logging.basicConfig(format=LOGFORMAT, syslog = logging.getLogger('packaging_scripts')
level=logging.DEBUG) syslog.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.ERROR)
console_handler.setFormatter(console_formatter)
syslog.addHandler(console_handler)
# ========== Functions ========== # ========== Functions ==========
@ -34,12 +43,15 @@ def add_pkgfile(pkg, cachedir):
"""Add package file to the repository directory. """Add package file to the repository directory.
:param pkg: path of package to add :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 :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}") syslog.info(f"Adding {pkg} to {cachedir}")
os.rename(pkg, cachedir.joinpath(pkg.name))
pkgpath = pathlib.Path(pkg)
cachepath = pathlib.Path(cachedir)
shutil.move(str(pkgpath), str(cachepath.joinpath(pkg.name)))
if __name__ == '__main__': if __name__ == '__main__':
@ -71,8 +83,8 @@ if __name__ == '__main__':
# has the same name as that repo # has the same name as that repo
db = f"{cachedir}/{repo}.{DB_EXT}" db = f"{cachedir}/{repo}.{DB_EXT}"
# if args.verbose: if args.verbose:
# stdout_handler.setLevel(logging.DEBUG) stdout_handler.setLevel(logging.DEBUG)
if pkgs: if pkgs:
pkg_tarballs = pkgs pkg_tarballs = pkgs
@ -81,10 +93,14 @@ if __name__ == '__main__':
pkg_tarballs = list(pkgfiles.get_pkgfiles()) pkg_tarballs = list(pkgfiles.get_pkgfiles())
sigfiles = list(pkgfiles.get_pkgfiles(signatures_only=True)) 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: try:
repos.repo_add('add', db, *pkg_tarballs, opts=opts) repos.repo_add('add', db, *pkg_tarballs, opts=opts)
except repos.RepoAddError as e: except repos.RepoAddError as e:
logging.error(e) syslog.error(e)
exit(E_REPO_ADDERR) exit(E_REPO_ADDERR)
for pkg_tarball in pkg_tarballs: for pkg_tarball in pkg_tarballs:

View File

@ -9,7 +9,6 @@ Functions:
import argparse import argparse
import logging import logging
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
@ -24,20 +23,14 @@ E_REPO_REMOVEERR = 2
# ========== Logging setup ========== # ========== Logging setup ==========
console_formatter = logging.Formatter('==> %(levelname)s %(message)s') console_formatter = logging.Formatter('==> %(levelname)s %(message)s')
#logging = logging.getLogger(__name__) syslog = logging.getLogger('packaging_scripts')
logging.basicConfig(format='==> %(levelname)s %(message)s', syslog.setLevel(logging.DEBUG)
level=logging.DEBUG)
#stdout_handler = logging.StreamHandler(sys.stdout) console_handler = logging.StreamHandler()
#stdout_handler.setLevel(logging.INFO) console_handler.setLevel(logging.ERROR)
#stdout_handler.setFormatter(console_formatter) console_handler.setFormatter(console_formatter)
#
#stderr_handler = logging.StreamHandler() syslog.addHandler(console_handler)
#stderr_handler.setLevel(logging.ERROR)
#stderr_handler.setFormatter(console_formatter)
#
#logging.addHandler(stdout_handler)
#logging.addHandler(stderr_handler)
# ========== Functions ========== # ========== Functions ==========
@ -48,7 +41,7 @@ def del_pkgfile(pkg):
:type pkg: pathlib.Path object :type pkg: pathlib.Path object
""" """
pkg.unlink() pkg.unlink()
logging.info(f"Removed {pkg}") syslog.info(f"Removed {pkg}")
if __name__ == '__main__': if __name__ == '__main__':
@ -79,10 +72,8 @@ if __name__ == '__main__':
# has the same name as that repo # has the same name as that repo
db = f"{cachedir}/{repo}.{DB_EXT}" db = f"{cachedir}/{repo}.{DB_EXT}"
# if args.verbose: if args.verbose:
# stdout_handler.setLevel(logging.DEBUG) stdout_handler.setLevel(logging.DEBUG)
logging.debug(f"Packages: {pkgs}")
for pkg in pkgs: for pkg in pkgs:
pkg_tarballs = pkgfiles.get_pkgfiles(query=pkg, pkg_tarballs = pkgfiles.get_pkgfiles(query=pkg,
@ -100,5 +91,4 @@ if __name__ == '__main__':
try: try:
repos.repo_add('remove', db, pkg, opts=opts) repos.repo_add('remove', db, pkg, opts=opts)
except repos.RepoAddError as e: except repos.RepoAddError as e:
logging.error(e) syslog.error(e)
pass