~bzr-pqm/bzr/bzr.dev

493 by Martin Pool
- Merge aaron's merge command
1
import os
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
2
from subprocess import Popen, PIPE
493 by Martin Pool
- Merge aaron's merge command
3
"""
4
Diff and patch functionality
5
"""
6
__docformat__ = "restructuredtext"
7
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
8
def write_to_cmd(args, input=""):
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
9
    if os.name != 'nt':
10
        process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
11
                        stderr=PIPE, close_fds=True)
12
    else:
13
        process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
14
                        stderr=PIPE)
15
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
16
    stdout, stderr = process.communicate(input)
17
    status = process.wait()
18
    if status < 0:
19
        raise Exception("%s killed by signal %i" (args[0], -status))
20
    return stdout, stderr, status
21
    
22
493 by Martin Pool
- Merge aaron's merge command
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.
26
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
30
    :type filename: str
31
    :param output_filename: The filename to produce.  If None, file is \
32
    modified in-place
33
    :type output_filename: str or NoneType
34
    :param reverse: If true, apply the patch in reverse
35
    :type reverse: bool
36
    :return: 0 on success, 1 if some hunks failed
37
    """
38
    args = ["patch", "-f", "-s", "--posix", "--binary"]
39
    if reverse:
40
        args.append("--reverse")
41
    if output_filename is not None:
42
        args.extend(("-o", output_filename))
43
    args.append(filename)
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
44
    stdout, stderr, status = write_to_cmd(args, patch_contents)
493 by Martin Pool
- Merge aaron's merge command
45
    return status 
46
47
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))
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
56
    output, stderr, status = write_to_cmd(args)
493 by Martin Pool
- Merge aaron's merge command
57
    if status not in (0, 1):
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
58
        raise Exception(stderr)
493 by Martin Pool
- Merge aaron's merge command
59
    file(out_file, "wb").write(output)
60
    return status