~abentley/bzrtools/bzrtools.dev

0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
1
import patches
2
3
class PatchSource(object):
4
    def __iter__(self):
5
        def iterator(obj):
6
            for p in obj.read():
7
                yield p
8
        return iterator(self)
9
10
    def can_live_update(self):
11
        return False
12
13
    def readlines(self):
14
        raise NotImplementedError()
15
16
    def readpatches(self):
17
        return patches.parse_patches(self.readlines())
18
19
class FilePatchSource(PatchSource):
20
    def __init__(self, filename):
21
        self.filename = filename
22
        PatchSource.__init__(self)
23
24
    def readlines(self):
25
        f = open(self.filename, 'r')
26
        return f.readlines()
27
28
class BzrPatchSource(PatchSource):
29
    def __init__(self, revision=None, file_list=None):
30
        from bzrlib.branch import Branch
31
        self.revision = revision
32
        self.file_list = file_list
33
        if file_list is not None and len(file_list) > 0:
34
            location = file_list[0]
35
        else:
36
            location = '.'
37
        self.branch = Branch.open_containing(location)[0]
38
39
        PatchSource.__init__(self)
40
41
    def can_live_update(self):
42
        return True
43
44
    def readlines(self):
45
        from StringIO import StringIO
46
        from bzrlib.diff import show_diff
47
        f = StringIO()
48
        show_diff(self.branch, self.revision,
49
                specific_files=self.file_list, output=f)
50
        f.seek(0)
51
        return f.readlines()