From 5d1c0550520c0277082be0d243ed4a60f03f0121 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Mon, 28 May 2018 15:09:19 +1000 Subject: [PATCH] Move to argparse I presume optparse was a python2.6 compat thing; but since it's stated in the NEWS file that it's not supported for several years, I think we can switch. Even if someone *was* running on centos6 era, the odds they won't/can't have python-argparse installed anyway are minimal, so it's not like this is a completely hard break. Some minor refactoring is included to reduce a level of nesting. Change-Id: Ib933db547a811d533bdc95b4836354e959c0a6f5 --- bindep/main.py | 94 ++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/bindep/main.py b/bindep/main.py index a1c0130..0091d85 100644 --- a/bindep/main.py +++ b/bindep/main.py @@ -15,8 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import argparse import logging -import optparse import sys import bindep.depends @@ -27,32 +27,37 @@ logging.basicConfig( def main(depends=None): - usage = "Usage: %prog [options] [profile]..." - parser = optparse.OptionParser( - usage=usage, version="%%prog %s" % bindep.version) - parser.add_option( - "-b", "--brief", action="store_true", dest="brief", + parser = argparse.ArgumentParser() + parser.add_argument( + "--brief", "-b", action="store_true", dest="brief", help="List only missing packages one per line.") - parser.add_option( - "-f", "--file", action="store", type="string", dest="filename", - default="", + parser.add_argument( + "--file", "-f", action="store", dest="filename", default="", help="Package list file (default: bindep.txt or " - "other-requirements.txt).") - parser.add_option( + "other-requirements.txt).") + parser.add_argument( "--profiles", action="store_true", help="List the platform and configuration profiles.") - parser.add_option( - "-l", "--list_all", dest="list_all", choices=["newline", "csv"], - type="choice", action="store", + parser.add_argument( + "--list_all", "-l", dest="list_all", action="store", + choices=["newline", "csv"], help="List all dependencies for this platform and profile." " Pass in either 'newline' or 'csv' to specify the format" " of the output.") - opts, args = parser.parse_args() + parser.add_argument( + 'profile', nargs='*', default=["default"], + help="Extra profiles to match when checking for packages.") + + parser.add_argument( + '--version', action='version', version="%%(prog)s %s" % bindep.version) + args = parser.parse_args() + if depends is None: - depends = bindep.depends.get_depends(opts.filename) + depends = bindep.depends.get_depends(args.filename) if not depends: return 1 - if opts.profiles: + + if args.profiles: logging.info("Platform profiles:") for profile in depends.platform_profiles(): logging.info("%s", profile) @@ -60,35 +65,34 @@ def main(depends=None): logging.info("Configuration profiles:") for profile in depends.profiles(): logging.info("%s", profile) - else: - if args: - profiles = args - else: - profiles = ["default"] - profiles = profiles + depends.platform_profiles() - rules = depends.active_rules(profiles) - if opts.list_all: - depends.list_all_packages(rules, opts.list_all) - return 0 - errors = depends.check_rules(rules) - for error in errors: - if error[0] == 'missing': - if opts.brief: - logging.info("%s", "\n".join(error[1])) - else: - logging.info("Missing packages:") - logging.info(" %s", " ".join(error[1])) - if error[0] == 'badversion': - if not opts.brief: - logging.info("Bad versions of installed packages:") - for pkg, constraint, version in error[1]: - logging.info( - " %s version %s does not match %s", - pkg, version, constraint) - if errors: - return 1 - return 0 + return 0 + profiles = args.profile + depends.platform_profiles() + rules = depends.active_rules(profiles) + + if args.list_all: + depends.list_all_packages(rules, args.list_all) + return 0 + + errors = depends.check_rules(rules) + for error in errors: + if error[0] == 'missing': + if args.brief: + logging.info("%s", "\n".join(error[1])) + else: + logging.info("Missing packages:") + logging.info(" %s", " ".join(error[1])) + if error[0] == 'badversion': + if not args.brief: + logging.info("Bad versions of installed packages:") + for pkg, constraint, version in error[1]: + logging.info( + " %s version %s does not match %s", + pkg, version, constraint) + if errors: + return 1 + + return 0 if __name__ == '__main__': sys.exit(main())