Run black python linting utility on all source files

This commit is contained in:
Eric Torres 2019-03-06 23:41:05 -08:00
parent a2ec1729b9
commit 01f41472e2
8 changed files with 77 additions and 78 deletions

View File

@ -1,6 +1,6 @@
# Maintainer: Eric Torres <erictorres4@protonmail.com> # Maintainer: Eric Torres <erictorres4@protonmail.com>
pkgname=packaging-scripts pkgname=packaging-scripts
pkgver=1.0.2 pkgver=1.0.3
pkgrel=1 pkgrel=1
pkgdesc="A set of helper scripts for handling Arch Linux packages" pkgdesc="A set of helper scripts for handling Arch Linux packages"
arch=('any') arch=('any')

View File

@ -18,8 +18,8 @@ import packaging_scripts.pkgfiles as pkgfiles
import packaging_scripts.repos as repos import packaging_scripts.repos as repos
# ========== Constants ========== # ========== Constants ==========
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
@ -27,7 +27,7 @@ E_REPO_ADDERR = 2
# ========== Logging setup ========== # ========== Logging setup ==========
console_formatter = logging.Formatter(LOGFORMAT) console_formatter = logging.Formatter(LOGFORMAT)
syslog = logging.getLogger('packaging_scripts') syslog = logging.getLogger("packaging_scripts")
syslog.setLevel(logging.DEBUG) syslog.setLevel(logging.DEBUG)
stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler = logging.StreamHandler(sys.stdout)
@ -56,24 +56,26 @@ def add_pkgfile(pkgfile, cachedir):
shutil.move(pkgfile, os.path.join(cachedir, os.path.basename(pkgfile))) shutil.move(pkgfile, os.path.join(cachedir, os.path.basename(pkgfile)))
if __name__ == '__main__': if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sign', parser.add_argument(
dest='opts', "-s",
action='append_const', "--sign",
const='--sign', dest="opts",
help='sign repository file') action="append_const",
parser.add_argument('-v', '--verbose', const="--sign",
action='store_true', help="sign repository file",
help='increase script verbosity') )
parser.add_argument('repository', parser.add_argument(
choices=pacmanconf.list_configured_repos(), "-v", "--verbose", action="store_true", help="increase script verbosity"
help='the repository to operate on', )
metavar='repo') parser.add_argument(
parser.add_argument('packages', "repository",
default=None, choices=pacmanconf.list_configured_repos(),
nargs='*', help="the repository to operate on",
help='packages to add') metavar="repo",
)
parser.add_argument("packages", default=None, nargs="*", help="packages to add")
args = parser.parse_args() args = parser.parse_args()
repo = args.repository repo = args.repository
@ -84,7 +86,7 @@ if __name__ == '__main__':
else: else:
opts = args.opts opts = args.opts
cachedir = os.path.join('/var', 'cache', 'pacman', repo) cachedir = os.path.join("/var", "cache", "pacman", repo)
# this assumes that the db file for the repo # this assumes that the db file for the repo
# has the same name as that repo # has the same name as that repo
db = os.path.join(cachedir, f"{repo}.{DB_EXT}") db = os.path.join(cachedir, f"{repo}.{DB_EXT}")
@ -100,11 +102,11 @@ if __name__ == '__main__':
sigfiles = pkgfiles.get_pkgfiles(signatures_only=True) sigfiles = pkgfiles.get_pkgfiles(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")
exit(E_NOFILESERR) exit(E_NOFILESERR)
try: try:
repos.db_modify('add', db, *opts, *pkg_tarballs) repos.db_modify("add", db, *opts, *pkg_tarballs)
except repos.RepoAddError as e: except repos.RepoAddError as e:
syslog.error(e) syslog.error(e)
exit(E_REPO_ADDERR) exit(E_REPO_ADDERR)

View File

@ -17,14 +17,14 @@ import packaging_scripts.pkgfiles as pkgfiles
import packaging_scripts.repos as repos import packaging_scripts.repos as repos
# ========== 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 ==========
console_formatter = logging.Formatter('==> %(levelname)s %(message)s') console_formatter = logging.Formatter("==> %(levelname)s %(message)s")
syslog = logging.getLogger('packaging_scripts') syslog = logging.getLogger("packaging_scripts")
syslog.setLevel(logging.DEBUG) syslog.setLevel(logging.DEBUG)
stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler = logging.StreamHandler(sys.stdout)
@ -51,23 +51,26 @@ def del_pkgfile(pkg):
syslog.info(f"Removed {pkg}") syslog.info(f"Removed {pkg}")
if __name__ == '__main__': if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sign', parser.add_argument(
dest='opts', "-s",
action='append_const', "--sign",
const='--sign', dest="opts",
help='sign repository file') action="append_const",
parser.add_argument('-v', '--verbose', const="--sign",
action='store_true', help="sign repository file",
help='increase script verbosity') )
parser.add_argument('repository', parser.add_argument(
choices=pacmanconf.list_configured_repos(), "-v", "--verbose", action="store_true", help="increase script verbosity"
help='the repository to operate on', )
metavar='repo') parser.add_argument(
parser.add_argument('packages', "repository",
nargs='+', choices=pacmanconf.list_configured_repos(),
help='packages to remove') help="the repository to operate on",
metavar="repo",
)
parser.add_argument("packages", nargs="+", help="packages to remove")
args = parser.parse_args() args = parser.parse_args()
repo = args.repository repo = args.repository
@ -78,7 +81,7 @@ if __name__ == '__main__':
else: else:
opts = args.opts opts = args.opts
cachedir = os.path.join('/var', 'cache', 'pacman', repo) cachedir = os.path.join("/var", "cache", "pacman", repo)
# this assumes that the db file for the repo # this assumes that the db file for the repo
# has the same name as that repo # has the same name as that repo
db = os.path.join(cachedir, f"{repo}.{DB_EXT}") db = os.path.join(cachedir, f"{repo}.{DB_EXT}")
@ -87,16 +90,15 @@ if __name__ == '__main__':
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, pkg_tarballs = pkgfiles.get_pkgfiles(query=pkg, directory=cachedir)
directory=cachedir) sigfiles = pkgfiles.get_pkgfiles(
sigfiles = pkgfiles.get_pkgfiles(query=pkg, query=pkg, directory=cachedir, signatures_only=True
directory=cachedir, )
signatures_only=True)
for pkgfile in (*pkg_tarballs, *sigfiles): for pkgfile in (*pkg_tarballs, *sigfiles):
del_pkgfile(pkgfile) del_pkgfile(pkgfile)
try: try:
repos.db_modify('remove', db, *opts, *pkgs) repos.db_modify("remove", db, *opts, *pkgs)
except repos.RepoAddError as e: except repos.RepoAddError as e:
syslog.error(e) syslog.error(e)

26
bin/fqo
View File

@ -12,12 +12,12 @@ import subprocess
# ========== Constants ========== # ========== Constants ==========
# Commands # Commands
PACMAN_CMD = '/usr/bin/pacman' PACMAN_CMD = "/usr/bin/pacman"
FZF_CMD = '/usr/bin/fzf' FZF_CMD = "/usr/bin/fzf"
FZF_OPTS = ['--read0', '--select-1', '--exit-0', '--print0'] FZF_OPTS = ["--read0", "--select-1", "--exit-0", "--print0"]
LOCATE_CMD = '/usr/bin/locate' LOCATE_CMD = "/usr/bin/locate"
LOCATE_OPTS = ['--all', '--ignore-case', '--null'] LOCATE_OPTS = ["--all", "--ignore-case", "--null"]
LOCALE = 'utf-8' LOCALE = "utf-8"
# ========== Functions ========== # ========== Functions ==========
@ -29,11 +29,11 @@ def run_fzf(files):
:returns: selected file :returns: selected file
:rtype: str :rtype: str
""" """
selected_file = subprocess.run([FZF_CMD] + FZF_OPTS, selected_file = subprocess.run(
input=files, [FZF_CMD] + FZF_OPTS, input=files, stdout=subprocess.PIPE
stdout=subprocess.PIPE).stdout ).stdout
return selected_file.decode(LOCALE).strip('\x00') return selected_file.decode(LOCALE).strip("\x00")
def locate_files(patterns): def locate_files(patterns):
@ -52,13 +52,11 @@ def locate_files(patterns):
# ========== Main Script ========== # ========== Main Script ==========
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('patterns', parser.add_argument("patterns", nargs="+", help="file pattern to search for")
nargs='+',
help='file pattern to search for')
args = parser.parse_args() args = parser.parse_args()
files = locate_files(args.patterns) files = locate_files(args.patterns)
selected_file = run_fzf(files) selected_file = run_fzf(files)
subprocess.run([PACMAN_CMD, '-Qo', selected_file]) subprocess.run([PACMAN_CMD, "-Qo", selected_file])

View File

@ -4,7 +4,7 @@ import configparser
import pathlib import pathlib
# ========== Constants ========== # ========== Constants ==========
PACMAN_CONF = pathlib.Path('/etc/pacman.conf') PACMAN_CONF = pathlib.Path("/etc/pacman.conf")
# ========== Functions ========== # ========== Functions ==========
@ -21,8 +21,7 @@ def parse_configfile(filepath):
if not pathlib.Path(filepath).is_file(): if not pathlib.Path(filepath).is_file():
raise FileNotFoundError(f"{filepath} does not exist") raise FileNotFoundError(f"{filepath} does not exist")
config_reader = configparser.ConfigParser(strict=False, config_reader = configparser.ConfigParser(strict=False, allow_no_value=True)
allow_no_value=True)
config_reader.read(filepath) config_reader.read(filepath)
return config_reader return config_reader

View File

@ -5,7 +5,7 @@ import os
import os.path import os.path
# ========== Constants ========== # ========== Constants ==========
PKGEXT = 'pkg.tar.xz' PKGEXT = "pkg.tar.xz"
SIGEXT = f"{PKGEXT}.sig" SIGEXT = f"{PKGEXT}.sig"

View File

@ -37,10 +37,10 @@ def db_modify(operation, db, *args):
:raises: RepoAddError if repo-add failed :raises: RepoAddError if repo-add failed
:raises: ValueError if an invalid operation was specified :raises: ValueError if an invalid operation was specified
""" """
if operation == 'add': if operation == "add":
syslog.info('Adding packages to database') syslog.info("Adding packages to database")
elif operation == 'remove': elif operation == "remove":
syslog.info('Removing packages from database') syslog.info("Removing packages from database")
else: else:
raise ValueError(f"Invalid operation specified: {operation}") raise ValueError(f"Invalid operation specified: {operation}")
@ -53,7 +53,7 @@ def db_modify(operation, db, *args):
raise RepoAddError(e) raise RepoAddError(e)
else: else:
syslog.debug(process.stdout) syslog.debug(process.stdout)
syslog.info('Database operation complete') syslog.info("Database operation complete")
def _run_script(operation, *args): def _run_script(operation, *args):
@ -66,7 +66,6 @@ def _run_script(operation, *args):
options and packages options and packages
:type args: str :type args: str
""" """
return subprocess.run((f"repo-{operation}", *args), return subprocess.run(
check=True, (f"repo-{operation}", *args), check=True, capture_output=True, text=True
capture_output=True, )
text=True)

View File

@ -1,7 +1,6 @@
#compdef addpkg #compdef addpkg
# zsh completions for 'addpkg' # zsh completions for 'addpkg'
# automatically generated with http://github.com/RobSis/zsh-completion-generator
local arguments local arguments
arguments=( arguments=(