~roignac/apport/bugpattern-810182

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
# Author: Matt Zimmerman <mdz@ubuntu.com>
# Copyright (C) 2011 Canonical, Ltd.
# License: GPLv3
#
# Update the bugpattern-needed / bugpattern-written tags for bugs for which
# patterns exist

from launchpadlib.launchpad import Launchpad

import xml.dom.minidom

import argparse
import sys

BUGPATTERNS = 'bugpatterns.xml'

class BugDatabase:
    def __init__(self):
        self.launchpad = None
        self.ubuntu = None

    def connect(self, authenticated=False):
        progname = 'ubuntu-bugpatterns/update-tags'
        root = 'staging'

        if authenticated:
            self.launchpad = Launchpad.login_with(progname, root)
        else:
            self.launchpad = Launchpad.login_anonymously(progname, root)

        self.ubuntu = self.launchpad.distributions['ubuntu']

# Launchpad makes this harder than this unfortunately :-(
#    def bugs_with_tag(self, tag):
#        bugs = set()
#        for task in self.ubuntu.searchTasks(tags=tag):
#            bugs.add(task.bug.id)
#        return bugs

    def bug_has_tag(self, bug, tag):
        return tag in self.launchpad.bugs[bug].tags

    def bug_add_tag(self, bug, tag):
        bug = self.launchpad.bugs[bug]
        new_tags = bug.tags[:]
        new_tags.append(tag)
        bug.tags = new_tags
        bug.lp_save()

    def bug_remove_tag(self, bug, tag):
        bug = self.launchpad.bugs[bug]
        new_tags = bug.tags[:]
        new_tags.remove(tag)
        bug.tags = new_tags
        bug.lp_save()

def urls_from_dom(dom):
    urls = set()

    for pattern in dom.getElementsByTagName('pattern'):
        if not pattern.attributes.has_key('url'): continue

        url = pattern.attributes['url'].nodeValue

        urls.add(url)

    return urls
        
def bug_numbers_from_urls(urls):
    bug_numbers = set()
    for url in urls:
        if not 'launchpad.net/bugs/' in url: continue

        bug_number = int(url.split('/')[-1])
        bug_numbers.add(bug_number)
    return bug_numbers

def bug_to_url(bug):
    return 'http://launchpad.net/bugs/%d' % bug

def bugs_to_string(bugs):
    '\n'.join(map(bug_to_url, bugs))

def main():
    parser = argparse.ArgumentParser(description='Fix up bugpattern-{needed,written} tags')
    parser.add_argument("--file", help="File to read bugpatterns from", dest="file", action="store")
    parser.add_argument("--dry-run", help="Dry run (make no changes)", dest="dry_run", action="store_true")
    parser.add_argument("-r", help="Bazaar revisionspec to compare against when checking recent patterns (ignored for --all-patterns)", dest="revision", default='last:2')

    args = parser.parse_args()

    dom = xml.dom.minidom.parseString(open(BUGPATTERNS).read())
    urls = urls_from_dom(dom)

    bugs_with_patterns = bug_numbers_from_urls(urls)
    if not bugs_with_patterns:
        print "No patterns found"
        return

    print "Found bug patterns for %d bugs" % len(bugs_with_patterns)

    db = BugDatabase()
    db.connect(authenticated=not args.dry_run)

    changes = set()
    for bug in bugs_with_patterns:
        if db.bug_has_tag(bug, 'bugpattern-needed'):
            # remove the bugpattern-needed tag
            changes.add((bug, 'bugpattern-needed', False))
        if not db.bug_has_tag(bug, 'bugpattern-written'):
            # add the bugpattern-written tag
            changes.add((bug, 'bugpattern-written', True))

    if changes:
        for bug, tag, state in changes:
            print "%s - %s %s" % (bug_to_url(bug), state and 'add' or 'remove', tag)
            if args.dry_run: continue

            if state:
                db.bug_add_tag(bug, tag)
            else:
                db.bug_remove_tag(bug, tag)
    else:
        print "All good!"

if __name__ == '__main__':
    main()
    sys.exit(0)