~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=""):
9
    process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
10
                    stderr=PIPE, close_fds=True)
11
    stdout, stderr = process.communicate(input)
12
    status = process.wait()
13
    if status < 0:
14
        raise Exception("%s killed by signal %i" (args[0], -status))
15
    return stdout, stderr, status
16
    
17
493 by Martin Pool
- Merge aaron's merge command
18
def patch(patch_contents, filename, output_filename=None, reverse=False):
19
    """Apply a patch to a file, to produce another output file.  This is should
20
    be suitable for our limited purposes.
21
22
    :param patch_contents: The contents of the patch to apply
23
    :type patch_contents: str
24
    :param filename: the name of the file to apply the patch to
25
    :type filename: str
26
    :param output_filename: The filename to produce.  If None, file is \
27
    modified in-place
28
    :type output_filename: str or NoneType
29
    :param reverse: If true, apply the patch in reverse
30
    :type reverse: bool
31
    :return: 0 on success, 1 if some hunks failed
32
    """
33
    args = ["patch", "-f", "-s", "--posix", "--binary"]
34
    if reverse:
35
        args.append("--reverse")
36
    if output_filename is not None:
37
        args.extend(("-o", output_filename))
38
    args.append(filename)
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
39
    stdout, stderr, status = write_to_cmd(args, patch_contents)
493 by Martin Pool
- Merge aaron's merge command
40
    return status 
41
42
43
def diff(orig_file, mod_str, orig_label=None, mod_label=None):
44
    """Compare two files, and produce a patch.
45
46
    :param orig_file: path to the old file
47
    :type orig_file: str
48
    :param mod_str: Contents of the new file
49
    :type mod_str: str
50
    :param orig_label: The label to use for the old file
51
    :type orig_label: str
52
    :param mod_label: The label to use for the new file
53
    :type mod_label: str
54
    """
55
    args = ["diff", "-u" ]
56
    if orig_label is not None and mod_label is not None:
57
        args.extend(("-L", orig_label, "-L", mod_label))
58
    args.extend(("--", orig_file, "-"))
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
59
    patch, stderr, status = write_to_cmd(args, mod_str)
493 by Martin Pool
- Merge aaron's merge command
60
    if status == 0:
61
        return None
62
    else:
63
        return patch
64
65
def diff3(out_file, mine_path, older_path, yours_path):
66
    def add_label(args, label):
67
        args.extend(("-L", label))
68
    args = ['diff3', "-E", "--merge"]
69
    add_label(args, "TREE")
70
    add_label(args, "ANCESTOR")
71
    add_label(args, "MERGE-SOURCE")
72
    args.extend((mine_path, older_path, yours_path))
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
73
    output, stderr, status = write_to_cmd(args)
493 by Martin Pool
- Merge aaron's merge command
74
    if status not in (0, 1):
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
75
        raise Exception(stderr)
493 by Martin Pool
- Merge aaron's merge command
76
    file(out_file, "wb").write(output)
77
    return status