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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# pylint: disable-msg=W0703
import cStringIO
from datetime import datetime
import email
import os
import re
import subprocess
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
description = None
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, debbugs_pl, subdir='db-h'):
self.root = root
self.debbugs_pl = debbugs_pl
self.subdir = subdir
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 in ('report', 'description'):
self.load_report(bug)
elif name in ('comments',):
self.load_log(bug)
elif name == 'status':
if bug.done is not None:
bug.status = 'done'
elif bug.forwarded is not None:
bug.status = 'forwarded'
else:
bug.status = 'open'
else:
return False
return True
def load_summary(self, bug):
summary = os.path.join(self.root, self.subdir, 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()
report_msg = email.message_from_string(bug.report)
charset = report_msg.get_content_charset('ascii')
description = report_msg.get_payload(decode=True)
bug.description = description.decode(charset)
def load_log(self, bug):
log = os.path.join(self.root, self.subdir, self._hash(bug),
'%d.log' % bug.id)
comments = []
# We set the perl path manually so that debbugs-log.pl can
# always find the Debbugs::Log module.
debbugs_path = os.path.dirname(self.debbugs_pl)
command = ['perl', '-I', debbugs_path, self.debbugs_pl, log]
try:
process = subprocess.Popen(command,
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
logreader = process.stdout
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)
logreader.close()
if comment.tell() != 0:
raise LogParseFailed(
'Unterminated comment from debbugs-log.pl')
process.wait()
err = process.stderr
errors = "\n".join(err.readlines())
if process.returncode != 0:
raise LogParseFailed(errors)
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_id):
bug = Bug(self, bug_id)
try:
self.load_summary(bug)
except SummaryMissing:
raise KeyError(bug_id)
return 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))
|