~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2006-02-12 03:47:20 UTC
  • mto: (0.1.73 shelf-tmp)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20060212034720-1482cf30017f2ec7
Infrastructure to allow for multiple interchangeable shelves.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from diffstat import DiffStat
8
8
from patchsource import PatchSource, FilePatchSource
9
9
 
10
 
BASE_DIR = '.shelf'
11
 
 
12
10
class Shelf(object):
13
11
    MESSAGE_PREFIX = "# Shelved patch: "
14
12
 
15
 
    def __init__(self, base, name='default'):
 
13
    _paths = {
 
14
        'base'          : '.shelf',
 
15
        'shelves'       : '.shelf/shelves',
 
16
        'current-shelf' : '.shelf/current-shelf',
 
17
    }
 
18
 
 
19
    def __init__(self, base, name=None):
 
20
        self.base = base
 
21
        self.__setup()
 
22
 
 
23
        if name is None:
 
24
            current = os.path.join(self.base, self._paths['current-shelf'])
 
25
            name = open(current).read().strip()
 
26
            assert '\n' not in name
 
27
 
16
28
        self.name = name
17
 
        self.base = base
18
 
        shelf_base = os.path.join(self.base, BASE_DIR)
19
 
        self.shelf_dir = os.path.join(shelf_base, name)
 
29
        self.dir = os.path.join(self.base, self._paths['shelves'], name)
 
30
        if not os.path.isdir(self.dir):
 
31
            os.mkdir(self.dir)
20
32
 
21
 
        for dir in [shelf_base, self.shelf_dir]:
 
33
    def __setup(self):
 
34
        # Create required directories etc.
 
35
        for dir in [self._paths['base'], self._paths['shelves']]:
 
36
            dir = os.path.join(self.base, dir)
22
37
            if not os.path.isdir(dir):
23
38
                os.mkdir(dir)
24
39
 
 
40
        current = os.path.join(self.base, self._paths['current-shelf'])
 
41
        if not os.path.exists(current):
 
42
            f = open(current, 'w')
 
43
            f.write('default')
 
44
            f.close()
 
45
 
25
46
    def log(self, msg):
26
47
        sys.stderr.write(msg)
27
48
 
52
73
            self.log(' %.2d: %s\n' % (index, msg))
53
74
 
54
75
    def __path(self, index):
55
 
        return os.path.join(self.shelf_dir, '%.2d' % index)
 
76
        return os.path.join(self.dir, '%.2d' % index)
56
77
 
57
78
    def next_patch(self):
58
79
        indexes = self.__list()
64
85
        return self.__path(next)
65
86
 
66
87
    def __list(self):
67
 
        patches = os.listdir(self.shelf_dir)
 
88
        patches = os.listdir(self.dir)
68
89
        indexes = [int(f) for f in patches]
69
90
        indexes.sort()
70
91
        return indexes