~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):
27
        self.file_list = file_list
28
        if file_list is not None and len(file_list) > 0:
29
            location = file_list[0]
30
        else:
31
            location = '.'
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
32
0.1.96 by Michael Ellerman
Support for bzr 0.7 and 0.8 in BzrPatchSource. Big thanks to marienz
33
        # Hack to cope with 0.7 and 0.8 bzr
34
        try:
335 by Aaron Bentley
Fixed checkout handling in Shelve
35
            from bzrlib.workingtree import WorkingTree
36
            self.wt = WorkingTree.open_containing(location)[0]
37
            self.base = self.wt.basedir
0.1.96 by Michael Ellerman
Support for bzr 0.7 and 0.8 in BzrPatchSource. Big thanks to marienz
38
            self.__readlines = self._v08_readlines
39
        except ImportError:
40
            from bzrlib.branch import Branch
41
            self.branch = Branch.open_containing(location)[0]
42
            self.base = self.branch.base
43
            self.__readlines = self._v07_readlines
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
44
45
        self.revision = revision
0.2.1 by Michael Ellerman
Factor out patch generation into PatchSource classes.
46
47
        PatchSource.__init__(self)
48
49
    def readlines(self):
0.1.96 by Michael Ellerman
Support for bzr 0.7 and 0.8 in BzrPatchSource. Big thanks to marienz
50
        from StringIO import StringIO
51
        f = StringIO()
52
        self.__readlines(f)
53
        f.seek(0)
54
        return f.readlines()
55
56
    def _v07_readlines(self, output):
57
        from bzrlib.diff import show_diff
58
        show_diff(self.branch, self.revision,
59
            specific_files=self.file_list, output=output)
60
61
    def _v08_readlines(self, output):
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
62
        import sys
63
        from bzrlib.diff import diff_cmd_helper
0.1.96 by Michael Ellerman
Support for bzr 0.7 and 0.8 in BzrPatchSource. Big thanks to marienz
64
        tmp = sys.stdout
65
        sys.stdout = output
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
66
        # FIXME diff_cmd_helper() should take an output parameter
335 by Aaron Bentley
Fixed checkout handling in Shelve
67
        diff_cmd_helper(self.wt, self.file_list, external_diff_options=None,
68
                        old_revision_spec=self.revision)
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
69
        sys.stdout = tmp