1
# Copyright (C) 2004, 2005 by Canonical Ltd
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.commands import Command
21
class ExternalCommand(Command):
22
"""Class to wrap external commands.
24
We cheat a little here, when get_cmd_class() calls us we actually
25
give it back an object we construct that has the appropriate path,
26
help, options etc for the specified command.
28
When run_bzr() tries to instantiate that 'class' it gets caught by
29
the __call__ method, which we override to call the Command.__init__
30
method. That then calls our run method which is pretty straight
33
The only wrinkle is that we have to map bzr's dictionary of options
34
and arguments back into command line options and arguments for the
38
def find_command(cls, cmd):
40
bzrpath = os.environ.get('BZRPATH', '')
42
for dir in bzrpath.split(os.pathsep):
43
path = os.path.join(dir, cmd)
44
if os.path.isfile(path):
45
return ExternalCommand(path)
49
find_command = classmethod(find_command)
51
def __init__(self, path):
54
pipe = os.popen('%s --bzr-usage' % path, 'r')
55
self.takes_options = pipe.readline().split()
57
for opt in self.takes_options:
58
if not opt in OPTIONS:
59
raise BzrError("Unknown option '%s' returned by external command %s"
62
# TODO: Is there any way to check takes_args is valid here?
63
self.takes_args = pipe.readline().split()
65
if pipe.close() is not None:
66
raise BzrError("Failed funning '%s --bzr-usage'" % path)
68
pipe = os.popen('%s --bzr-help' % path, 'r')
69
self.__doc__ = pipe.read()
70
if pipe.close() is not None:
71
raise BzrError("Failed funning '%s --bzr-help'" % path)
73
def __call__(self, options, arguments):
74
Command.__init__(self, options, arguments)
78
raise NotImplementedError()
80
def run(self, **kargs):
81
raise NotImplementedError()
89
optname = name.replace('_','-')
91
if OPTIONS.has_key(optname):
93
opts.append('--%s' % optname)
94
if value is not None and value is not True:
95
opts.append(str(value))
97
# it's an arg, or arg list
98
if type(value) is not list:
104
self.status = os.spawnv(os.P_WAIT, self.path, [self.path] + opts + args)