aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/bugdir.py
blob: 5e1d4094f87eb581a00dac63e389bdab35f06968 (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
import os
import os.path
import cmdutil
import errno
import names

class NoBugDir(cmdutil.UserError):
    def __init__(self, path):
        msg = "The directory \"%s\" has no bug directory." % path
        Exception.__init__(self, msg)
        self.path = path
    

def tree_root(dir):
    rootdir = os.path.realpath(dir)
    while (True):
        versionfile=os.path.join(rootdir, ".be/version")
        if os.path.exists(versionfile):
            test_version(versionfile)
            break;
        elif rootdir == "/":
            raise NoBugDir(dir)
        rootdir=os.path.dirname(rootdir)
    return BugDir(os.path.join(rootdir, ".be"))

def test_version(path):
    assert (file(path, "rb").read() == "Bugs Everywhere Tree 0 0\n")

class BugDir:
    def __init__(self, dir):
        self.dir = dir
        self.bugs_path = os.path.join(self.dir, "bugs")


    def list(self):
        for uuid in os.listdir(self.bugs_path):
            if (uuid.startswith('.')):
                continue
            yield Bug(self.bugs_path, uuid)

    def new_bug(self):
        uuid = names.uuid()
        path = os.path.join(self.bugs_path, uuid)
        os.mkdir(path)
        return Bug(self.bugs_path, uuid)


def file_property(name, valid=None):
    def getter(self):
        value = self._get_value(name) 
        if valid is not None:
            assert value in valid
        return value
    def setter(self, value):
        if valid is not None:
            assert value in valid
        return self._set_value(name, value)
    return property(getter, setter)

class Bug(object):
    def __init__(self, path, uuid):
        self.path = os.path.join(path, uuid)
        self.uuid = uuid

    def get_path(self, file):
        return os.path.join(self.path, file)

    def _get_name(self):
        return self._get_value("name")
    
    def _set_name(self, value):
        return self._set_value("name", value)
    
    name = file_property("name")
    summary = file_property("summary")
    creator = file_property("creator")
    target = file_property("target")
    status = file_property("status", valid=("open", "closed"))
    severity = file_property("severity", valid=("wishlist", "minor", "serious",
                                                "critical", "fatal"))

    def _get_active(self):
        return self.status == "open"

    active = property(_get_active)

    def _get_value(self, name):
        try:
            return file(self.get_path(name), "rb").read().rstrip("\n")
        except IOError, e:
            if e.errno == errno.EEXIST:
                return None

    def _set_value(self, name, value):
        if value is None:
            os.unlink(self.get_path(name))
        file(self.get_path(name), "wb").write("%s\n" % value)