root/bzr-plugins/extcommand/__init__.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright (C) 2008 Lukas Lalinsky <lalinsky@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""Support for integrating external commands into bzr

If you want to add new command, e.g. `bzr sed`, you can either
create a script named `bzr-sed`::

#!/bin/sh
bzr ls --versioned --kind=file | xargs sed -i $1

and put it somewhere to $PATH or define an alias in ~/.bazaar/bazaar.conf::

[EXTERNAL_ALIASES]
sed = bzr ls --versioned --kind=file | xargs sed -i $1

In both cases you will be able to execute the command using::

$ bzr sed s/foo/bar/g
"""

from bzrlib import externalcommand


class ExternalAlias(externalcommand.Command):

def __init__(self, name, cmdline):
self.name = lambda: name
self.cmdline = cmdline

def run_argv_aliases(self, argv, alias_argv=None):
import re, subprocess
def _replace_arg(match):
space = match.group(1)
i = int(match.group(2)) - 1
try:
return space + '"%s"' % argv[i].replace(r'"', r'\"')
except IndexError:
return space
cmdline = re.sub(r'([^\\])\$(\d+)', _replace_arg, self.cmdline)
subprocess.call(cmdline, shell=True)

def help(self):
return 'external command: %s\n' % self.cmdline


class ExternalCommand(externalcommand.ExternalCommand):

@classmethod
def find_command(cls, name):
import os, sys, shlex, subprocess
# Try to find an alias
from bzrlib.config import GlobalConfig
config = GlobalConfig()
try:
cmdline = config._get_parser().get_value('EXTERNAL_ALIASES', name)
return ExternalAlias(name, cmdline)
except KeyError:
pass
return None
# Try to find an executable named "bzr-COMMAND"
executable = 'bzr-%s' % cmd
if sys.platform == 'win32':
executables = [executable + '.bat', executable + '.cmd', executable + '.exe']
else:
executables = [executable]
for path in os.environ.get('PATH', '').split(os.pathsep):
for executable in executables:
f = os.path.join(path, executable)
if os.path.isfile(f):
return cls(f)
# Use bzrlib's own ExternalCommand
return externalcommand.ExternalCommand(cls, cmd)

def help(self):
return 'external command from %s\n' % self.path


externalcommand.ExternalCommand = ExternalCommand