~bzr-pqm/bzr/bzr.dev

5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
1
# Copyright (C) 2010 Canonical Ltd.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Utility functions for managing external merge tools such as kdiff3."""
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
21
import os
22
import shutil
23
import subprocess
24
import sys
25
import tempfile
26
27
from bzrlib.lazy_import import lazy_import
28
lazy_import(globals(), """
29
from bzrlib import (
30
    cmdline,
5321.1.83 by Gordon Tyler
Use osutils.find_executable_on_path in is_available instead.
31
    osutils,
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
32
    trace,
33
)
34
""")
35
36
5321.1.108 by Gordon Tyler
Changed known merge tools into a default set of merge tools that are always defined but can be overridden by user-defined merge tools.
37
known_merge_tools = {
5321.2.8 by Vincent Ladeuil
_KNOWN_MERGE_TOOLS should be a dict (there is an hidden assumption that the merg tool is unique anyway).
38
    'bcompare': 'bcompare {this} {other} {base} {result}',
39
    'kdiff3': 'kdiff3 {base} {this} {other} -o {result}',
40
    'xdiff': 'xxdiff -m -O -M {result} {this} {base} {other}',
41
    'meld': 'meld {base} {this_temp} {other}',
42
    'opendiff': 'opendiff {this} {other} -ancestor {base} -merge {result}',
43
    'winmergeu': 'winmergeu {result}',
44
}
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
45
46
47
def check_availability(command_line):
48
    cmd_list = cmdline.split(command_line)
49
    exe = cmd_list[0]
50
    if sys.platform == 'win32':
51
        if os.path.isabs(exe):
52
            base, ext = os.path.splitext(exe)
53
            path_ext = [unicode(s.lower())
54
                        for s in os.getenv('PATHEXT', '').split(os.pathsep)]
55
            return os.path.exists(exe) and ext in path_ext
56
        else:
57
            return osutils.find_executable_on_path(exe) is not None
58
    else:
59
        return (os.access(exe, os.X_OK)
60
                or osutils.find_executable_on_path(exe) is not None)
61
62
63
def invoke(command_line, filename, invoker=None):
64
    """Invokes the given merge tool command line, substituting the given
65
    filename according to the embedded substitution markers. Optionally, it
66
    will use the given invoker function instead of the default
67
    subprocess_invoker.
68
    """
69
    if invoker is None:
70
        invoker = subprocess_invoker
71
    cmd_list = cmdline.split(command_line)
72
    args, tmp_file = _subst_filename(cmd_list, filename)
73
    def cleanup(retcode):
74
        if tmp_file is not None:
75
            if retcode == 0: # on success, replace file with temp file
76
                shutil.move(tmp_file, filename)
77
            else: # otherwise, delete temp file
78
                os.remove(tmp_file)
79
    return invoker(args[0], args[1:], cleanup)
80
81
82
def _subst_filename(args, filename):
83
    subst_names = {
5321.1.119 by Gordon Tyler
Replace usage of format function from python 2.6 with our own very simple formatting function.
84
        'base': filename + u'.BASE',
85
        'this': filename + u'.THIS',
86
        'other': filename + u'.OTHER',
87
        'result': filename,
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
88
    }
89
    tmp_file = None
90
    subst_args = []
91
    for arg in args:
5321.1.119 by Gordon Tyler
Replace usage of format function from python 2.6 with our own very simple formatting function.
92
        if '{this_temp}' in arg and not 'this_temp' in subst_names:
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
93
            fh, tmp_file = tempfile.mkstemp(u"_bzr_mergetools_%s.THIS" %
94
                                            os.path.basename(filename))
95
            trace.mutter('fh=%r, tmp_file=%r', fh, tmp_file)
96
            os.close(fh)
97
            shutil.copy(filename + u".THIS", tmp_file)
98
            subst_names['this_temp'] = tmp_file
5321.1.119 by Gordon Tyler
Replace usage of format function from python 2.6 with our own very simple formatting function.
99
        arg = _format_arg(arg, subst_names)
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
100
        subst_args.append(arg)
101
    return subst_args, tmp_file
102
103
5321.1.119 by Gordon Tyler
Replace usage of format function from python 2.6 with our own very simple formatting function.
104
# This would be better implemented using format() from python 2.6
105
def _format_arg(arg, subst_names):
106
    arg = arg.replace('{base}', subst_names['base'])
107
    arg = arg.replace('{this}', subst_names['this'])
108
    arg = arg.replace('{other}', subst_names['other'])
109
    arg = arg.replace('{result}', subst_names['result'])
110
    if subst_names.has_key('this_temp'):
111
        arg = arg.replace('{this_temp}', subst_names['this_temp'])
112
    return arg
113
114
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
115
def subprocess_invoker(executable, args, cleanup):
116
    retcode = subprocess.call([executable] + args)
117
    cleanup(retcode)
118
    return retcode