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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
# Copyright (C) 2005 Aaron Bentley and Panometrics, Inc.
# <abentley@panoramicfeedback.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import os.path
import cmdutil
import errno
import names
import mapfile
import time
import utility
from rcs import rcs_by_name
class NoBugDir(Exception):
def __init__(self, path):
msg = "The directory \"%s\" has no bug directory." % path
Exception.__init__(self, msg)
self.path = path
def tree_root(dir, old_version=False):
rootdir = os.path.realpath(dir)
while (True):
versionfile=os.path.join(rootdir, ".be/version")
if os.path.exists(versionfile):
if not old_version:
test_version(versionfile)
break;
elif rootdir == "/":
raise NoBugDir(dir)
rootdir=os.path.dirname(rootdir)
return BugDir(os.path.join(rootdir, ".be"))
class BadTreeVersion(Exception):
def __init__(self, version):
Exception.__init__(self, "Unsupported tree version: %s" % version)
self.version = version
def test_version(path):
tree_version = file(path, "rb").read()
if tree_version != TREE_VERSION_STRING:
raise BadTreeVersion(tree_version)
def set_version(path, rcs):
rcs.set_file_contents(os.path.join(path, "version"), TREE_VERSION_STRING)
TREE_VERSION_STRING = "Bugs Everywhere Tree 1 0\n"
class NoRootEntry(Exception):
def __init__(self, path):
self.path = path
Exception.__init__(self, "Specified root does not exist: %s" % path)
class AlreadyInitialized(Exception):
def __init__(self, path):
self.path = path
Exception.__init__(self,
"Specified root is already initialized: %s" % path)
def create_bug_dir(path, rcs):
"""
>>> import no_rcs, tests
>>> create_bug_dir('/highly-unlikely-to-exist', no_rcs)
Traceback (most recent call last):
NoRootEntry: Specified root does not exist: /highly-unlikely-to-exist
>>> test_dir = os.path.dirname(tests.bug_arch_dir().dir)
>>> try:
... create_bug_dir(test_dir, no_rcs)
... except AlreadyInitialized, e:
... print "Already Initialized"
Already Initialized
"""
root = os.path.join(path, ".be")
try:
rcs.mkdir(root, paranoid=True)
except OSError, e:
if e.errno == errno.ENOENT:
raise NoRootEntry(path)
elif e.errno == errno.EEXIST:
raise AlreadyInitialized(path)
else:
raise
rcs.mkdir(os.path.join(root, "bugs"))
set_version(root, rcs)
map_save(rcs, os.path.join(root, "settings"), {"rcs_name": rcs.name})
return BugDir(os.path.join(path, ".be"))
def setting_property(name, valid=None):
def getter(self):
value = self.settings.get(name)
if valid is not None:
if value not in valid:
raise InvalidValue(name, value)
return value
def setter(self, value):
if valid is not None:
if value not in valid and value is not None:
raise InvalidValue(name, value)
if value is None:
del self.settings[name]
else:
self.settings[name] = value
self.save_settings()
return property(getter, setter)
class BugDir:
def __init__(self, dir):
self.dir = dir
self.bugs_path = os.path.join(self.dir, "bugs")
try:
self.settings = map_load(os.path.join(self.dir, "settings"))
except NoSuchFile:
self.settings = {"rcs_name": "None"}
rcs_name = setting_property("rcs_name", ("None", "bzr", "Arch"))
_rcs = None
target = setting_property("target")
def save_settings(self):
map_save(self.rcs, os.path.join(self.dir, "settings"), self.settings)
def get_rcs(self):
if self._rcs is not None and self.rcs_name == self._rcs.name:
return self._rcs
self._rcs = rcs_by_name(self.rcs_name)
return self._rcs
rcs = property(get_rcs)
def get_reference_bugdir(self, spec):
return BugDir(self.rcs.path_in_reference(self.dir, spec))
def list(self):
for uuid in self.list_uuids():
yield self.get_bug(uuid)
def bug_map(self):
bugs = {}
for bug in self.list():
bugs[bug.uuid] = bug
return bugs
def get_bug(self, uuid):
return Bug(self.bugs_path, uuid, self.rcs_name)
def list_uuids(self):
for uuid in os.listdir(self.bugs_path):
if (uuid.startswith('.')):
continue
yield uuid
def new_bug(self, uuid=None):
if uuid is None:
uuid = names.uuid()
path = os.path.join(self.bugs_path, uuid)
self.rcs.mkdir(path)
bug = Bug(self.bugs_path, None, self.rcs_name)
bug.uuid = uuid
return bug
class InvalidValue(Exception):
def __init__(self, name, value):
msg = "Cannot assign value %s to %s" % (value, name)
Exception.__init__(self, msg)
self.name = name
self.value = value
def checked_property(name, valid):
def getter(self):
value = self.__getattribute__("_"+name)
if value not in valid:
raise InvalidValue(name, value)
return value
def setter(self, value):
if value not in valid:
raise InvalidValue(name, value)
return self.__setattr__("_"+name, value)
return property(getter, setter)
severity_levels = ("wishlist", "minor", "serious", "critical", "fatal")
severity_value = {}
for i in range(len(severity_levels)):
severity_value[severity_levels[i]] = i
class Bug(object):
status = checked_property("status", (None, "open", "closed", "in-progress"))
severity = checked_property("severity", (None, "wishlist", "minor",
"serious", "critical", "fatal"))
def __init__(self, path, uuid, rcs_name):
self.path = path
self.uuid = uuid
if uuid is not None:
dict = map_load(self.get_path("values"))
else:
dict = {}
self.rcs_name = rcs_name
self.summary = dict.get("summary")
self.creator = dict.get("creator")
self.target = dict.get("target")
self.status = dict.get("status")
self.severity = dict.get("severity")
self.assigned = dict.get("assigned")
self.time = dict.get("time")
if self.time is not None:
self.time = utility.str_to_time(self.time)
def get_path(self, file):
return os.path.join(self.path, self.uuid, file)
def _get_active(self):
return self.status in ("open", "in-progress")
active = property(_get_active)
def add_attr(self, map, name):
value = self.__getattribute__(name)
if value is not None:
map[name] = value
def save(self):
map = {}
self.add_attr(map, "assigned")
self.add_attr(map, "summary")
self.add_attr(map, "creator")
self.add_attr(map, "target")
self.add_attr(map, "status")
self.add_attr(map, "severity")
if self.time is not None:
map["time"] = utility.time_to_str(self.time)
path = self.get_path("values")
map_save(rcs_by_name(self.rcs_name), path, map)
def _get_rcs(self):
return rcs_by_name(self.rcs_name)
rcs = property(_get_rcs)
def new_comment(self):
if not os.path.exists(self.get_path("comments")):
self.rcs.mkdir(self.get_path("comments"))
comm = Comment(None, self)
comm.uuid = names.uuid()
return comm
def get_comment(self, uuid):
return Comment(uuid, self)
def iter_comment_ids(self):
try:
for uuid in os.listdir(self.get_path("comments")):
if (uuid.startswith('.')):
continue
yield uuid
except OSError, e:
if e.errno != errno.ENOENT:
raise
return
def list_comments(self):
comments = [Comment(id, self) for id in self.iter_comment_ids()]
comments.sort(cmp_date)
return comments
def cmp_date(comm1, comm2):
return cmp(comm1.date, comm2.date)
def new_bug(dir, uuid=None):
bug = dir.new_bug(uuid)
bug.creator = names.creator()
bug.severity = "minor"
bug.status = "open"
bug.time = time.time()
return bug
def new_comment(bug, body=None):
comm = bug.new_comment()
comm.From = names.creator()
comm.date = time.time()
comm.body = body
return comm
def add_headers(obj, map, names):
map_names = {}
for name in names:
map_names[name] = pyname_to_header(name)
add_attrs(obj, map, names, map_names)
def add_attrs(obj, map, names, map_names=None):
if map_names is None:
map_names = {}
for name in names:
map_names[name] = name
for name in names:
value = obj.__getattribute__(name)
if value is not None:
map[map_names[name]] = value
class Comment(object):
def __init__(self, uuid, bug):
object.__init__(self)
self.uuid = uuid
self.bug = bug
if self.uuid is not None and self.bug is not None:
mapfile = map_load(self.get_path("values"))
self.date = utility.str_to_time(mapfile["Date"])
self.From = mapfile["From"]
self.in_reply_to = mapfile.get("In-reply-to")
self.body = file(self.get_path("body")).read()
else:
self.date = None
self.From = None
self.in_reply_to = None
self.body = None
def save(self):
map_file = {"Date": utility.time_to_str(self.date)}
add_headers(self, map_file, ("From", "in_reply_to"))
if not os.path.exists(self.get_path(None)):
self.bug.rcs.mkdir(self.get_path(None))
map_save(self.bug.rcs, self.get_path("values"), map_file)
self.bug.rcs.set_file_contents(self.get_path("body"), self.body)
def get_path(self, name):
my_dir = os.path.join(self.bug.get_path("comments"), self.uuid)
if name is None:
return my_dir
return os.path.join(my_dir, name)
def pyname_to_header(name):
return name.capitalize().replace('_', '-')
def map_save(rcs, path, map):
"""Save the map as a mapfile to the specified path"""
add = not os.path.exists(path)
output = file(path, "wb")
mapfile.generate(output, map)
if add:
rcs.add_id(path)
class NoSuchFile(Exception):
def __init__(self, pathname):
Exception.__init__(self, "No such file: %s" % pathname)
def map_load(path):
try:
return mapfile.parse(file(path, "rb"))
except IOError, e:
if e.errno != errno.ENOENT:
raise e
raise NoSuchFile(path)
class MockBug:
def __init__(self, severity):
self.severity = severity
def cmp_severity(bug_1, bug_2):
"""
Compare the severity levels of two bugs, with more sever bugs comparing
as less.
>>> cmp_severity(MockBug(None), MockBug(None))
0
>>> cmp_severity(MockBug("wishlist"), MockBug(None)) < 0
True
>>> cmp_severity(MockBug(None), MockBug("wishlist")) > 0
True
>>> cmp_severity(MockBug("critical"), MockBug("wishlist")) < 0
True
"""
val_1 = severity_value.get(bug_1.severity)
val_2 = severity_value.get(bug_2.severity)
return -cmp(val_1, val_2)
|