From 0dd3081896bd83db133b1c781c2ca5c94d25e640 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Apr 27 2021 07:18:08 +0000 Subject: [PATCH 1/2] Don't sys.exit() from library function Related to: https://pagure.io/copr/copr/issue/1794 https://pagure.io/copr/copr/issue/1791 --- diff --git a/prunerepo/__init__.py b/prunerepo/__init__.py index a46daee..dcaba9e 100644 --- a/prunerepo/__init__.py +++ b/prunerepo/__init__.py @@ -1,3 +1,3 @@ """ Prunerepo API methods """ -from .helpers import get_rpms_to_remove +from .helpers import get_rpms_to_remove, PrunerepoException diff --git a/prunerepo/helpers.py b/prunerepo/helpers.py index 6e096f7..9d7d2cf 100644 --- a/prunerepo/helpers.py +++ b/prunerepo/helpers.py @@ -16,6 +16,10 @@ import logging from prunerepo.pair_srpm_rpm import RPMToSRPMPairs +class PrunerepoException(Exception): + """ Returned upon failure """ + + def is_srpm(package): """ Check if the PACKAGE string ends with src.rpm """ return package.endswith(".src.rpm") @@ -36,14 +40,15 @@ def run_cmd(cmd, log, dry_run=False): """ Run given command in a subprocess """ - log.debug("Executing: " + ' '.join(cmd)) + str_cmd = ' '.join(cmd) + log.debug("Executing: %s", str_cmd) if dry_run: return [] process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = process.communicate() sys.stderr.write(stderr.decode(encoding='utf-8')) if process.returncode != 0: - sys.exit(1) + raise PrunerepoException("Command {} failed".format(str_cmd)) return stdout.decode(encoding='utf-8').splitlines() @@ -149,6 +154,8 @@ def get_rpms_to_remove(directory, days=0, log=None): :param days: how old are the packages to be removed, in the number of days :param log: logger to use, if not specified an INFO stderr logger is created :return: a list of (s)RPM path names that should be removed + :raises PrunerepoException: Upon any failure that could provide bad results + causing unwanted RPM removals. """ get_all_packages_cmd = [ "dnf", diff --git a/prunerepo/main.py b/prunerepo/main.py index f3cf5a5..419a43b 100644 --- a/prunerepo/main.py +++ b/prunerepo/main.py @@ -9,6 +9,7 @@ from prunerepo.helpers import ( recreate_repo, clean_copr, get_logger, + PrunerepoException, ) @@ -56,12 +57,15 @@ def main(): args = _get_parser() log = get_logger(args.log_level) - was_deletion = prune_packages(args.path, args.days, args.dry_run, log) - if (was_deletion or args.alwayscreaterepo) and not args.nocreaterepo: - recreate_repo(args.path, args.dry_run, log) + try: + was_deletion = prune_packages(args.path, args.days, args.dry_run, log) + if (was_deletion or args.alwayscreaterepo) and not args.nocreaterepo: + recreate_repo(args.path, args.dry_run, log) - if args.cleancopr: - clean_copr(args.path, args.days, args.dry_run, log) + if args.cleancopr: + clean_copr(args.path, args.days, args.dry_run, log) + except PrunerepoException as err: + log.error("Can not continue: %s", err) if __name__ == "__main__": From cc1def2662ea4f24de1b039ac79b7e0e2d763b96 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Apr 27 2021 09:55:59 +0000 Subject: [PATCH 2/2] Move the stderr output to log Closes: #13 --- diff --git a/prunerepo/helpers.py b/prunerepo/helpers.py index 9d7d2cf..fad350d 100644 --- a/prunerepo/helpers.py +++ b/prunerepo/helpers.py @@ -46,7 +46,10 @@ def run_cmd(cmd, log, dry_run=False): return [] process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = process.communicate() - sys.stderr.write(stderr.decode(encoding='utf-8')) + err_output = stderr.decode(encoding='utf-8') + if err_output: + log.debug("Command error output: %s", err_output) + if process.returncode != 0: raise PrunerepoException("Command {} failed".format(str_cmd)) return stdout.decode(encoding='utf-8').splitlines()