~ubuntu-reports-dev/ubuntu-reports/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
#!/usr/bin/env python
#
# 2009-07-23
# Find out how many bugs a particular package has open and the count
# for each status / importance - similar to 'bugnumbers --stats'

from datetime import datetime
from launchpadlib.launchpad import Launchpad

import optparse
import os
import simplejson as json
import sys
import time

OPEN_STATUS = ['New', 'Incomplete', 'Confirmed', 'Triaged', 'In Progress',
               'Fix Committed']
CLOSED_STATUS = ['''Won't Fix''', 'Fix Released', 'Invalid', 'Expired',
                 'Opinion']

ALL_STATUS = OPEN_STATUS + CLOSED_STATUS


def setup_credentials():
    cachedir = os.path.expanduser("~/.launchpadlib/cache/")

    if not os.path.exists(cachedir):
        os.makedirs(cachedir, 0700)

    script_name = sys.argv[0].split("/")[-1].split('.')[0]

    credfile = os.path.expanduser('~/.launchpadlib/%s.cred' % script_name)

    launchpad = Launchpad.login_with(script_name, service_root='production',
        launchpadlib_dir=cachedir, credentials_file=credfile, version='devel')
    return launchpad


def load_existing_stats(data_filename):
    try:
        data_file = open(data_filename, 'r')
    except IOError:
        print("Unable to open %s" % data_filename)
        sys.exit(1)
    json_data = json.load(data_file)
    data_file.close()

    for record in json_data['records']:
        records.append(record)


if __name__ == '__main__':
    parser = optparse.OptionParser('Usage: %prog [-p SRCPACKAGE| -P PROJECT]')
    parser.add_option('-p', '--package', help='Ubuntu source package')
    parser.add_option('-P', '--project', help='Launchpad project')
    (opt, args) = parser.parse_args()
    if not opt.package and not opt.project:
        parser.error('No package or project provided')

    launchpad = setup_credentials()

    if opt.package:
        ubuntu = launchpad.distributions['ubuntu']
        target_name = opt.package
        target = ubuntu.getSourcePackage(name='%s' % target_name)
    if opt.project:
        target_name = opt.project
        target = launchpad.projects['%s' % target_name]

    now = int(time.time())
    records = []
    record = {}
    counts = {}
    stats_filename = "%s-bug-stats.json" % (target_name)

    if os.path.isfile(stats_filename):
        load_existing_stats(stats_filename)
        last_check = records[-1].keys()[0]
        last_check_datetime = datetime.fromtimestamp(float(last_check))
        latest_records = records[-1][last_check]
    else:
        last_check_datetime = None
        latest_records = []

    tasks = {}
    for bug_task in target.searchTasks(status=ALL_STATUS,
        modified_since=last_check_datetime):
        number = bug_task.bug.id
        bug_last_update = bug_task.bug.date_last_updated
        # add a newly reported reported bug
        if number not in latest_records:
            tasks[number] = {}
            tasks[number]['status'] = bug_task.status
            tasks[number]['importance'] = bug_task.importance
            tasks[number]['tags'] = bug_task.bug.tags
        # add a modified bug
        elif bug_last_update > last_check_datetime:
            tasks[number] = {}
            tasks[number]['status'] = bug_task.status
            tasks[number]['importance'] = bug_task.importance
            tasks[number]['tags'] = bug_task.bug.tags

    # if its not in the recently modified tasks lets add it
    for bug_number in latest_records:
        if bug_number not in tasks:
            tasks[bug_number] = latest_records[bug_number]

    record[now] = tasks
    records.append(record)

    report = {'records': records}
    stats_file = open(stats_filename, 'w')
    stats_file.write(json.dumps(report, indent=4))
    stats_file.close()