/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/patch.py

  • Committer: Robert Collins
  • Date: 2005-10-16 00:22:17 UTC
  • mto: This revision was merged to the branch mainline in revision 1457.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051016002217-aa38f9c1eb13ee48
Plugins are now loaded under bzrlib.plugins, not bzrlib.plugin.

Plugins are also made available for other plugins to use by making them 
accessible via import bzrlib.plugins.NAME. You should not import other
plugins during the __init__ of your plugin though, as no ordering is
guaranteed, and the plugins directory is not on the python path.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from subprocess import Popen, PIPE
 
3
"""
 
4
Diff and patch functionality
 
5
"""
 
6
__docformat__ = "restructuredtext"
 
7
 
 
8
def write_to_cmd(args, input=""):
 
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
 
 
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
 
 
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)
 
44
    stdout, stderr, status = write_to_cmd(args, patch_contents)
 
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))
 
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)
 
60
    return status