~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patchsource.py

  • Committer: Robert Collins
  • Date: 2005-09-07 13:02:59 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050907130259-35bb613478f3ec74
start adding baz_import unit test cases

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import re
2
 
 
3
 
from bzrlib import (
4
 
    patches,
5
 
    workingtree,
6
 
    )
7
 
 
8
 
from bzrlib.plugins.bzrtools import errors
9
 
 
10
 
class PatchSource(object):
11
 
    def __iter__(self):
12
 
        def iterator(obj):
13
 
            for p in obj.read():
14
 
                yield p
15
 
        return iterator(self)
16
 
 
17
 
    def readlines(self):
18
 
        raise NotImplementedError()
19
 
 
20
 
    def readpatches(self):
21
 
        return patches.parse_patches(self.readlines())
22
 
 
23
 
class FilePatchSource(PatchSource):
24
 
    def __init__(self, filename):
25
 
        self.filename = filename
26
 
        PatchSource.__init__(self)
27
 
 
28
 
    def readlines(self):
29
 
        f = open(self.filename, 'r')
30
 
        return f.readlines()
31
 
 
32
 
class BzrPatchSource(PatchSource):
33
 
    def __init__(self, revision=None, file_list=None):
34
 
        open_containing_paths = workingtree.WorkingTree.open_containing_paths
35
 
        self.tree, self.file_list = open_containing_paths(file_list)
36
 
        self.base = self.tree.basedir
37
 
        self.revision = revision
38
 
 
39
 
        # Hacks to cope with v0.7 and v0.8 of bzr
40
 
        if self.revision is None:
41
 
            if hasattr(self.tree, 'basis_tree'):
42
 
                self.old_tree = self.tree.basis_tree()
43
 
            else:
44
 
                self.old_tree = self.tree.branch.basis_tree()
45
 
        else:
46
 
            revision_id = self.revision.in_store(self.tree.branch).rev_id
47
 
            if hasattr(self.tree.branch, 'repository'):
48
 
                self.old_tree = self.tree.branch.repository.revision_tree(revision_id)
49
 
            else:
50
 
                self.old_tree = self.tree.branch.revision_tree(revision_id)
51
 
 
52
 
        PatchSource.__init__(self)
53
 
 
54
 
    def readlines(self):
55
 
        from bzrlib.diff import show_diff_trees
56
 
        from StringIO import StringIO
57
 
        f = StringIO()
58
 
 
59
 
        show_diff_trees(self.old_tree, self.tree, f, self.file_list,
60
 
                        old_label='', new_label='')
61
 
        if re.search('Binary files .* differ', f.getvalue()):
62
 
            raise errors.ChangedBinaryFiles()
63
 
        f.seek(0)
64
 
        return f.readlines()