~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:19:08 UTC
  • Revision ID: mbp@sourcefrog.net-20050901111907-ff5ac13ee6fedc85
- split commit message editor functions out into own file

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# ExternalCommands to handle it differently to internal commands?
20
20
 
21
21
 
22
 
import os
23
 
import sys
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):
43
46
    def __init__(self, path):
44
47
        self.path = path
45
48
 
46
 
 
 
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
 
47
72
    def name(self):
48
 
        return self.path.split(os.sep)[-1]
49
 
 
50
 
 
51
 
    def run(self, *args, **kwargs):
52
 
        raise NotImplementedError('should not be called on %r' % self)
53
 
 
54
 
 
55
 
    def run_argv(self, argv):
56
 
        return os.spawnv(os.P_WAIT, self.path, [self.path] + argv)
57
 
 
58
 
 
59
 
    def help(self):
60
 
        m = 'external command from %s\n\n' % self.path
61
 
        pipe = os.popen('%s --help' % self.path)
62
 
        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
 
 
103