2
from subprocess import Popen, PIPE
4
Diff and patch functionality
6
__docformat__ = "restructuredtext"
8
def write_to_cmd(args, input=""):
10
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
11
stderr=PIPE, close_fds=True)
13
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
16
stdout, stderr = process.communicate(input)
17
status = process.wait()
19
raise Exception("%s killed by signal %i" (args[0], -status))
20
return stdout, stderr, status
23
def patch(patch_contents, filename, output_filename=None, reverse=False):
24
"""Apply a patch to a file, to produce another output file. This is should
25
be suitable for our limited purposes.
27
:param patch_contents: The contents of the patch to apply
28
:type patch_contents: str
29
:param filename: the name of the file to apply the patch to
31
:param output_filename: The filename to produce. If None, file is \
33
:type output_filename: str or NoneType
34
:param reverse: If true, apply the patch in reverse
36
:return: 0 on success, 1 if some hunks failed
38
args = ["patch", "-f", "-s", "--posix", "--binary"]
40
args.append("--reverse")
41
if output_filename is not None:
42
args.extend(("-o", output_filename))
44
stdout, stderr, status = write_to_cmd(args, patch_contents)
48
def diff3(out_file, mine_path, older_path, yours_path):
49
def add_label(args, label):
50
args.extend(("-L", label))
51
args = ['diff3', "-E", "--merge"]
52
add_label(args, "TREE")
53
add_label(args, "ANCESTOR")
54
add_label(args, "MERGE-SOURCE")
55
args.extend((mine_path, older_path, yours_path))
56
output, stderr, status = write_to_cmd(args)
57
if status not in (0, 1):
58
raise Exception(stderr)
59
file(out_file, "wb").write(output)