~juju-qa/lp-release-manager-tools/trunk

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python

from __future__ import print_function

from datetime import datetime
import json
from optparse import OptionParser
import sys

from launchpadlib.launchpad import Launchpad


all_bug_statuses = [
    'Invalid',
    "Won't Fix",
    'Incomplete (with response)',
    'Incomplete (without response)',
    'Incomplete',
    'Opinion',
    'Expired',
    'New',
    'Confirmed',
    'Triaged',
    'In Progress',
    'Fix Committed',
    'Fix Released',
]


public_information_types = [
    'Public',
    'Public Security',
]


private_information_types = [
    'Private',
    'Private Secruity',
    'Proprietary',
    'Embargoed',
]


def get_saved_bug_data(options):
    with open(options.union_file, 'r') as in_file:
        previous_json = in_file.read()
    previous_data = json.loads(previous_json)
    return transform_bug_data(previous_data)


def transform_bug_data(previous_data):
    return dict([(b['bug_id'], b) for b in previous_data['bugs']])


def get_bug_data(target, all_bugs, search_params, private=False):
    information_types = public_information_types
    if private:
        information_types = information_types + private_information_types
    found_tasks = target.searchTasks(**search_params)
    count = 0
    for task in found_tasks:
        json_data = dict(task._wadl_resource.representation)
        bug = task.bug
        json_data[u'bug_id'] = bug.id
        json_data[u'bug_title'] = bug.title
        json_data[u'bug_information_type'] = bug.information_type
        json_data[u'bug_tags'] = bug.tags
        all_bugs[bug.id] = json_data
        count += 1
    return count


def get_targets(lp, project_name):
    project = lp.projects[project_name]
    targets = [s for s in project.series]
    targets.insert(0, project)
    return targets


def get_option_parser():
    """Return the option parser for this program."""
    example = "eg. %prog -t oops -t 404 -s Critical -s High -m 1.1 my-project"
    usage = "usage: %%prog [options] project\n%s" % example
    parser = OptionParser(usage=usage)
    parser.add_option(
        "-o", "--outfile", dest="outfile",
        help="The name of the file to write the json data to.")
    parser.add_option(
        "-u", "--union-file", dest="union_file",
        help="The name of the file to merge the new json data with.")
    parser.add_option(
        "-c", "--changed-since", dest="since",
        help="Match bugs reported changed after iso-date (2012-09-28).")
    parser.add_option(
        "-p", "--private", dest="private", action="store_true",
        help="Match bugs that are private.")
    parser.add_option(
        "-t", "--tags", dest="tags", action="append", type="string",
        help="Match bugs with all of the listed tags.")
    parser.add_option(
        "-m", "--milestone", dest="milestone",
        help="Match bugs targeted to a milestone.")
    parser.add_option(
        "-i", "--importance", dest="importance", action="append",
        type="string", help="Match bugs with all of the listed importances.")
    parser.add_option(
        "-s", "--statuses", dest="statuses", action="append",
        type="string", help="Match bugs with all of the listed statues.")
    parser.add_option(
        "-C", "--credentials", dest="credentials", type="string",
        help="Launchpad credentials file.")
    parser.set_defaults(
        outfile='bug-data.json',
        union_file=None,
        since=None,
        private=False,
        tags=None,
        milestone=None,
        importance=None,
        statuses=[],
        credentials=None,
    )
    return parser


def main():
    argv = sys.argv
    parser = get_option_parser()
    (options, args) = parser.parse_args(args=argv[1:])
    if len(args) < 1:
        print(parser.usage)
        sys.exit(1)
    if not options.statuses:
        options.statuses = all_bug_statuses
    project = args[0]
    if options.union_file:
        all_bugs = get_saved_bug_data(options)
        print(len(all_bugs))
    else:
        all_bugs = {}
    lp = Launchpad.login_with(
        'milestone-report',
        service_root='https://api.launchpad.net',
        version='devel', credentials_file=options.credentials)
    json_source = pull_bugs(
        project, lp, all_bugs, status=options.statuses,
        modified_since=options.since, importance=options.importance,
        milestone=options.milestone, tags=options.tags,
        private=options.private)
    json_data = json.dumps(json_source, indent=0)
    with open(options.outfile, 'w') as json_file:
        json_file.write(json_data)


def pull_bugs(project, lp, all_bugs, status, modified_since, importance,
              milestone, tags, private):
    targets = get_targets(lp, project)
    search_params = dict(
        status=status, modified_since=modified_since, importance=importance,
        milestone=milestone, tags=tags, tags_combinator='All',
        omit_duplicates=True)
    for target in targets:
        get_bug_data(target, all_bugs, search_params, private)
    print(len(all_bugs))
    now = datetime.utcnow()
    json_source = {
        u'date_retrieved': now.isoformat(' '),
        u'bugs': all_bugs.values(),
    }
    return json_source


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