~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patch.py

  • Committer: Martin Pool
  • Date: 2005-08-01 23:14:12 UTC
  • mfrom: (974.1.11)
  • Revision ID: mbp@sourcefrog.net-20050801231412-6a6278d24a7cbb0a
- merge aaron's revert and merge improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
 
import popen2
 
2
from subprocess import Popen, PIPE
3
3
"""
4
4
Diff and patch functionality
5
5
"""
6
6
__docformat__ = "restructuredtext"
7
7
 
 
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
 
8
18
def patch(patch_contents, filename, output_filename=None, reverse=False):
9
19
    """Apply a patch to a file, to produce another output file.  This is should
10
20
    be suitable for our limited purposes.
26
36
    if output_filename is not None:
27
37
        args.extend(("-o", output_filename))
28
38
    args.append(filename)
29
 
    process = popen2.Popen3(args, bufsize=len(patch_contents))
30
 
    process.tochild.write(patch_contents)
31
 
    process.tochild.close()
32
 
    status = os.WEXITSTATUS(process.wait())
 
39
    stdout, stderr, status = write_to_cmd(args, patch_contents)
33
40
    return status 
34
41
 
35
42
 
49
56
    if orig_label is not None and mod_label is not None:
50
57
        args.extend(("-L", orig_label, "-L", mod_label))
51
58
    args.extend(("--", orig_file, "-"))
52
 
    process = popen2.Popen3(args, bufsize=len(mod_str))
53
 
    process.tochild.write(mod_str)
54
 
    process.tochild.close()
55
 
    patch = process.fromchild.read()
56
 
    status = os.WEXITSTATUS(process.wait())
 
59
    patch, stderr, status = write_to_cmd(args, mod_str)
57
60
    if status == 0:
58
61
        return None
59
62
    else:
67
70
    add_label(args, "ANCESTOR")
68
71
    add_label(args, "MERGE-SOURCE")
69
72
    args.extend((mine_path, older_path, yours_path))
70
 
    process = popen2.Popen4(args)
71
 
    process.tochild.close()
72
 
    output = process.fromchild.read()
73
 
    status = os.WEXITSTATUS(process.wait())
 
73
    output, stderr, status = write_to_cmd(args)
74
74
    if status not in (0, 1):
75
 
        raise Exception(output)
 
75
        raise Exception(stderr)
76
76
    file(out_file, "wb").write(output)
77
77
    return status