1
# Copyright (C) 2004, 2005 Canonical Ltd
1
# Copyright (C) 2004, 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
# TODO: Perhaps rather than mapping options and arguments back and
18
18
# forth, we should just pass in the whole argv, and allow
19
19
# ExternalCommands to handle it differently to internal commands?
24
22
from bzrlib.commands import Command
27
25
class ExternalCommand(Command):
28
"""Class to wrap external commands."""
26
"""Class to wrap external commands.
28
The only wrinkle is that we have to map bzr's dictionary of
29
options and arguments back into command line options and arguments
31
34
def find_command(cls, cmd):
33
36
bzrpath = os.environ.get('BZRPATH', '')
35
38
for dir in bzrpath.split(os.pathsep):
36
## Empty directories are not real paths
39
# This needs to be os.path.join() or windows cannot
40
# find the batch file that you are wanting to execute
41
39
path = os.path.join(dir, cmd)
42
40
if os.path.isfile(path):
43
41
return ExternalCommand(path)
48
46
def __init__(self, path):
49
pipe = os.popen('%s --bzr-usage' % path, 'r')
50
self.takes_options = pipe.readline().split()
52
for opt in self.takes_options:
53
if not opt in OPTIONS:
54
raise BzrError("Unknown option '%s' returned by external command %s"
57
# TODO: Is there any way to check takes_args is valid here?
58
self.takes_args = pipe.readline().split()
60
if pipe.close() is not None:
61
raise BzrError("Failed funning '%s --bzr-usage'" % path)
63
pipe = os.popen('%s --bzr-help' % path, 'r')
64
self.__doc__ = pipe.read()
65
if pipe.close() is not None:
66
raise BzrError("Failed funning '%s --bzr-help'" % path)
68
def __call__(self, options, arguments):
69
Command.__init__(self, options, arguments)
52
return os.path.basename(self.path)
54
def run(self, *args, **kwargs):
55
raise NotImplementedError('should not be called on %r' % self)
57
def run_argv_aliases(self, argv, alias_argv=None):
58
return os.spawnv(os.P_WAIT, self.path, [self.path] + argv)
61
m = 'external command from %s\n\n' % self.path
62
pipe = os.popen('%s --help' % self.path)
63
return m + pipe.read()
73
raise NotImplementedError()
75
def run(self, **kargs):
76
raise NotImplementedError()
84
optname = name.replace('_','-')
86
if OPTIONS.has_key(optname):
88
opts.append('--%s' % optname)
89
if value is not None and value is not True:
90
opts.append(str(value))
92
# it's an arg, or arg list
93
if type(value) is not list:
99
self.status = os.spawnv(os.P_WAIT, self.path, [self.path] + opts + args)