[Reportbug-commits] [SCM] Reportbug - reports bugs in the Debian distribution branch, master, updated. 4.9-4-g2e4725d

Sandro Tosi morph at debian.org
Wed Dec 16 19:50:06 UTC 2009


The following commit has been merged in the master branch:
commit c2d6fc2c1b776fa39bec4c1be230b0aa638edce0
Author: Sandro Tosi <morph at debian.org>
Date:   Tue Dec 15 22:39:43 2009 +0100

    use optparse instead of getopt to parse CLI options and arguments; Closes: #559677

diff --git a/bin/querybts b/bin/querybts
index 4320379..3a70938 100755
--- a/bin/querybts
+++ b/bin/querybts
@@ -27,7 +27,7 @@
 
 import sys
 import os
-import getopt
+import optparse
 import re
 
 from reportbug import utils
@@ -47,148 +47,120 @@ from reportbug import VERSION_NUMBER
 
 VERSION = "querybts %s" % VERSION_NUMBER
 
-USAGE = ("querybts - Examine the state of a debbugs server.\n\n"
-         "Usage: querybts [options] {<package> | <report number> [report2] ...}\n"
-         "Supported options (see man page for long forms):\n"
-         "  -A: Browse archived bugs.\n"
-         "  -b: Display a bugs list for the given package.\n"
-         "  -B: Specify an alternate debbugs BTS. *\n"
-         "  -h: Display this help message.\n"
-         "  -s: Query for source packages rather than binary packages.\n"
-         "  -u: Specify the user interface to  use.\n"
-         "  -v: Show the version number of this program.\n"
-         "  -w: Use a web browser instead of the internal interface.\n"
-         "\nOptions marked * take the word 'help' to list allowed options."
-         )
-
 def main():
-    system = 'debian'
-    archived = False
-    http_proxy = interface = ''
-    use_browser = source = False
-    mirrors = None
-    mbox = False
-    buglist = False
 
+    # default values for cli options
+    defaults = dict(system = 'debian', archived = False,
+                    http_proxy = '', interface = 'text',
+                    use_browser = False, source = False,
+                    mirrors = None, mbox = False,  buglist = False)
+
+    # parse config file to update default options
     args = utils.parse_config_files()
     for option, arg in args.items():
-        if option == 'system':
-            system = arg
-        elif option == 'mirrors':
-            mirrors = arg
-        elif option == 'interface':
-            interface = arg
-        elif option == 'http_proxy':
-            http_proxy = arg
+        if option in ('system', 'mirrors', 'interface', 'http_proxy'):
+            defaults[option] = arg
+
+    # define the cli options parser
+    parser = optparse.OptionParser(
+        description='%prog - Examine the state of a debbugs server.',
+        usage='%prog [options] {<package> | <report number> [report2] ...}',
+        version=VERSION)
+
+    # set the defaults
+    parser.set_defaults(**defaults)
+
+    # add the cli options
+    parser.add_option('-A', '--archive', action='store_true', dest='archived',
+                      help='Browse archived bugs.')
+    parser.add_option('-b', '--buglist', action='store_true', dest='buglist',
+                      help='Display a bugs list for the given package.')
+    parser.add_option('-B', '--bts', dest='system',
+                      help='Specify an alternate debbugs BTS; available values:  %s ' %
+                      ', '.join([k for k in debianbts.SYSTEMS if debianbts.SYSTEMS[k].get('btsroot')]))
+    parser.add_option('-m', '--mbox', action='store_true', dest='mbox',
+                      help='generate mbox')
+    parser.add_option('--proxy', '--http_proxy', dest='http_proxy',
+                      help='define the proxy to use')
+    parser.add_option('-s', '--source', action='store_true', dest='source',
+                      help='Query for source packages rather than binary packages.')
+    parser.add_option('-u', '--ui', '--interface', dest='interface',
+                      help='Specify the user interface to use; available values: %s ' % ', '.join(AVAILABLE_UIS.keys()))
+    parser.add_option('-w', '--web', action='store_true', dest='use_browser',
+                      help='Use a web browser instead of the internal interface.')
+
+
+    # parse cli options
+    (options, args) = parser.parse_args()
+
+    # check options for consistency
+
+    # interface must be one of those supported
+    if options.interface not in AVAILABLE_UIS:
+        parser.error('Allowed arguments to --ui: \n' +
+                     '\n'.join(['  %s  (%s)' % (key, value) for key, value in AVAILABLE_UIS.iteritems()]))
+    else:
+        # prepare the UI and import it
+        global ui, ui_mode
+        iface = '%s_ui' % options.interface
+        try:
+            lib_package = __import__('reportbug.ui', fromlist=[iface])
+            ui = getattr(lib_package, iface)
+            ui_mode = options.interface
+        except UINotImportable, msg:
+            ui.long_message('*** Unable to import %s interface: %s '
+                            'Falling back to %s interface.\n',
+                            options.interface, msg, ui_mode)
+            print
 
-    try:
-        (opts, args) = getopt.getopt(
-            sys.argv[1:], 'ABu:bhlmsvw', ['help', 'version',
-                                        'bts=', 'web', 'mbox',
-                                        'archive', 'source',
-                                        'http_proxy=', 'proxy=',
-                                        'ui=', 'interface=',
-                                        'buglist'])
-    except getopt.error, msg:
-        print msg
-        sys.exit(1)
-
-    for option, arg in opts:
-        if option in ('-h', '--help'):
-            print USAGE
-            return
-        elif option in ('-v', '--version'):
-            print VERSION
-            return
-        elif option in ('--proxy', '--http_proxy'):
-            http_proxy = arg
-        elif option in ('-m', '--mbox'):
-            mbox = True
-        elif option in ('-b', '--buglist'):
-            buglist = True
-        elif option in ('--archive', '-A'):
-            archived = True
-        elif option in ('-s', '--source'):
-            source = True
-        elif option in ('-u', '--ui', '--interface'):
-            if arg in AVAILABLE_UIS.keys():
-                interface = arg
-            elif arg == 'help':
-                print 'Allowed arguments to --ui:\n'
-                for k in AVAILABLE_UIS.keys(): print ' %s: %s' % (k, AVAILABLE_UIS[k])
-                sys.exit(0)
-            else:
-                print "Ignoring unknown user interface %s\n" % arg
-        elif option in ('-w', '--web'):
-            use_browser = True
-        elif option in ('-B', '--bts'):
-            if arg in debianbts.SYSTEMS.keys():
-                if debianbts.SYSTEMS[arg].get('btsroot'):
-                    system = arg
-                else:
-                    print "Queries not supported for %s BTS." % arg
-                    return
-            elif arg == 'help':
-                print 'Permitted arguments to --bts:'
-                names = debianbts.SYSTEMS.keys()
-                names.sort()
-                for bsys in names:
-                    if debianbts.SYSTEMS[bsys].get('btsroot'):
-                        print ' %-11.11s %s' % \
-                              (bsys, debianbts.SYSTEMS[bsys]['name'])
-                return
-            else:
-                print "Ignoring unknown BTS server %s." % arg
+    # initialize the selected UI
+    ui.initialize ()
+
+    # system must be one of those supported
+    if options.system not in [k for k in debianbts.SYSTEMS if debianbts.SYSTEMS[k].get('btsroot')]:
+        parser.error('Allowed arguments to --bts: \n' +
+                     '\n'.join(['  %s  (%s)' % (k, debianbts.SYSTEMS[k]['name']) for k in debianbts.SYSTEMS if debianbts.SYSTEMS[k].get('btsroot')]))
+    else:
+        # set the system info to those of the one selected
+        sysinfo = debianbts.SYSTEMS[options.system]
 
-    sysinfo = debianbts.SYSTEMS[system]
+    # there should be at least one argument
     if len(args) == 0:
-        print "Please specify a package or one or more bug numbers."
-        print "Note: most shells consider # a comment character; however, a"
-        print "leading # is not needed to specify a bug by number."
-        sys.exit(1)
-
-    if use_browser:
-        if use_browser and buglist:
-            print "--web and --buglist can't work together, exiting."
-            sys.exit(1)
+        parser.error('Please specify a package or one or more bug numbers.  ' +
+                     'Note: most shells consider # a comment character; however, a ' +
+                     'leading # is not needed to specify a bug by number.')
+
+    if options.use_browser:
+        if options.buglist:
+            parser.error("--web and --buglist can't work together, exiting.")
+
         package = args[0]
+
         m = re.match('^#?(\d+)$', package)
         if m:
             num = int(m.group(1))
-            url = debianbts.get_report_url(system, num, mirrors, archived)
+            url = debianbts.get_report_url(options.system, num, options.mirrors, options.archived)
         else:
-            url = debianbts.get_package_url(system, package, mirrors, source, archived)
+            url = debianbts.get_package_url(options.system, package, options.mirrors, options.source, options.archived)
+
+        # launch the browser and exit
         urlutils.launch_browser(url)
-        return
+        sys.exit()
 
-    if interface:
-        global ui, ui_mode
-        iface = '%(interface)s_ui' % vars()
-        try:
-            lib_package = __import__('reportbug.ui', fromlist=[iface])
-            ui = getattr(lib_package, iface)
-            ui_mode = interface
-        except UINotImportable, msg:
-            ui.long_message('*** Unable to import %s interface: %s '
-                            'Falling back to %s interface.\n',
-                            interface, msg, ui_mode)
-            print
-    ui.initialize ()
+    if options.mbox:
+        if options.buglist:
+            parser.error("--mbox and --buglist can't work together, exiting.")
 
-    if mbox:
-        if mbox and buglist:
-            print "--mbox and --buglist can't work together, exiting."
-            sys.exit(1)
         for bugnum in args:
             package = bugnum
             m = re.match('^#?(\d+)$', bugnum)
             if not m:
                 mboxbuglist = []
-                mboxbuglist = ui.handle_bts_query(package, system, mirrors, http_proxy,
-                                    queryonly=True, title=VERSION, archived=archived,
-                                    source=source, mbox=mbox)
+                mboxbuglist = ui.handle_bts_query(package, options.system, options.mirrors, options.http_proxy,
+                                    queryonly=True, title=VERSION, archived=options.archived,
+                                    source=options.source, mbox=options.mbox)
                 for num in mboxbuglist:
-                    url = debianbts.get_report_url(system, num, archived, mbox=True)
+                    url = debianbts.get_report_url(options.system, num, options.archived, mbox=True)
                     try:
                         report = urlutils.open_url(url)
                         sys.stdout.write(report.read())
@@ -196,7 +168,7 @@ def main():
                         print >> sys.stderr, "Error while accessing mbox report (%s)." % ex
             else:
                 num = int(m.group(1))
-                url = debianbts.get_report_url(system, num, archived, mbox=True)
+                url = debianbts.get_report_url(options.system, num, options.archived, mbox=True)
                 try:
                     report = urlutils.open_url(url)
                     sys.stdout.write(report.read())
@@ -222,13 +194,13 @@ def main():
             match = reportre.match(package)
             if match:
                 report = int(match.group(1))
-                return ui.show_report(report, system, mirrors,
-                                      http_proxy, queryonly=True,
+                return ui.show_report(report, options.system, options.mirrors,
+                                      options.http_proxy, queryonly=True,
                                       title=VERSION,
-                                      archived=archived)
-        ui.handle_bts_query(package, system, mirrors, http_proxy,
-                            queryonly=True, title=VERSION, archived=archived,
-                            source=source, buglist=buglist)
+                                      archived=options.archived)
+        ui.handle_bts_query(package, options.system, options.mirrors, options.http_proxy,
+                            queryonly=True, title=VERSION, archived=options.archived,
+                            source=options.source, buglist=options.buglist)
     except NoPackage:
         ui.long_message('Package appears not to exist in the BTS.\n')
     except NoBugs:
diff --git a/debian/changelog b/debian/changelog
index 87a4222..59979ab 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -3,8 +3,11 @@ reportbug (4.10~WIP) UNRELEASED; urgency=low
   * conf/reportbug.conf, man/reportbug.conf.5
     - added 'keyid' documentation; thanks to gregor herrmann for the report and
       to Carl Chenet for the partial patch; Closes: #558364
+  * bin/querybts
+    - use optparse instead of getopt to parse CLI options and arguments;
+      Closes: #559677
 
- -- Sandro Tosi <morph at debian.org>  Sun, 06 Dec 2009 10:23:00 +0100
+ -- Sandro Tosi <morph at debian.org>  Tue, 15 Dec 2009 22:37:29 +0100
 
 reportbug (4.9) unstable; urgency=low
 

-- 
Reportbug - reports bugs in the Debian distribution



More information about the Reportbug-commits mailing list