~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 readlines(self):
11
        raise NotImplementedError()
12
0.2.12 by Michael Ellerman
Try to clear up terminology confusion. A shelf contains multiple patches, each
13
    def readhunks(self):
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
14
        return patches.parse_patches(self.readlines())
15
16
class FilePatchSource(PatchSource):
17
    def __init__(self, filename):
18
        self.filename = filename
19
        PatchSource.__init__(self)
20
21
    def readlines(self):
22
        f = open(self.filename, 'r')
23
        return f.readlines()
24
25
class BzrPatchSource(PatchSource):
26
    def __init__(self, revision=None, file_list=None):
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
27
        from bzrlib.bzrdir import BzrDir
28
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
29
        self.file_list = file_list
30
        if file_list is not None and len(file_list) > 0:
31
            location = file_list[0]
32
        else:
33
            location = '.'
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
34
35
        self.bzrdir = BzrDir.open_containing(location)[0]
36
        self.base = self.bzrdir.open_branch().base
37
38
        self.revision = revision
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
39
40
        PatchSource.__init__(self)
41
42
    def readlines(self):
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
43
        import sys
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
44
        from StringIO import StringIO
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
45
        from bzrlib.diff import diff_cmd_helper
46
47
        # FIXME diff_cmd_helper() should take an output parameter
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
48
        f = StringIO()
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
49
        tmp = sys.stdout
50
        sys.stdout = f
51
        diff_cmd_helper(self.bzrdir.open_workingtree(), self.file_list,
52
                external_diff_options=None, old_revision_spec=self.revision)
53
        sys.stdout = tmp
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
54
        f.seek(0)
55
        return f.readlines()