From efa3ad830678772c05bf6e5c15bc4f5bf0ea3928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Thu, 30 Jan 2025 11:42:15 +0000 Subject: [PATCH] Fix formatting and linting issues --- python/spacewalk/common/repo.py | 1 + python/spacewalk/common/rhnLog.py | 6 +- .../satellite_tools/repo_plugins/deb_src.py | 6 +- .../satellite_tools/spacewalk-repo-sync | 379 ++++++++++++------ 4 files changed, 271 insertions(+), 121 deletions(-) diff --git a/python/spacewalk/common/repo.py b/python/spacewalk/common/repo.py index d0f72dfdcfb..ee0f150bdc4 100644 --- a/python/spacewalk/common/repo.py +++ b/python/spacewalk/common/repo.py @@ -26,6 +26,7 @@ logger = logging.getLogger(__name__) + class GeneralRepoException(Exception): """ Dpkg repository exception diff --git a/python/spacewalk/common/rhnLog.py b/python/spacewalk/common/rhnLog.py index f18beb893fe..f7efcc9b5a2 100644 --- a/python/spacewalk/common/rhnLog.py +++ b/python/spacewalk/common/rhnLog.py @@ -308,6 +308,7 @@ def _exit(): atexit.register(_exit) + def log_level_to_logging_constant(rhnLog_log_level: int): mapping = { 0: logging.ERROR, @@ -318,6 +319,7 @@ def log_level_to_logging_constant(rhnLog_log_level: int): # 4+: logging.DEBUG return mapping.get(rhnLog_log_level, logging.DEBUG) + def align_root_logger(): """Align the root logger with LOG. @@ -336,8 +338,7 @@ def align_root_logger(): handler = logging.FileHandler(filename=LOG.file) formatter = logging.Formatter( - fmt="%(asctime)s - %(name)s - %(message)s", - datefmt="%Y/%m/%d %H:%M:%S" + fmt="%(asctime)s - %(name)s - %(message)s", datefmt="%Y/%m/%d %H:%M:%S" ) handler.setFormatter(formatter) root_logger = logging.getLogger(None) @@ -345,7 +346,6 @@ def align_root_logger(): root_logger.setLevel(log_level_to_logging_constant(LOG.level)) - # ------------------------------------------------------------------------------ if __name__ == "__main__": print("You can not run this module by itself") diff --git a/python/spacewalk/satellite_tools/repo_plugins/deb_src.py b/python/spacewalk/satellite_tools/repo_plugins/deb_src.py index bdc38f1e350..23fbbf81d0f 100644 --- a/python/spacewalk/satellite_tools/repo_plugins/deb_src.py +++ b/python/spacewalk/satellite_tools/repo_plugins/deb_src.py @@ -493,7 +493,11 @@ def list_packages(self, filters, latest): pack.name, pack.version, pack.release, pack.epoch, pack.arch ) except ValueError as e: - log.error("Skipping package %s. Package contains incorrect metadata.\n%s", new_pack, e) + log.error( + "Skipping package %s. Package contains incorrect metadata.\n%s", + new_pack, + e, + ) continue new_pack.unique_id = pack new_pack.checksum_type = pack.checksum_type diff --git a/python/spacewalk/satellite_tools/spacewalk-repo-sync b/python/spacewalk/satellite_tools/spacewalk-repo-sync index 43b30df165b..0dd74af2fca 100755 --- a/python/spacewalk/satellite_tools/spacewalk-repo-sync +++ b/python/spacewalk/satellite_tools/spacewalk-repo-sync @@ -1,4 +1,5 @@ #!/usr/bin/python3 -u +# pylint: disable=missing-module-docstring,invalid-name # # Copyright (c) 2008--2017 Red Hat, Inc. # Copyright (c) 2011 SUSE LLC @@ -16,16 +17,13 @@ # import re -try: - # python2 - import StringIO as StringIO -except ImportError: - # python3 - import io as StringIO + +import io as StringIO import json -import shutil import sys import os + +# pylint: disable-next=deprecated-module from optparse import OptionParser import datetime @@ -33,17 +31,20 @@ from spacewalk.satellite_tools.syncLib import initEMAIL_LOG LOCK = None -log_path = '/var/log/rhn/reposync.log' +log_path = "/var/log/rhn/reposync.log" def systemExit(code, msg=None): "Exit with a code and optional message(s). Saved a few lines of code." - sys.stderr.write(str(msg)+'\n') + sys.stderr.write(str(msg) + "\n") sys.exit(code) + try: from rhn import rhnLockfile from uyuni.common.context_managers import cfg_component + + # pylint: disable-next=ungrouped-imports from spacewalk.common import rhnLog from spacewalk.common.rhnConfig import CFG, initCFG from spacewalk.satellite_tools import reposync @@ -51,8 +52,7 @@ try: except KeyboardInterrupt: systemExit(0, "\nUser interrupted process.") except ImportError: - systemExit(1, "Unable to find code tree.\n" - "Path not correct? " + str(sys.path)) + systemExit(1, "Unable to find code tree.\n" "Path not correct? " + str(sys.path)) def releaseLOCK(): @@ -66,72 +66,189 @@ def main(): # quick check to see if you are a super-user. if os.getuid() != 0: - systemExit(8, 'ERROR: must be root to execute.') - + systemExit(8, "ERROR: must be root to execute.") parser = OptionParser() - parser.add_option('-l', '--list', action='store_true', dest='list', - help='List the custom channels with the associated repositories.') - parser.add_option('-s', '--show-packages', action='store_true', dest='show_packages', - help='List all packages in a specified channel.') - parser.add_option('-u', '--url', action='append', dest='url', - default=[], help='The url of the repository. Can be used multiple times.') - parser.add_option('-c', '--channel', action='append', - dest='channel_label', - help='The label of the channel to sync packages to. Can be used multiple times.') - parser.add_option('-p', '--parent-channel', action='append', - dest='parent_label', default=[], - help='Synchronize the parent channel and all its child channels.') - parser.add_option('-d', '--dry-run', action='store_true', - dest='dry_run', - help='Test run. No sync takes place.') - parser.add_option('--latest', action='store_true', - dest='latest', - help='Sync latest packages only. Use carefully - you might need to fix some dependencies on your own.') - parser.add_option('-g', '--config', action='store', dest='config', - help='Configuration file') - parser.add_option('-t', '--type', action='store', dest='repo_type', - help='Force type of repository ("yum", "uln" and "deb" are supported)') - parser.add_option('-f', '--fail', action='store_true', dest='fail', - default=False, - help="If a package import fails, fail the entire operation") - parser.add_option('-n', '--non-interactive', action='store_true', - dest='noninteractive', default=False, - help="Do not ask anything, use default answers") - parser.add_option('-i', '--include', action='callback', - callback=reposync.set_filter_opt, type='str', nargs=1, - dest='filters', default=[], - help="Comma or space separated list of included packages or package groups.") - parser.add_option('-e', '--exclude', action='callback', - callback=reposync.set_filter_opt, - type='str', nargs=1, dest='filters', default=[], - help="Comma or space separated list of excluded packages or package groups.") - parser.add_option('', '--no-strict', action='store_true',help="do not unlink packages when deleted from repository", dest='no_strict') - parser.add_option('', '--email', action="store_true", help="e-mail a report of what was synced/imported") - parser.add_option('', '--traceback-mail', action="store", - help="alternative email address(es) for sync output (--email option)") - parser.add_option('', '--no-errata', action='store_true', dest='no_errata', - default=False, help="Do not sync errata") - parser.add_option('', '--no-packages', action='store_true', dest='no_packages', - default=False, help="Do not sync packages") - parser.add_option('', '--sync-kickstart', action='store_true', dest='sync_kickstart', - default=False, help="Sync kickstartable tree") - parser.add_option('', '--force-all-errata', action='store_true', dest='force_all_errata', - default=False, help="Process metadata of all errata, not only missing.") - parser.add_option('', '--batch-size', action='store', help="max. batch size for package import (debug only)") - parser.add_option('-Y', '--deep-verify', action='store_true', - dest='deep_verify', default=False, - help='Do not use cached package checksums') - parser.add_option('-v', '--verbose', action='count', - help="Verbose output. Possible to accumulate: -vvv") - (options, args) = parser.parse_args() + parser.add_option( + "-l", + "--list", + action="store_true", + dest="list", + help="List the custom channels with the associated repositories.", + ) + parser.add_option( + "-s", + "--show-packages", + action="store_true", + dest="show_packages", + help="List all packages in a specified channel.", + ) + parser.add_option( + "-u", + "--url", + action="append", + dest="url", + default=[], + help="The url of the repository. Can be used multiple times.", + ) + parser.add_option( + "-c", + "--channel", + action="append", + dest="channel_label", + help="The label of the channel to sync packages to. Can be used multiple times.", + ) + parser.add_option( + "-p", + "--parent-channel", + action="append", + dest="parent_label", + default=[], + help="Synchronize the parent channel and all its child channels.", + ) + parser.add_option( + "-d", + "--dry-run", + action="store_true", + dest="dry_run", + help="Test run. No sync takes place.", + ) + parser.add_option( + "--latest", + action="store_true", + dest="latest", + help="Sync latest packages only. Use carefully - you might need to fix some dependencies on your own.", + ) + parser.add_option( + "-g", "--config", action="store", dest="config", help="Configuration file" + ) + parser.add_option( + "-t", + "--type", + action="store", + dest="repo_type", + help='Force type of repository ("yum", "uln" and "deb" are supported)', + ) + parser.add_option( + "-f", + "--fail", + action="store_true", + dest="fail", + default=False, + help="If a package import fails, fail the entire operation", + ) + parser.add_option( + "-n", + "--non-interactive", + action="store_true", + dest="noninteractive", + default=False, + help="Do not ask anything, use default answers", + ) + parser.add_option( + "-i", + "--include", + action="callback", + callback=reposync.set_filter_opt, + type="str", + nargs=1, + dest="filters", + default=[], + help="Comma or space separated list of included packages or package groups.", + ) + parser.add_option( + "-e", + "--exclude", + action="callback", + callback=reposync.set_filter_opt, + type="str", + nargs=1, + dest="filters", + default=[], + help="Comma or space separated list of excluded packages or package groups.", + ) + parser.add_option( + "", + "--no-strict", + action="store_true", + help="do not unlink packages when deleted from repository", + dest="no_strict", + ) + parser.add_option( + "", + "--email", + action="store_true", + help="e-mail a report of what was synced/imported", + ) + parser.add_option( + "", + "--traceback-mail", + action="store", + help="alternative email address(es) for sync output (--email option)", + ) + parser.add_option( + "", + "--no-errata", + action="store_true", + dest="no_errata", + default=False, + help="Do not sync errata", + ) + parser.add_option( + "", + "--no-packages", + action="store_true", + dest="no_packages", + default=False, + help="Do not sync packages", + ) + parser.add_option( + "", + "--sync-kickstart", + action="store_true", + dest="sync_kickstart", + default=False, + help="Sync kickstartable tree", + ) + parser.add_option( + "", + "--force-all-errata", + action="store_true", + dest="force_all_errata", + default=False, + help="Process metadata of all errata, not only missing.", + ) + parser.add_option( + "", + "--batch-size", + action="store", + help="max. batch size for package import (debug only)", + ) + parser.add_option( + "-Y", + "--deep-verify", + action="store_true", + dest="deep_verify", + default=False, + help="Do not use cached package checksums", + ) + parser.add_option( + "-v", + "--verbose", + action="count", + help="Verbose output. Possible to accumulate: -vvv", + ) + (options, _) = parser.parse_args() global LOCK try: - LOCK = rhnLockfile.Lockfile('/run/spacewalk-repo-sync.pid') + LOCK = rhnLockfile.Lockfile("/run/spacewalk-repo-sync.pid") except rhnLockfile.LockfileLockedException: - systemExit(1, "ERROR: attempting to run more than one instance of " - "spacewalk-repo-sync Exiting.") + systemExit( + 1, + "ERROR: attempting to run more than one instance of " + "spacewalk-repo-sync Exiting.", + ) log_level = options.verbose if log_level is None: @@ -142,21 +259,22 @@ def main(): # Apparently we need to call initCFG and have CFG available # to prevent some errors accessing CFG later on during package # import. - initCFG('server.satellite') - CFG.set('DEBUG', log_level) + initCFG("server.satellite") + CFG.set("DEBUG", log_level) CFG.set("TRACEBACK_MAIL", options.traceback_mail or CFG.TRACEBACK_MAIL) if options.email: initEMAIL_LOG() rhnLog.initLOG(log_path, log_level) + # pylint: disable-next=consider-using-f-string log2disk(0, "Command: %s" % str(sys.argv)) - l_params=["no_errata", "sync_kickstart", "fail", "no-strict"] - d_chan_repo=reposync.getChannelRepo() - l_ch_custom=reposync.getCustomChannels() - d_parent_child=reposync.getParentsChilds() - d_ch_repo_sync={} - l_no_ch_repo_sync=[] + l_params = ["no_errata", "sync_kickstart", "fail", "no-strict"] + d_chan_repo = reposync.getChannelRepo() + l_ch_custom = reposync.getCustomChannels() + d_parent_child = reposync.getParentsChilds() + d_ch_repo_sync = {} + l_no_ch_repo_sync = [] if options.list: log(0, "======================================") @@ -164,8 +282,10 @@ def main(): log(0, "======================================") for ch in list(set(l_ch_custom) & set(d_chan_repo)): for repo in d_chan_repo[ch]: - log(0, "%s | %s" %(ch,repo)) - for ch in list(set(l_ch_custom)-set(d_chan_repo)): + # pylint: disable-next=consider-using-f-string + log(0, "%s | %s" % (ch, repo)) + for ch in list(set(l_ch_custom) - set(d_chan_repo)): + # pylint: disable-next=consider-using-f-string log(0, "%s | No repository set" % ch) return 0 @@ -174,49 +294,60 @@ def main(): if options.config: try: + # pylint: disable-next=unspecified-encoding config_file = open(options.config).read() # strip all whitespace - config_file = re.sub(r'\s', '', config_file) + config_file = re.sub(r"\s", "", config_file) config = json.load(StringIO.StringIO(config_file)) + # pylint: disable-next=broad-exception-caught except Exception as e: - systemExit(1, "Configuration file is invalid, please check syntax. Error [%s]" % e ) + systemExit( + 1, + # pylint: disable-next=consider-using-f-string + "Configuration file is invalid, please check syntax. Error [%s]" % e, + ) for key in l_params: if key in config and not getattr(options, key): setattr(options, key, config[key]) # Channels - if 'channel' in config: - for ch,repo in config['channel'].items(): - if not isinstance(repo, list): + if "channel" in config: + for ch, repo in config["channel"].items(): + if not isinstance(repo, list): systemExit( 1, + # pylint: disable-next=consider-using-f-string "Configuration file is invalid, " - "{0}'s value needs to be a list.".format(ch) + "{0}'s value needs to be a list.".format(ch), ) - d_ch_repo_sync[ch]=repo + d_ch_repo_sync[ch] = repo - if 'parent_channel' in config: - options.parent_label+=config['parent_channel'] + if "parent_channel" in config: + options.parent_label += config["parent_channel"] - if options.channel_label and len(options.channel_label)>0: + if options.channel_label and len(options.channel_label) > 0: for channel in options.channel_label: - d_ch_repo_sync[channel]=options.url + d_ch_repo_sync[channel] = options.url if options.parent_label: for pch in options.parent_label: if pch in d_parent_child: - for ch in [pch]+d_parent_child[pch]: + for ch in [pch] + d_parent_child[pch]: if ch in l_ch_custom and ch not in d_ch_repo_sync: - d_ch_repo_sync[ch]=[] + d_ch_repo_sync[ch] = [] else: + # pylint: disable-next=consider-using-f-string systemExit(1, "Channel %s is not custom base channel." % pch) + # pylint: disable-next=consider-using-dict-items for ch in d_ch_repo_sync: if ch not in l_ch_custom: + # pylint: disable-next=consider-using-f-string systemExit(1, "Channel %s is not custom or does not exist." % ch) if not d_ch_repo_sync[ch] and not ch in d_chan_repo: + # pylint: disable-next=consider-using-f-string log(0, "Channel %s Channel has no URL associated, skipping sync" % ch) l_no_ch_repo_sync.append(ch) @@ -228,17 +359,20 @@ def main(): log(0, "| Channel Label | Repository |") log(0, "======================================") - for ch,repo in list(d_ch_repo_sync.items()): + for ch, repo in list(d_ch_repo_sync.items()): if repo: - log(0, " %s : %s" % (ch,", ".join(repo))) + # pylint: disable-next=consider-using-f-string + log(0, " %s : %s" % (ch, ", ".join(repo))) else: - log(0, " %s : %s" % (ch,", ".join(d_chan_repo[ch]))) + # pylint: disable-next=consider-using-f-string + log(0, " %s : %s" % (ch, ", ".join(d_chan_repo[ch]))) log(0, "======================================") log(0, "| Parameters |") log(0, "======================================") - for key in l_params: - log(0, " %s: %s" % (key,str(getattr(options, key)))) + for key in l_params: + # pylint: disable-next=consider-using-f-string + log(0, " %s: %s" % (key, str(getattr(options, key)))) return 0 if options.batch_size: @@ -247,33 +381,43 @@ def main(): if batch_size <= 0: raise ValueError() except ValueError: + # pylint: disable-next=consider-using-f-string systemExit(1, "Invalid batch size: %s" % options.batch_size) reposync.clear_ssl_cache() total_time = datetime.timedelta() ret_code = 0 - for ch,repo in list(d_ch_repo_sync.items()): + for ch, repo in list(d_ch_repo_sync.items()): log(0, "======================================") + # pylint: disable-next=consider-using-f-string log(0, "| Channel: %s" % ch) log(0, "======================================") log(0, "Sync of channel started.") - log2disk(0, "Please check 'reposync/%s.log' for sync log of this channel." % ch, notimeYN=True) - sync = reposync.RepoSync(channel_label=ch, - repo_type=options.repo_type, - url=repo, - fail=options.fail, - strict= not options.no_strict, - noninteractive=options.noninteractive, - filters=options.filters, - deep_verify=options.deep_verify, - no_errata=options.no_errata, - no_packages=options.no_packages, - sync_kickstart=options.sync_kickstart, - latest=options.latest, - log_level=log_level, - force_all_errata=options.force_all_errata, show_packages_only=options.show_packages) + log2disk( + 0, + # pylint: disable-next=consider-using-f-string + "Please check 'reposync/%s.log' for sync log of this channel." % ch, + notimeYN=True, + ) + sync = reposync.RepoSync( + channel_label=ch, + repo_type=options.repo_type, + url=repo, + fail=options.fail, + strict=not options.no_strict, + noninteractive=options.noninteractive, + filters=options.filters, + deep_verify=options.deep_verify, + no_errata=options.no_errata, + no_packages=options.no_packages, + sync_kickstart=options.sync_kickstart, + latest=options.latest, + log_level=log_level, + force_all_errata=options.force_all_errata, + show_packages_only=options.show_packages, + ) if options.batch_size: sync.set_import_batch_size(options.batch_size) elapsed_time, channel_ret_code = sync.sync() @@ -284,14 +428,15 @@ def main(): rhnLog.initLOG(log_path, log_level) log2disk(0, "Sync of channel completed.") - log(0, "Total time: %s" % str(total_time).split('.')[0]) + # pylint: disable-next=consider-using-f-string,use-maxsplit-arg + log(0, "Total time: %s" % str(total_time).split(".")[0]) if options.email: reposync.send_mail() releaseLOCK() return ret_code -if __name__ == '__main__': +if __name__ == "__main__": try: sys.exit(abs(main() or 0)) except KeyboardInterrupt: