~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/externalcommand.py

  • Committer: Martin Pool
  • Date: 2005-09-01 11:27:20 UTC
  • Revision ID: mbp@sourcefrog.net-20050901112720-f5ccb6b6627991de
- work properly when $EDITOR contains multiple words

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2004, 2005 by Canonical Ltd
 
2
 
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.
7
 
#
 
7
 
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.
12
 
#
 
12
 
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
16
16
 
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?
20
20
 
21
21
 
22
 
import os
23
 
 
24
22
from bzrlib.commands import Command
25
23
 
26
24
 
27
25
class ExternalCommand(Command):
28
 
    """Class to wrap external commands."""
 
26
    """Class to wrap external commands.
 
27
 
 
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
 
30
    for the script.
 
31
    """
29
32
 
30
33
    @classmethod
31
34
    def find_command(cls, cmd):
33
36
        bzrpath = os.environ.get('BZRPATH', '')
34
37
 
35
38
        for dir in bzrpath.split(os.pathsep):
36
 
            ## Empty directories are not real paths
37
 
            if not dir:
38
 
                continue
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
47
        self.path = path
50
48
 
 
49
        pipe = os.popen('%s --bzr-usage' % path, 'r')
 
50
        self.takes_options = pipe.readline().split()
 
51
 
 
52
        for opt in self.takes_options:
 
53
            if not opt in OPTIONS:
 
54
                raise BzrError("Unknown option '%s' returned by external command %s"
 
55
                               % (opt, path))
 
56
 
 
57
        # TODO: Is there any way to check takes_args is valid here?
 
58
        self.takes_args = pipe.readline().split()
 
59
 
 
60
        if pipe.close() is not None:
 
61
            raise BzrError("Failed funning '%s --bzr-usage'" % path)
 
62
 
 
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)
 
67
 
 
68
    def __call__(self, options, arguments):
 
69
        Command.__init__(self, options, arguments)
 
70
        return self
 
71
 
51
72
    def name(self):
52
 
        return os.path.basename(self.path)
53
 
 
54
 
    def run(self, *args, **kwargs):
55
 
        raise NotImplementedError('should not be called on %r' % self)
56
 
 
57
 
    def run_argv_aliases(self, argv, alias_argv=None):
58
 
        return os.spawnv(os.P_WAIT, self.path, [self.path] + argv)
59
 
 
60
 
    def help(self):
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()
 
74
 
 
75
    def run(self, **kargs):
 
76
        raise NotImplementedError()
 
77
        
 
78
        opts = []
 
79
        args = []
 
80
 
 
81
        keys = kargs.keys()
 
82
        keys.sort()
 
83
        for name in keys:
 
84
            optname = name.replace('_','-')
 
85
            value = kargs[name]
 
86
            if OPTIONS.has_key(optname):
 
87
                # it's an option
 
88
                opts.append('--%s' % optname)
 
89
                if value is not None and value is not True:
 
90
                    opts.append(str(value))
 
91
            else:
 
92
                # it's an arg, or arg list
 
93
                if type(value) is not list:
 
94
                    value = [value]
 
95
                for v in value:
 
96
                    if v is not None:
 
97
                        args.append(str(v))
 
98
 
 
99
        self.status = os.spawnv(os.P_WAIT, self.path, [self.path] + opts + args)
 
100
        return self.status
 
101
 
 
102
 
64
103