~abentley/bzrtools/bzrtools.dev

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
import patches

class PatchSource(object):
    def __iter__(self):
        def iterator(obj):
            for p in obj.read():
                yield p
        return iterator(self)

    def can_live_update(self):
        return False

    def readlines(self):
        raise NotImplementedError()

    def readhunks(self):
        return patches.parse_patches(self.readlines())

class FilePatchSource(PatchSource):
    def __init__(self, filename):
        self.filename = filename
        PatchSource.__init__(self)

    def readlines(self):
        f = open(self.filename, 'r')
        return f.readlines()

class BzrPatchSource(PatchSource):
    def __init__(self, revision=None, file_list=None):
        from bzrlib.branch import Branch
        self.revision = revision
        self.file_list = file_list
        if file_list is not None and len(file_list) > 0:
            location = file_list[0]
        else:
            location = '.'
        self.branch = Branch.open_containing(location)[0]

        PatchSource.__init__(self)

    def can_live_update(self):
        return True

    def readlines(self):
        from StringIO import StringIO
        from bzrlib.diff import show_diff
        f = StringIO()
        show_diff(self.branch, self.revision,
                specific_files=self.file_list, output=f)
        f.seek(0)
        return f.readlines()