1163
by Martin Pool
- split ExternalCommand class into its own file |
1 |
# Copyright (C) 2004, 2005 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
||
18 |
from bzrlib.commands import Command |
|
19 |
||
20 |
||
21 |
class ExternalCommand(Command): |
|
22 |
"""Class to wrap external commands.
|
|
23 |
||
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.
|
|
27 |
||
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
|
|
31 |
forward.
|
|
32 |
||
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
|
|
35 |
script.
|
|
36 |
"""
|
|
37 |
||
38 |
def find_command(cls, cmd): |
|
39 |
import os.path |
|
40 |
bzrpath = os.environ.get('BZRPATH', '') |
|
41 |
||
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) |
|
46 |
||
47 |
return None |
|
48 |
||
49 |
find_command = classmethod(find_command) |
|
50 |
||
51 |
def __init__(self, path): |
|
52 |
self.path = path |
|
53 |
||
54 |
pipe = os.popen('%s --bzr-usage' % path, 'r') |
|
55 |
self.takes_options = pipe.readline().split() |
|
56 |
||
57 |
for opt in self.takes_options: |
|
58 |
if not opt in OPTIONS: |
|
59 |
raise BzrError("Unknown option '%s' returned by external command %s" |
|
60 |
% (opt, path)) |
|
61 |
||
62 |
# TODO: Is there any way to check takes_args is valid here?
|
|
63 |
self.takes_args = pipe.readline().split() |
|
64 |
||
65 |
if pipe.close() is not None: |
|
66 |
raise BzrError("Failed funning '%s --bzr-usage'" % path) |
|
67 |
||
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) |
|
72 |
||
73 |
def __call__(self, options, arguments): |
|
74 |
Command.__init__(self, options, arguments) |
|
75 |
return self |
|
76 |
||
77 |
def name(self): |
|
78 |
raise NotImplementedError() |
|
79 |
||
80 |
def run(self, **kargs): |
|
81 |
raise NotImplementedError() |
|
82 |
||
83 |
opts = [] |
|
84 |
args = [] |
|
85 |
||
86 |
keys = kargs.keys() |
|
87 |
keys.sort() |
|
88 |
for name in keys: |
|
89 |
optname = name.replace('_','-') |
|
90 |
value = kargs[name] |
|
91 |
if OPTIONS.has_key(optname): |
|
92 |
# it's an option
|
|
93 |
opts.append('--%s' % optname) |
|
94 |
if value is not None and value is not True: |
|
95 |
opts.append(str(value)) |
|
96 |
else: |
|
97 |
# it's an arg, or arg list
|
|
98 |
if type(value) is not list: |
|
99 |
value = [value] |
|
100 |
for v in value: |
|
101 |
if v is not None: |
|
102 |
args.append(str(v)) |
|
103 |
||
104 |
self.status = os.spawnv(os.P_WAIT, self.path, [self.path] + opts + args) |
|
105 |
return self.status |
|
106 |
||
107 |
||
108 |