From 9b98bb606e32f24120775bada3a0a127fbb7e1b1 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Thu, 19 Feb 2009 06:39:59 +0000 Subject: [PATCH] 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 for this patch. svn path=/main/trunk/; revision=12634 --- bin/glsa-check | 164 +++++++++++++++++++++---------------------------- 1 file 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 " __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