Dan Bungert has proposed merging 
~dbungert/apport/+git/ubuntu-bugpatterns:search-bugs-cleanup into 
~ubuntu-bugcontrol/apport/+git/ubuntu-bugpatterns:main.

Commit message:
Several cleanup items on the python code.  My intention is to use these scripts 
both with the apport bugpattern workflow, and with our own scripting, so some 
code cleanups here are a step toward that latter direction.

Easier to read under `git diff -w`.

Requested reviews:
  Ubuntu Bug Control (ubuntu-bugcontrol)

For more details, see:
https://code.launchpad.net/~dbungert/apport/+git/ubuntu-bugpatterns/+merge/481820
-- 
Your team Ubuntu Bug Control is requested to review the proposed merge of 
~dbungert/apport/+git/ubuntu-bugpatterns:search-bugs-cleanup into 
~ubuntu-bugcontrol/apport/+git/ubuntu-bugpatterns:main.
diff --git a/search-bugs b/search-bugs
index f8000e8..3ade05a 100755
--- a/search-bugs
+++ b/search-bugs
@@ -15,7 +15,7 @@ from launchpadlib.launchpad import Launchpad
 from os.path import abspath
 
 
-import optparse
+import argparse
 import os
 import re
 import sys
@@ -66,7 +66,7 @@ def trim_dpkg_log(report):
     if 'DpkgTerminalLog' not in report:
         return
     lines = []
-    trim_re = re.compile('^\(.* ... \d+ .*\)$')
+    trim_re = re.compile(r'^\(.* ... \d+ .*\)$')
     if isinstance(report['DpkgTerminalLog'], bytes):
         report['DpkgTerminalLog'] = report['DpkgTerminalLog']\
                                           .decode('utf-8', errors='ignore')
@@ -78,137 +78,144 @@ def trim_dpkg_log(report):
     report['DpkgTerminalLog'] = '\n'.join(lines)
 
 
-parser = optparse.OptionParser(usage="usage: %prog -p PACKAGE -t TAG(s) "\
-    "[options]")
-parser.add_option("-p", "--package", help="Filter on PACKAGE", dest="package",
-    metavar="PACKAGE")
-parser.add_option("-s", "--status", help="Filter on STATUS", dest="status",
-    metavar="STATUS")
-parser.add_option("-t", "--tags", help="Filter on TAG,TAG", dest="tags",
-    metavar="TAGS")
-parser.add_option("-d", "--dupes", help="Include duplicates in search",
-    dest="dupes", action="store_true")
-parser.add_option("-q", "--quiet", help="Only print bug numbers",
-    dest="quiet", action="store_true")
-parser.add_option("-a", "--all", help="Check all package bugs", dest="all",
-    action="store_true")
-parser.add_option("-k", "--keywords", help="Search for KEYWORDS",
-    dest="keywords", metavar="KEYWORDS")
-parser.add_option("-w", "--within",
-    help="Search for bugs reported within the past X days",
-    dest="within", metavar="WITHIN")
-parser.add_option("-C", "--consolidate", dest="consolidate",
-    help="Mark bugs as duplicate of master bug in pattern",
-    action="store_true")
-
-(opt, args) = parser.parse_args()
-
-# Connect to Launchpad
-lp = connect()
-
-status_list = []
-tag_list = []
-ubuntu = lp.distributions['ubuntu']
-
-valid_status = ['New', 'Incomplete', 'Invalid', "Won't Fix", 'Confirmed',
-                'Triaged', 'In Progress', 'Fix Committed', 'Fix Released',
-                'Unknown']
-
-if not opt.status:
-    status_list = ['New', 'Incomplete', 'Confirmed', 'Triaged',
-                   'In Progress', 'Fix Committed' ]
-elif opt.status:
-    if opt.status not in valid_status:
-        print("Invalid status '%s'. Aborting" % (opt.status), file=sys.stderr)
+def get_args():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-p", "--package", help="Filter on PACKAGE", dest="package",
+        metavar="PACKAGE")
+    parser.add_argument("-s", "--status", help="Filter on STATUS", dest="status",
+        metavar="STATUS")
+    parser.add_argument("-t", "--tags", help="Filter on TAG,TAG", dest="tags",
+        metavar="TAGS")
+    parser.add_argument("-d", "--dupes", help="Include duplicates in search",
+        dest="dupes", action="store_true")
+    parser.add_argument("-q", "--quiet", help="Only print bug numbers",
+        dest="quiet", action="store_true")
+    parser.add_argument("-a", "--all", help="Check all package bugs", dest="all",
+        action="store_true")
+    parser.add_argument("-k", "--keywords", help="Search for KEYWORDS",
+        dest="keywords", metavar="KEYWORDS")
+    parser.add_argument("-w", "--within",
+        help="Search for bugs reported within the past X days",
+        dest="within", metavar="WITHIN")
+    parser.add_argument("-C", "--consolidate", dest="consolidate",
+        help="Mark bugs as duplicate of master bug in pattern",
+        action="store_true")
+    return parser.parse_args()
+
+
+def main():
+    opt = get_args()
+
+    # Connect to Launchpad
+    lp = connect()
+
+    status_list = []
+    tag_list = []
+    ubuntu = lp.distributions['ubuntu']
+
+    valid_status = ['New', 'Incomplete', 'Invalid', "Won't Fix", 'Confirmed',
+                    'Triaged', 'In Progress', 'Fix Committed', 'Fix Released',
+                    'Unknown']
+
+    if not opt.status:
+        status_list = ['New', 'Incomplete', 'Confirmed', 'Triaged',
+                       'In Progress', 'Fix Committed' ]
+    elif opt.status:
+        if opt.status not in valid_status:
+            print("Invalid status '%s'. Aborting" % (opt.status), file=sys.stderr)
+            sys.exit(1)
+        else:
+            status_list.append(opt.status)
+
+    if opt.package == 'ubuntu':
+        package = ubuntu
+    if opt.package != 'ubuntu':
+        package = ubuntu.getSourcePackage(name='%s' % opt.package)
+        if package is None:
+            print('Package %s not found in Ubuntu' % opt.package)
+            sys.exit(1)
+    elif not opt.package:
+        print("A package is required.", file=sys.stderr)
         sys.exit(1)
-    else:
-        status_list.append(opt.status)
-
-if opt.package == 'ubuntu':
-    package = ubuntu
-if opt.package != 'ubuntu':
-    package = ubuntu.getSourcePackage(name='%s' % opt.package)
-    if package is None:
-        print('Package %s not found in Ubuntu' % opt.package)
-        sys.exit(1)
-elif not opt.package:
-    print("A package is required.", file=sys.stderr)
-    sys.exit(1)
-
-if opt.dupes:
-    dupes = False
-elif not opt.dupes:
-    dupes = True
-
-if opt.tags:
-    for tag in opt.tags.split(','):
-        tag_list.append(tag)
-
-if opt.within:
-    period = int(opt.within)
-    today = datetime.utcnow()
-    search_start = today - timedelta(period)
-elif not opt.within:
-    search_start = None
-
-
-search_args = {'tags': tag_list,
-               'tags_combinator': 'All',
-               'order_by': '-datecreated',
-               'status': opt.status,
-               'search_text': opt.keywords,
-               'omit_duplicates': dupes,
-               'created_since': search_start}
-
-tasks = package.searchTasks(**search_args)
-db = get_crashdb(None)
-for task in tasks:
-    # they should be retraced first
-    if 'need-i386-retrace' in task.bug.tags or 'need-amd64-retrace' in task.bug.tags:
-        continue
-    task_number = task.bug.id
-    try:
-        report = db.download(task_number)
-    except AssertionError as error:
-        print("LP: #%s: %s" % (task_number, error))
-        continue
-    except Exception as error:
-        print("LP: #%s: %s" % (task_number, error))
-        continue
-    except AttributeError as error:
-        print("LP: #%s: %s" % (task_number, error))
-        continue
-
-    # trim the dpkg log file
-    trim_dpkg_log(report)
-
-    try:
-        match = report.search_bug_patterns('file://' + abspath('./bugpatterns.xml'))
-    except AssertionError as error:
-        print("%s" % error)
-        continue
-
-    if match and not opt.quiet:
-        # this should handle wiki urls somehow
-        master_number = match.split('/')[-1]
-        master = lp.bugs[master_number]
-        if str(master_number) == str(task_number) and not opt.all:
-            print("Reached master bug LP: #%s" % master_number)
-            break
-        if int(task_number) == int(master_number):
+
+    if opt.dupes:
+        dupes = False
+    elif not opt.dupes:
+        dupes = True
+
+    if opt.tags:
+        for tag in opt.tags.split(','):
+            tag_list.append(tag)
+
+    if opt.within:
+        period = int(opt.within)
+        today = datetime.utcnow()
+        search_start = today - timedelta(period)
+    elif not opt.within:
+        search_start = None
+
+
+    search_args = {'tags': tag_list,
+                   'tags_combinator': 'All',
+                   'order_by': '-datecreated',
+                   'status': opt.status,
+                   'search_text': opt.keywords,
+                   'omit_duplicates': dupes,
+                   'created_since': search_start}
+
+    tasks = package.searchTasks(**search_args)
+    db = get_crashdb(None)
+    for task in tasks:
+        # they should be retraced first
+        if 'need-i386-retrace' in task.bug.tags or 'need-amd64-retrace' in task.bug.tags:
             continue
-        print('LP: #%s (%s, %s): Matched bug pattern: %s with %s dupes' % (task_number,
-            task.status, task.importance, match, master.number_of_duplicates))
-        if opt.consolidate:
-            bug = task.bug
-            if 'bot-stop-nagging' in bug.tags:
-                print('LP: #%s is tagged bot-stop-nagging' % (task_number))
-                continue
-            if bug.duplicate_of:
-                print('LP: #%s is already a duplicate of another bug' % (task_number))
+        task_number = task.bug.id
+        try:
+            report = db.download(task_number)
+        except AssertionError as error:
+            print("LP: #%s: %s" % (task_number, error))
+            continue
+        except Exception as error:
+            print("LP: #%s: %s" % (task_number, error))
+            continue
+        except AttributeError as error:
+            print("LP: #%s: %s" % (task_number, error))
+            continue
+
+        # trim the dpkg log file
+        trim_dpkg_log(report)
+
+        try:
+            match = report.search_bug_patterns('file://' + abspath('./bugpatterns.xml'))
+        except AssertionError as error:
+            print("%s" % error)
+            continue
+
+        if match and not opt.quiet:
+            # this should handle wiki urls somehow
+            master_number = match.split('/')[-1]
+            master = lp.bugs[master_number]
+            if str(master_number) == str(task_number) and not opt.all:
+                print("Reached master bug LP: #%s" % master_number)
                 break
-            elif int(bug.id) == int(master_number):
+            if int(task_number) == int(master_number):
                 continue
-            mark_as_duplicate(master_number, bug)
-    elif match and opt.quiet:
-        print('%s' % (task_number))
+            print('LP: #%s (%s, %s): Matched bug pattern: %s with %s dupes' % (task_number,
+                task.status, task.importance, match, master.number_of_duplicates))
+            if opt.consolidate:
+                bug = task.bug
+                if 'bot-stop-nagging' in bug.tags:
+                    print('LP: #%s is tagged bot-stop-nagging' % (task_number))
+                    continue
+                if bug.duplicate_of:
+                    print('LP: #%s is already a duplicate of another bug' % (task_number))
+                    break
+                elif int(bug.id) == int(master_number):
+                    continue
+                mark_as_duplicate(master_number, bug)
+        elif match and opt.quiet:
+            print('%s' % (task_number))
+
+
+if __name__ == "__main__":
+    main()
_______________________________________________
Mailing list: https://launchpad.net/~ubuntu-bugcontrol
Post to     : ubuntu-bugcontrol@lists.launchpad.net
Unsubscribe : https://launchpad.net/~ubuntu-bugcontrol
More help   : https://help.launchpad.net/ListHelp

Reply via email to