diff options
author | Zac Medico <zmedico@gentoo.org> | 2009-02-19 06:39:59 +0000 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2009-02-19 06:39:59 +0000 |
commit | 9b98bb606e32f24120775bada3a0a127fbb7e1b1 (patch) | |
tree | 2b447eb645dcd8800e733833a72fe0ad7d814ba7 | |
parent | 8ad412e223f255f7fa473028b49ac7b01cdbe008 (diff) | |
download | portage-9b98bb606e32f24120775bada3a0a127fbb7e1b1.tar.gz portage-9b98bb606e32f24120775bada3a0a127fbb7e1b1.tar.bz2 portage-9b98bb606e32f24120775bada3a0a127fbb7e1b1.zip |
Bug #230483 - Convert glsa-check option parsing to use the optparse module.
This replaces some code which triggers a traceback from 2to3. Thanks to
Ali Polatel <hawking@g.o> for this patch.
svn path=/main/trunk/; revision=12634
-rw-r--r-- | bin/glsa-check | 164 |
1 files changed, 71 insertions, 93 deletions
diff --git a/bin/glsa-check b/bin/glsa-check index 8aea638a5..b7b9b47d9 100644 --- a/bin/glsa-check +++ b/bin/glsa-check @@ -15,111 +15,89 @@ except ImportError: from portage.output import * -from getopt import getopt, GetoptError +from optparse import OptionGroup, OptionParser __program__ = "glsa-check" __author__ = "Marius Mauch <genone@gentoo.org>" __version__ = "1.0" -optionmap = [ -["-l", "--list", "list all unapplied GLSA"], -["-d", "--dump", "--print", "show all information about the given GLSA"], -["-t", "--test", "test if this system is affected by the given GLSA"], -["-p", "--pretend", "show the necessary commands to apply this GLSA"], -["-f", "--fix", "try to auto-apply this GLSA (experimental)"], -["-i", "--inject", "inject the given GLSA into the checkfile"], -["-n", "--nocolor", "disable colors (option)"], -["-e", "--emergelike", "do not use a least-change algorithm (option)"], -["-h", "--help", "show this help message"], -["-V", "--version", "some information about this tool"], -["-v", "--verbose", "print more information (option)"], -["-c", "--cve", "show CAN ids in listing mode (option)"], -["-m", "--mail", "send a mail with the given GLSAs to the administrator"] -] +def cb_version(*args, **kwargs): + """Callback for --version""" + sys.stderr.write("\n"+ __program__ + ", version " + __version__ + "\n") + sys.stderr.write("Author: " + __author__ + "\n") + sys.stderr.write("This program is licensed under the GPL, version 2\n\n") + sys.exit(0) # option parsing -args = [] -params = [] -try: - args, params = getopt(sys.argv[1:], "".join([o[0][1] for o in optionmap]), \ - [x[2:] for x in reduce(lambda x,y: x+y, [z[1:-1] for z in optionmap])]) -# ["dump", "print", "list", "pretend", "fix", "inject", "help", "verbose", "version", "test", "nocolor", "cve", "mail"]) - args = [a for a,b in args] - - for option in ["--nocolor", "-n"]: - if option in args: - nocolor() - args.remove(option) - - verbose = False - for option in ["--verbose", "-v"]: - if option in args: - verbose = True - args.remove(option) - - list_cve = False - for option in ["--cve", "-c"]: - if option in args: - list_cve = True - args.remove(option) - - least_change = True - for option in ["--emergelike", "-e"]: - if option in args: - least_change = False - args.remove(option) - - # sanity checking - if len(args) <= 0: - sys.stderr.write("no option given: what should I do ?\n") - mode="help" - elif len(args) > 1: - sys.stderr.write("please use only one command per call\n") - mode = "help" - else: - # in what mode are we ? - args = args[0] - for m in optionmap: - if args in [o for o in m[:-1]]: - mode = m[1][2:] - -except GetoptError, e: - sys.stderr.write("unknown option given: ") - sys.stderr.write(str(e)+"\n") - mode = "help" - -# we need a set of glsa for most operation modes -if len(params) <= 0 and mode in ["fix", "test", "pretend", "dump", "inject", "mail"]: +parser = OptionParser(usage="%prog <option> [glsa-list]", + version="%prog "+ __version__) +parser.epilog = "glsa-list can contain an arbitrary number of GLSA ids," \ + " filenames containing GLSAs or the special identifiers" \ + " 'all', 'new' and 'affected'" + +modes = OptionGroup(parser, "Modes") +modes.add_option("-l", "--list", action="store_const", + const="list", dest="mode", + help="List all unapplied GLSA") +modes.add_option("-d", "--dump", action="store_const", + const="dump", dest="mode", + help="Show all information about the given GLSA") +modes.add_option("", "--print", action="store_const", + const="dump", dest="mode", + help="Alias for --dump") +modes.add_option("-t", "--test", action="store_const", + const="test", dest="mode", + help="Test if this system is affected by the given GLSA") +modes.add_option("-p", "--pretend", action="store_const", + const="pretend", dest="mode", + help="Show the necessary commands to apply this GLSA") +modes.add_option("-f", "--fix", action="store_const", + const="fix", dest="mode", + help="Try to auto-apply this GLSA (experimental)") +modes.add_option("-i", "--inject", action="store_const", dest="mode", + help="Inject the given GLSA into the checkfile") +modes.add_option("-m", "--mail", action="store_const", + const="mail", dest="mode", + help="Send a mail with the given GLSAs to the administrator") +parser.add_option_group(modes) + +parser.remove_option("--version") +parser.add_option("-V", "--version", action="callback", + callback=cb_version, help="Some information about this tool") +parser.add_option("-v", "--verbose", action="store_true", dest="verbose", + help="Print more information") +parser.add_option("-n", "--nocolor", action="callback", + callback=lambda *args, **kwargs: nocolor(), + help="Disable colors") +parser.add_option("-e", "--emergelike", action="store_false", dest="least_change", + help="Do not use a least-change algorithm") +parser.add_option("-c", "--cve", action="store_true", dest="list_cve", + help="Show CAN ids in listing mode") + +options, params = parser.parse_args() + +mode = options.mode +least_change = options.least_change +list_cve = options.list_cve +verbose = options.verbose + +# Sanity checking +if mode is None: + sys.stderr.write("No mode given: what should I do?\n") + parser.print_help() + sys.exit(1) +elif mode != "list" and not params: sys.stderr.write("\nno GLSA given, so we'll do nothing for now. \n") sys.stderr.write("If you want to run on all GLSA please tell me so \n") sys.stderr.write("(specify \"all\" as parameter)\n\n") - mode = "help" -elif len(params) <= 0 and mode == "list": - params.append("new") - -# show help message -if mode == "help": - sys.stderr.write("\nSyntax: glsa-check <option> [glsa-list]\n\n") - for m in optionmap: - sys.stderr.write(m[0] + "\t" + m[1] + " \t: " + m[-1] + "\n") - for o in m[2:-1]: - sys.stderr.write("\t" + o + "\n") - sys.stderr.write("\nglsa-list can contain an arbitrary number of GLSA ids, \n") - sys.stderr.write("filenames containing GLSAs or the special identifiers \n") - sys.stderr.write("'all', 'new' and 'affected'\n") + parser.print_help() sys.exit(1) - -# we need root priviledges for write access -if mode in ["fix", "inject"] and os.geteuid() != 0: - sys.stderr.write("\nThis tool needs root access to "+mode+" this GLSA\n\n") +elif mode in ["fix", "inject"] and os.geteuid() != 0: + # we need root priviledges for write access + sys.stderr.write("\nThis tool needs root access to "+options.mode+" this GLSA\n\n") sys.exit(2) - -# show version and copyright information -if mode == "version": - sys.stderr.write("\n"+ __program__ + ", version " + __version__ + "\n") - sys.stderr.write("Author: " + __author__ + "\n") - sys.stderr.write("This program is licensed under the GPL, version 2\n\n") - sys.exit(0) +elif mode == "list" and not params: + params.append("new") # delay this for speed increase from portage.glsa import * |