/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« 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 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
# 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?
 
20
 
 
21
 
 
22
from bzrlib.commands import Command
 
23
 
 
24
 
 
25
class ExternalCommand(Command):
 
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
    """
 
32
 
 
33
    @classmethod
 
34
    def find_command(cls, cmd):
 
35
        import os.path
 
36
        bzrpath = os.environ.get('BZRPATH', '')
 
37
 
 
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)
 
42
 
 
43
        return None
 
44
 
 
45
 
 
46
    def __init__(self, path):
 
47
        self.path = path
 
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
 
 
72
    def name(self):
 
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
 
 
103