/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from .lazy_import import lazy_import
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
28
lazy_import(globals(), """
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
29
from breezy import (
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
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
6973.6.2 by Jelmer Vernooij
Fix more tests.
36
from .sixish import text_type
37
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
38
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.
39
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).
40
    'bcompare': 'bcompare {this} {other} {base} {result}',
41
    'kdiff3': 'kdiff3 {base} {this} {other} -o {result}',
42
    'xdiff': 'xxdiff -m -O -M {result} {this} {base} {other}',
43
    'meld': 'meld {base} {this_temp} {other}',
44
    'opendiff': 'opendiff {this} {other} -ancestor {base} -merge {result}',
45
    'winmergeu': 'winmergeu {result}',
46
}
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
47
48
49
def check_availability(command_line):
50
    cmd_list = cmdline.split(command_line)
51
    exe = cmd_list[0]
52
    if sys.platform == 'win32':
6437.44.1 by Gordon Tyler
Backport of fix for bug 939605 to bzr 2.5 series.
53
        exe = _get_executable_path(exe)
54
        if exe is None:
55
            return False
56
        base, ext = os.path.splitext(exe)
6973.6.4 by Jelmer Vernooij
Avoid text_type()
57
        path_ext = [s.lower()
6437.44.1 by Gordon Tyler
Backport of fix for bug 939605 to bzr 2.5 series.
58
                    for s in os.getenv('PATHEXT', '').split(os.pathsep)]
59
        return os.path.exists(exe) and ext in path_ext
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
60
    else:
61
        return (os.access(exe, os.X_OK)
62
                or osutils.find_executable_on_path(exe) is not None)
63
64
65
def invoke(command_line, filename, invoker=None):
66
    """Invokes the given merge tool command line, substituting the given
67
    filename according to the embedded substitution markers. Optionally, it
68
    will use the given invoker function instead of the default
69
    subprocess_invoker.
70
    """
71
    if invoker is None:
72
        invoker = subprocess_invoker
73
    cmd_list = cmdline.split(command_line)
6437.44.1 by Gordon Tyler
Backport of fix for bug 939605 to bzr 2.5 series.
74
    exe = _get_executable_path(cmd_list[0])
75
    if exe is not None:
76
        cmd_list[0] = exe
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
77
    args, tmp_file = _subst_filename(cmd_list, filename)
78
    def cleanup(retcode):
79
        if tmp_file is not None:
80
            if retcode == 0: # on success, replace file with temp file
81
                shutil.move(tmp_file, filename)
82
            else: # otherwise, delete temp file
83
                os.remove(tmp_file)
84
    return invoker(args[0], args[1:], cleanup)
85
86
6437.44.1 by Gordon Tyler
Backport of fix for bug 939605 to bzr 2.5 series.
87
def _get_executable_path(exe):
88
    if os.path.isabs(exe):
89
        return exe
90
    return osutils.find_executable_on_path(exe)
91
92
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
93
def _subst_filename(args, filename):
94
    subst_names = {
5321.1.119 by Gordon Tyler
Replace usage of format function from python 2.6 with our own very simple formatting function.
95
        'base': filename + u'.BASE',
96
        'this': filename + u'.THIS',
97
        'other': filename + u'.OTHER',
98
        'result': filename,
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
99
    }
100
    tmp_file = None
101
    subst_args = []
102
    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.
103
        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.
104
            fh, tmp_file = tempfile.mkstemp(u"_bzr_mergetools_%s.THIS" %
105
                                            os.path.basename(filename))
106
            trace.mutter('fh=%r, tmp_file=%r', fh, tmp_file)
107
            os.close(fh)
108
            shutil.copy(filename + u".THIS", tmp_file)
109
            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.
110
        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.
111
        subst_args.append(arg)
112
    return subst_args, tmp_file
113
114
5321.1.119 by Gordon Tyler
Replace usage of format function from python 2.6 with our own very simple formatting function.
115
# This would be better implemented using format() from python 2.6
116
def _format_arg(arg, subst_names):
117
    arg = arg.replace('{base}', subst_names['base'])
118
    arg = arg.replace('{this}', subst_names['this'])
119
    arg = arg.replace('{other}', subst_names['other'])
120
    arg = arg.replace('{result}', subst_names['result'])
6619.3.1 by Jelmer Vernooij
Apply 2to3 has_key fix.
121
    if 'this_temp' in subst_names:
5321.1.119 by Gordon Tyler
Replace usage of format function from python 2.6 with our own very simple formatting function.
122
        arg = arg.replace('{this_temp}', subst_names['this_temp'])
123
    return arg
124
125
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
126
def subprocess_invoker(executable, args, cleanup):
127
    retcode = subprocess.call([executable] + args)
128
    cleanup(retcode)
129
    return retcode