From f596ef407f99202d5aa5d59ae323a6516c181389 Mon Sep 17 00:00:00 2001 From: Lukáš Růžička Date: Feb 15 2025 12:28:22 +0000 Subject: Change optparse with argparse The previous version used the optparse module that has been deprecated for some time already and Pylint would constantly warn about it. The PR replaces optparse with argparse while retaining the logic to add searching patterns on the CLI, similarly to other CLI tools, such as ls, etc. Fixes: https://pagure.io/fedora-easy-karma/issue/41 Merges: https://pagure.io/fedora-easy-karma/pull-request/45 Fix crashing when --help called. Fix the placeholders that are used to fill in default values. Rewrite the USAGE variable a little bit in order not to duplicate the usage info. probably wrongly-merged rebase reflow the usage text a bit better extras -> pattern Extras was confusing, pattern is clear (it's also mention in usage the same way). --- diff --git a/fedora-easy-karma.py b/fedora-easy-karma.py index 98b66a9..cbcc711 100755 --- a/fedora-easy-karma.py +++ b/fedora-easy-karma.py @@ -17,6 +17,7 @@ # along with fedora-easy-karma. If not, see . # standard python modules +import argparse import datetime import fnmatch import itertools @@ -31,7 +32,6 @@ from bodhi.client.bindings import BodhiClient, BodhiClientException import colored import dnf import munch -from optparse import OptionParser import requests from textwrap import wrap @@ -274,38 +274,30 @@ class FEK_helper(object): return ("\n%s" % subsequent_indent).join(output) -USAGE = """usage: %prog [options] [pattern, ..] - -You will be asked for every package installed from updates-testing to provide -feedback using karma points. If patterns are provided, you will be only -prompted for updates related to packages or builds that match any of the -patterns. Possible wildcards are *, ?, [seq] and [!seq] as explained at -http://docs.python.org/library/fnmatch.html +USAGE = """ +You will be asked for every package installed from updates-testing to +provide feedback using karma points. If patterns are provided, you will be +only prompted for updates related to packages or builds that match any of +the patterns. Possible wildcards are *, ?, [seq] and [!seq]. More is about +the wildcards is explained at . Possible values in the karma prompt: -1,0 or 1: Assign the respective karma value to the update - i: Ignore the update in the future - Other inputs will skip the update. - -After assigning karma to the update, a comment needs to be provided, otherwise -the update will be skipped. + i: Ignore the update in the future + Other inputs will skip the update. Note: -- on an empty prompt exits the program. -If you use a default comment, '- ' can be used to delete -the default comment to easily enter a custom one. + * - exits the program when used on empty prompt. + * - + deletes the default comment to enter a new one. For further documentation, please visit: https://fedoraproject.org/wiki/Fedora_Easy_Karma -Copyright 2010-2017 Till Maas and others -fedora-easy-karma is distributed under the terms of the GNU General Public -License -The source is available at: -https://pagure.io/fedora-easy-karma +Copyright 2010-2025 Till Maas and others. Fedora-easy-karma is distributed under +the terms of the GNU General Public License. The source is available at +. """ - class PkgHelper(object): def __init__(self): self.my = dnf.Base() @@ -332,175 +324,152 @@ class FedoraEasyKarma(object): width=80, extra_newline=False) - parser = OptionParser(usage=usage) - parser.add_option("", - "--bodhi-cached", dest="bodhi_cached", + parser = argparse.ArgumentParser(description=usage, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--bodhi-cached", dest="bodhi_cached", help="Use cached bodhi query", action="store_true", default=False) - parser.add_option("", - "--bodhi-update-cache", + parser.add_argument("--bodhi-update-cache", dest="bodhi_update_cache", help="Update bodhi query cache", action="store_true", default=False) - parser.add_option("", - "--critpath-only", + parser.add_argument("--critpath-only", dest="critpath_only", help="Only consider unapproved critpath updates", action="store_true", default=False) - parser.add_option("", - "--datadir", + parser.add_argument("--datadir", dest="datadir", help="Directory to store cache or ignore data, " - "default: %default", + "default: %(default)s", default=f"{config_dir}/fedora-easy-karma") - parser.add_option("", - "--debug", + parser.add_argument("--debug", dest="debug", help="Enable debug output", action="store_true", default=False) - parser.add_option("", - "--default-comment", + parser.add_argument("--default-comment", dest="default_comment", - help="Default comment to use, default: %default", + help="Default comment to use, default: %(default)s", default="", metavar="COMMENT") - parser.add_option("", - "--default-karma", + parser.add_argument("--default-karma", dest="default_karma", - help="Default karma to use, default: %default", + help="Default karma to use, default: %(default)s", default="", metavar="KARMA") - parser.add_option("", - "--no-color", + parser.add_argument("--no-color", dest="no_color", help="Do not colorize output", action="store_true", default=False) - parser.add_option("", - "--no-ignore-own", + parser.add_argument("--no-ignore-own", dest="ignore_own", help="Do not ignore own updates.", action="store_false", default=True) - parser.add_option("", - "--include-commented", + parser.add_argument("--include-commented", dest="include_commented", help="Also ask for more comments on updates that " "already got a comment from you, this is " "enabled if patterns are provided", action="store_true", default=False) - parser.add_option("", - "--include-ignored", + parser.add_argument("--include-ignored", dest="include_ignored", help="Also ask for comments on updates that have " "been ignored previously.", action="store_true", default=False) - parser.add_option("", - "--installed-max-days", + parser.add_argument("--installed-max-days", dest="installed_max_days", help="Only check packages installed within the last " - "XX days, default: %default", + "XX days, default: %(default)d", metavar="DAYS", default=28, - type="int") - parser.add_option("", - "--installed-min-days", + type=int) + parser.add_argument("--installed-min-days", dest="installed_min_days", help="Only check packages installed for at least " - "XX days, default: %default", + "XX days, default: %(default)d", metavar="DAYS", default=0, - type="int") - parser.add_option("", - "--ipdb", + type=int) + parser.add_argument("--ipdb", dest="ipdb", help="Launch ipbd for debugging", action="store_true", default=False) - parser.add_option("", - "--list-rpms-only", + parser.add_argument("--list-rpms-only", dest="list_rpms_only", help="Only list affected rpms", action="store_true", default=False) - parser.add_option("", - "--pages", + parser.add_argument("--pages", dest="pages", help="Clear terminal before each new presented update", action="store_true", default=False) - parser.add_option("", - "--product", + parser.add_argument("--product", dest="product", help="product to query Bodhi for, 'F' for Fedora, " - "'EL-' for EPEL, default: %default", + "'EL-' for EPEL, default: %(default)s", default="F") - parser.add_option("", - "--releasever", + parser.add_argument("--releasever", dest="releasever", help="releasever to query Bodhi for, " "default: releasever from dnf", default=None) - parser.add_option("", - "--retries", + parser.add_argument("--retries", dest="retries", help="Number if retries when submitting a comment " - "in case of an error, default: %default", + "in case of an error, default: %(default)d", default=3, - type="int") - parser.add_option("", - "--skip-bodhi-comments", + type=int) + parser.add_argument("--skip-bodhi-comments", dest="skip_bodhi_comments", help="Do not show comments created by bodhi", action="store_true", default=False) - parser.add_option("", - "--wrap-bugs", + parser.add_argument("--wrap-bugs", dest="wrap_bugs", help="Apply line-wrapping to bugs", action="store_true", default=False) - parser.add_option("", - "--wrap-rpms", + parser.add_argument("--wrap-rpms", dest="wrap_rpms", help="Apply line-wrapping to list of installed rpms", action="store_true", default=False) - parser.add_option("", - "--wrap-width", + parser.add_argument("--wrap-width", dest="wrap_width", help="Width to use for line wrapping of updates, " - "default: %default", + "default: %(default)d", default=80, - type="int") - parser.add_option("", - "--bodhi-request-limit", + type=int) + parser.add_argument("--bodhi-request-limit", dest="bodhi_request_limit", help="Maximum number of updates to request at " - "once from Bodhi, default: %default", + "once from Bodhi, default: %(default)d", default=25, - type="int") - parser.add_option("", - "--oraculum-endpoint", + type=int) + parser.add_argument("--oraculum-endpoint", dest="oraculum_endpoint", help="Specify URL for oraculum instance", default="https://packager-dashboard.fedoraproject.org/api/v1/libkarma/") - parser.add_option("", - "--no-cache", + parser.add_argument("--no-cache", dest="oraculum_disabled", help="Bypass oraculum and force fetch from bodhi", action="store_true", default=False) + parser.add_argument("pattern", + help="Show only updates matching one or more package name patterns", + nargs="*") - (self.options, args) = parser.parse_args() + self.options = parser.parse_args() - if args: + if self.options.pattern: self.options.include_commented = True if self.options.debug: @@ -689,13 +658,13 @@ class FedoraEasyKarma(object): b in installed_testing_builds]) ) - if args: + if self.options.pattern: installed_pkgs_names = ["%(name)s" % pkg for pkg in installed_pkgs] # remove version and release affected_builds_names = ["-".join(b.split("-")[:-2]) for b in affected_builds] - if not self.match_any(args, [installed_pkgs_names, + if not self.match_any(self.options.pattern, [installed_pkgs_names, affected_builds_names]): continue installed_rpms = [