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
17
# TODO: Perhaps rather than mapping options and arguments back and
18
# forth, we should just pass in the whole argv, and allow
19
# ExternalCommands to handle it differently to internal commands?
22
from bzrlib.commands import Command
25
class ExternalCommand(Command):
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
34
def find_command(cls, cmd):
36
bzrpath = os.environ.get('BZRPATH', '')
38
for dir in bzrpath.split(os.pathsep):
39
path = os.path.join(dir, cmd)
40
if os.path.isfile(path):
41
return ExternalCommand(path)
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)
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)