~launchpad-pqm/launchpad/devel

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import os
import re
from datetime import datetime
import email
import cStringIO

class Bug:
    def __init__(self, db, id, package=None, date=None, status=None,
                 originator=None, severity=None, tags=None, report=None):
        self.db = db
        self.id = id
        self._emails = []

        if package:
            self.package = package
        if date:
            self.date = date
        if status:
            self.status = status
        if originator:
            self.originator = originator
        if severity:
            self.severity = severity
        if tags:
            self.tags = tags
        if report:
            self.report = report

    def is_open(self):
        #return not self.done and 'fixed' not in self.tags
        return self.status != 'done' and 'fixed' not in self.tags

    def affects_unstable(self):
        return 'sid' in self.tags or ('woody' not in self.tags and
                                 'sarge' not in self.tags and
                                 'experimental' not in self.tags)

    def affects_package(self, packageset):
        for package in self.packagelist():
            if package in packageset:
                return True
        return False

    def is_release_critical(self):
        return self.severity in ('critical', 'grave', 'serious')

    def __str__(self):
        return 'Bug#%d' % self.id

    def __getattr__(self, name):
        # Lazy loading of non-indexed attributes

        if not self.db.load(self, name):
            raise AttributeError, name

        if not hasattr(self, name):
            raise InternalError, "Database.load did not provide attribute '%s'" % name

        return getattr(self, name)

    def packagelist(self):
        if self.package is None:
            return []
        if ',' in self.package:
            return self.package.split(',')
        return [self.package]

    def emails(self):
        if self._emails:
            return self._emails
        for comment in self.comments:
            message = email.message_from_string(comment)
            self._emails.append(message)
        return self._emails

class IndexParseError(Exception): pass
class StatusParseError(Exception): pass
class StatusMissing(Exception): pass
class SummaryMissing(Exception): pass
class SummaryParseError(Exception): pass
class SummaryVersionError(Exception): pass
class ReportMissing(Exception): pass
class ReportParseError(Exception): pass
class LogMissing(Exception): pass
class LogParseFailed(Exception): pass
class InternalError(Exception): pass

class Database:
    def __init__(self, root):
        self.root = root

    class bug_iterator:
        index_record = re.compile(r'^(?P<package>\S+) (?P<bugid>\d+) (?P<date>\d+) (?P<status>\w+) \[(?P<originator>.*)\] (?P<severity>\w+)(?: (?P<tags>.*))?$')

        def __init__(self, db, filter=None):
            self.db = db
            self.index = open(os.path.join(self.db.root, 'index/index.db'))
            self.filter = filter

        def next(self):
            line = self.index.readline()
            if not line:
                raise StopIteration

            match = self.index_record.match(line)
            if not match:
                raise IndexParseError(line)

            return Bug(self.db,
                       int(match.group('bugid')),
                       match.group('package'),
                       datetime.fromtimestamp(int(match.group('date'))),
                       match.group('status'),
                       match.group('originator'),
                       match.group('severity'),
                       match.group('tags').split(' '))
                       
    def load(self, bug, name):
        if name in ('originator', 'date', 'subject', 'msgid', 'package',
                    'tags', 'done', 'forwarded', 'mergedwith', 'severity'):
            self.load_summary(bug)
        elif name == 'report':
            self.load_report(bug)
        elif name in ('comments',):
            self.load_log(bug)
        elif name == 'status':
            if bug.done is not None:
                bug.status = 'done'
            if bug.forwarded is not None:
                bug.status = 'forwarded'
            bug.status = 'open'
        else:
            return False

        return True

    def load_summary(self, bug):
        summary = os.path.join(self.root, 'db-h', self._hash(bug), '%d.summary' % bug.id)

        try:
            fd = open(summary)
        except IOError, e:
            if e.errno == 2:
                raise SummaryMissing, summary
            raise

        try:
            message = email.message_from_file(fd)
        except Exception, e:
            raise SummaryParseError, '%s: %s' % (summary, str(e))

        version = message['format-version']
        if version is None:
            raise SummaryParseError, "%s: Missing Format-Version" % summary
        
        if version != '2':
            raise SummaryVersionError, "%s: I don't understand version %s" % (summary, version)

        bug.originator = message['submitter']
        bug.date = datetime.fromtimestamp(int(message['date']))
        bug.subject = message['subject']
        bug.msgid = message['message-id']
        bug.package = message['package']
        bug.done = message['done']
        bug.forwarded = message['forwarded-to']
        bug.severity = message['severity']

        if 'merged-with' in message:
            bug.mergedwith = map(int,message['merged-with'].split(' '))
        else:
            bug.mergedwith = []

        if 'tags' in message:
            bug.tags = message['tags'].split(' ')
        else:
            bug.tags = []

    def load_report(self, bug):
        report = os.path.join(self.root, 'db-h', self._hash(bug), '%d.report' % bug.id)

        try:
            fd = open(report)
        except IOError, e:
            if e.errno == 2:
                raise ReportMissing, report
            raise

        bug.report = fd.read()
        fd.close()

    def load_log(self, bug):
        log = os.path.join(self.root, 'db-h', self._hash(bug), '%d.log' % bug.id)
        comments = []

        try:
            logreader = os.popen('./debbugs-log.pl %s' % log, 'r')
            comment = cStringIO.StringIO()
            for line in logreader:
                if line == '.\n':
                    comments.append(comment.getvalue())
                    comment = cStringIO.StringIO()
                elif line.startswith('.'):
                    comment.write(line[1:])
                else:
                    comment.write(line)
            if comment.tell() != 0:
                raise LogParseFailed('Unterminated comment from debbugs-log.pl')
            exitcode = logreader.close()
            if exitcode is not None:
                raise LogParseFailed('debbugs-log.pl exited with code %d' % exitcode)
        except IOError, e:
            if e.errno == 2:
                raise LogMissing, log
            raise

        bug.comments = comments

    def _hash(self, bug):
        return '%02d' % (bug.id % 100)

    def __iter__(self):
        return self.bug_iterator(self, None)

    def __getitem__(self, bug):
        return Bug(self, bug)

if __name__ == '__main__':
    import sys

    for bug in Database('/srv/debzilla.no-name-yet.com/debbugs'):
        try:
            print bug, bug.subject
        except Exception, e:
            print >>sys.stderr, '%s: %s' % (e.__class__.__name__, str(e))