3
from subprocess import Popen, PIPE
5
from bzrlib.errors import NoDiff3
6
from bzrlib.textfile import check_text_path
8
Diff and patch functionality
10
__docformat__ = "restructuredtext"
12
def write_to_cmd(args, input=""):
14
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
15
stderr=PIPE, close_fds=True)
17
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
20
stdout, stderr = process.communicate(input)
21
status = process.wait()
23
raise Exception("%s killed by signal %i" (args[0], -status))
24
return stdout, stderr, status
27
def patch(patch_contents, filename, output_filename=None, reverse=False):
28
"""Apply a patch to a file, to produce another output file. This is should
29
be suitable for our limited purposes.
31
:param patch_contents: The contents of the patch to apply
32
:type patch_contents: str
33
:param filename: the name of the file to apply the patch to
35
:param output_filename: The filename to produce. If None, file is \
37
:type output_filename: str or NoneType
38
:param reverse: If true, apply the patch in reverse
40
:return: 0 on success, 1 if some hunks failed
42
args = ["patch", "-f", "-s", "--posix", "--binary"]
44
args.append("--reverse")
45
if output_filename is not None:
46
args.extend(("-o", output_filename))
48
stdout, stderr, status = write_to_cmd(args, patch_contents)
52
def diff3(out_file, mine_path, older_path, yours_path):
53
def add_label(args, label):
54
args.extend(("-L", label))
55
check_text_path(mine_path)
56
check_text_path(older_path)
57
check_text_path(yours_path)
58
args = ['diff3', "-E", "--merge"]
59
add_label(args, "TREE")
60
add_label(args, "ANCESTOR")
61
add_label(args, "MERGE-SOURCE")
62
args.extend((mine_path, older_path, yours_path))
64
output, stderr, status = write_to_cmd(args)
66
if e.errno == errno.ENOENT:
70
if status not in (0, 1):
71
raise Exception(stderr)
72
file(out_file, "wb").write(output)