aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/bug.py
blob: 4cb53c271b629d1795a2f778fe7589e936cda57a (plain) (blame)
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
# 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 errno
import names
import mapfile
import time
import utility
import doctest

### Define and describe valid bug categories
# Use a tuple of (category, description) tuples since we don't have
# ordered dicts in Python yet http://www.python.org/dev/peps/pep-0372/

# in order of increasing severity
severity_level_def = (
  ("wishlist","A feature that could improve usefullness, but not a bug."),
  ("minor","The standard bug level."),
  ("serious","A bug that requires workarounds."),
  ("critical","A bug that prevents some features from working at all."),
  ("fatal","A bug that makes the package unusable."))

# in order of increasing resolution
# roughly following http://www.bugzilla.org/docs/3.2/en/html/lifecycle.html
active_status_def = (
  ("unconfirmed","A possible bug which lacks independent existance confirmation."),
  ("open","A working bug that has not been assigned to a developer."),
  ("assigned","A working bug that has been assigned to a developer."),
  ("test","The code has been adjusted, but the fix is still being tested."))
inactive_status_def = (
  ("closed", "The bug is no longer relevant."),
  ("fixed", "The bug should no longer occur."),
  ("wontfix","It's not a bug, it's a feature."),
  ("disabled", "?"))


### Convert the description tuples to more useful formats

severity_values = tuple([val for val,description in severity_level_def])
severity_description = dict(severity_level_def)
severity_index = {}
for i in range(len(severity_values)):
    severity_index[severity_values[i]] = i

active_status_values = tuple(val for val,description in active_status_def)
inactive_status_values = tuple(val for val,description in inactive_status_def)
status_values = active_status_values + inactive_status_values
status_description = dict(active_status_def+inactive_status_def)
status_index = {}
for i in range(len(status_values)):
    status_index[status_values[i]] = i


def checked_property(name, valid):
    """
    Provide access to an attribute name, testing for valid values.
    """
    def getter(self):
        value = getattr(self, "_"+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 setattr(self, "_"+name, value)
    return property(getter, setter)


class Bug(object):
    severity = checked_property("severity", severity_values)
    status = checked_property("status", status_values)

    def __init__(self, path, uuid, rcs, bugdir):
        self.path = path
        self.uuid = uuid
        if uuid is not None:
            dict = mapfile.map_load(self.get_path("values"))
        else:
            dict = {}

        self.rcs = rcs
        self.bugdir = bugdir
        
        self.summary = dict.get("summary")
        self.creator = dict.get("creator")
        self.target = dict.get("target")
        self.status = dict.get("status", "open")
        self.severity = dict.get("severity", "minor")
        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=None):
        if file == None:
            return os.path.join(self.path, self.uuid)
        else:
            return os.path.join(self.path, self.uuid, file)

    def _get_active(self):
        return self.status in active_status_values

    active = property(_get_active)

    def __repr__(self):
        return "Bug(uuid=%r)" % self.uuid

    def string(self, bugs=None, shortlist=False):
        if bugs == None:
            bugs = list(self.bugdir.list())
        short_name = names.unique_name(self, bugs)
        if shortlist == False:
            if self.time == None:
                timestring = ""
            else:
                htime = utility.handy_time(self.time)
                ftime = utility.time_to_str(self.time)
                timestring = "%s (%s)" % (htime, ftime)
            info = [("ID", self.uuid),
                    ("Short name", short_name),
                    ("Severity", self.severity),
                    ("Status", self.status),
                    ("Assigned", self.assigned),
                    ("Target", self.target),
                    ("Creator", self.creator),
                    ("Created", timestring)]
            newinfo = []
            for k,v in info:
                if v == None:
                    newinfo.append((k,""))
                else:
                    newinfo.append((k,v))
            info = newinfo
            longest_key_len = max([len(k) for k,v in info])
            infolines = ["  %*s : %s\n" %(longest_key_len,k,v) for k,v in info]
            return "".join(infolines) + "%s\n" % self.summary
        else:
            statuschar = self.status[0]
            severitychar = self.severity[0]
            chars = "%c%c" % (statuschar, severitychar)
            return "%s:%s: %s" % (short_name, chars, self.summary)

    def __str__(self):
        return self.string(shortlist=True)

    def __cmp__(self, other):
        return cmp_full(self, other)

    def add_attr(self, map, name):
        value = getattr(self, name)
        if value is not None:
            map[name] = value

    def save(self):
        assert self.summary != None, "Can't save blank bug"
        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")
        mapfile.map_save(self.rcs, path, map)

    def remove(self):
        path = self.get_path()
        self.rcs.recursive_remove(path)
    
    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()
        comm.rcs = self.rcs
        return comm

    def get_comment(self, uuid):
        return Comment(uuid, self)

    def iter_comment_ids(self):
        path = self.get_path("comments")
        if not os.path.isdir(path):
            return
        try:
            for uuid in os.listdir(path):
                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_time)
        return comments

def new_comment(bug, body=None):
    comm = bug.new_comment()
    comm.From = comm.rcs.get_user_id()
    comm.time = 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 = getattr(obj, 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:
            map = mapfile.map_load(self.get_path("values"))
            self.time = utility.str_to_time(map["Date"])
            self.From = map["From"]
            self.in_reply_to = map.get("In-reply-to")
            self.content_type = map.get("Content-type", "text/plain")
            self.body = file(self.get_path("body")).read().decode("utf-8")
        else:
            self.time = None
            self.From = None
            self.in_reply_to = None
            self.content_type = "text/plain"
            self.body = None

    def save(self):
        map_file = {"Date": utility.time_to_str(self.time)}
        add_headers(self, map_file, ("From", "in_reply_to", "content_type"))
        if not os.path.exists(self.get_path()):
            self.bug.rcs.mkdir(self.get_path())
        mapfile.map_save(self.bug.rcs, self.get_path("values"), map_file)
        self.bug.rcs.set_file_contents(self.get_path("body"), 
                                       self.body.encode('utf-8'))

    def get_path(self, name=None):
        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 thread_comments(comments):
    child_map = {}
    top_comments = []
    for comment in comments:
        child_map[comment.uuid] = []
    for comment in comments:
        if comment.in_reply_to is None or comment.in_reply_to not in child_map:
            top_comments.append(comment)
            continue
        child_map[comment.in_reply_to].append(comment)

    def recurse_children(comment):
        child_list = []
        for child in child_map[comment.uuid]:
            child_list.append(recurse_children(child))
        return (comment, child_list)
    return [recurse_children(c) for c in top_comments]

def pyname_to_header(name):
    return name.capitalize().replace('_', '-')



class MockBug:
    def __init__(self, attr, value):
        setattr(self, attr, value)

# the general rule for bug sorting is that "more important" bugs are
# less than "less important" bugs.  This way sorting a list of bugs
# will put the most important bugs first in the list.  When relative
# importance is unclear, the sorting follows some arbitrary convention
# (i.e. dictionary order).

def cmp_severity(bug_1, bug_2):
    """
    Compare the severity levels of two bugs, with more severe bugs comparing
    as less.

    >>> attr="severity"
    >>> cmp_severity(MockBug(attr,"wishlist"), MockBug(attr,"wishlist")) == 0
    True
    >>> cmp_severity(MockBug(attr,"wishlist"), MockBug(attr,"minor")) > 0
    True
    >>> cmp_severity(MockBug(attr,"critical"), MockBug(attr,"wishlist")) < 0
    True
    """
    return -cmp(severity_index[bug_1.severity], severity_index[bug_2.severity])

def cmp_status(bug_1, bug_2):
    """
    Compare the status levels of two bugs, with more 'open' bugs
    comparing as less.

    >>> attr="status"
    >>> cmp_status(MockBug(attr,"open"), MockBug(attr,"open")) == 0
    True
    >>> cmp_status(MockBug(attr,"open"), MockBug(attr,"closed")) < 0
    True
    >>> cmp_status(MockBug(attr,"closed"), MockBug(attr,"open")) > 0
    True
    """
    val_2 = status_index[bug_2.status]
    return cmp(status_index[bug_1.status], status_index[bug_2.status])

def cmp_attr(bug_1, bug_2, attr, invert=False):
    """
    Compare a general attribute between two bugs using the conventional
    comparison rule for that attribute type.  If invert == True, sort
    *against* that convention.
    >>> attr="severity"
    >>> cmp_attr(MockBug(attr,1), MockBug(attr,2), attr, invert=False) < 0
    True
    >>> cmp_attr(MockBug(attr,1), MockBug(attr,2), attr, invert=True) > 0
    True
    >>> cmp_attr(MockBug(attr,1), MockBug(attr,1), attr) == 0
    True
    """
    if invert == True :
        return -cmp(getattr(bug_1, attr), getattr(bug_2, attr))
    else :
        return cmp(getattr(bug_1, attr), getattr(bug_2, attr))

# alphabetical rankings (a < z)
cmp_creator = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "creator")
cmp_assigned = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "assigned")
# chronological rankings (newer < older)
cmp_time = lambda bug_1, bug_2 : cmp_attr(bug_1, bug_2, "time", invert=True)

def cmp_full(bug_1, bug_2, cmp_list=(cmp_status,cmp_severity,cmp_assigned,
                                     cmp_time,cmp_creator)):
    for comparison in cmp_list :
        val = comparison(bug_1, bug_2)
        if val != 0 :
            return val
    return 0

class InvalidValue(ValueError):
    def __init__(self, name, value):
        msg = "Cannot assign value %s to %s" % (value, name)
        Exception.__init__(self, msg)
        self.name = name
        self.value = value

suite = doctest.DocTestSuite()