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
52
53
54
55
|
import patches
class PatchSource(object):
def __iter__(self):
def iterator(obj):
for p in obj.read():
yield p
return iterator(self)
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.bzrdir import BzrDir
self.file_list = file_list
if file_list is not None and len(file_list) > 0:
location = file_list[0]
else:
location = '.'
self.bzrdir = BzrDir.open_containing(location)[0]
self.base = self.bzrdir.open_branch().base
self.revision = revision
PatchSource.__init__(self)
def readlines(self):
import sys
from StringIO import StringIO
from bzrlib.diff import diff_cmd_helper
# FIXME diff_cmd_helper() should take an output parameter
f = StringIO()
tmp = sys.stdout
sys.stdout = f
diff_cmd_helper(self.bzrdir.open_workingtree(), self.file_list,
external_diff_options=None, old_revision_spec=self.revision)
sys.stdout = tmp
f.seek(0)
return f.readlines()
|