~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Jelmer Vernooij
  • Date: 2005-11-04 17:26:05 UTC
  • mfrom: (1185.16.146)
  • mto: (1185.33.1)
  • mto: This revision was merged to the branch mainline in revision 1509.
  • Revision ID: jelmer@samba.org-20051104172605-9288f261492667fd
MergeĀ fromĀ Martin

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 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
 
18
18
# TODO: probably should say which arguments are candidates for glob
19
19
# expansion on windows and do that at the command level.
20
20
 
 
21
# TODO: Help messages for options.
 
22
 
21
23
# TODO: Define arguments by objects, rather than just using names.
22
24
# Those objects can specify the expected type of the argument, which
23
 
# would help with validation and shell completion.  They could also provide
24
 
# help/explanation for that argument in a structured way.
25
 
 
26
 
# TODO: Specific "examples" property on commands for consistent formatting.
 
25
# would help with validation and shell completion.
27
26
 
28
27
# TODO: "--profile=cum", to change sort order.  Is there any value in leaving
29
28
# the profile output behind so it can be interactively examined?
30
29
 
 
30
import sys
31
31
import os
32
 
import sys
33
 
 
34
 
from bzrlib.lazy_import import lazy_import
35
 
lazy_import(globals(), """
36
 
import codecs
 
32
from warnings import warn
 
33
from inspect import getdoc
37
34
import errno
38
 
import threading
39
 
from warnings import warn
40
35
 
41
36
import bzrlib
42
 
from bzrlib import (
43
 
    debug,
44
 
    errors,
45
 
    option,
46
 
    osutils,
47
 
    trace,
48
 
    win32utils,
49
 
    )
50
 
""")
51
 
 
52
 
from bzrlib import registry
53
 
# Compatibility
54
 
from bzrlib.hooks import HookPoint, Hooks
 
37
import bzrlib.trace
 
38
from bzrlib.trace import mutter, note, log_error, warning
 
39
from bzrlib.errors import (BzrError, 
 
40
                           BzrCheckError,
 
41
                           BzrCommandError,
 
42
                           BzrOptionError,
 
43
                           NotBranchError)
 
44
from bzrlib.revisionspec import RevisionSpec
 
45
from bzrlib import BZRDIR
55
46
from bzrlib.option import Option
56
47
 
57
 
 
58
 
class CommandInfo(object):
59
 
    """Information about a command."""
60
 
 
61
 
    def __init__(self, aliases):
62
 
        """The list of aliases for the command."""
63
 
        self.aliases = aliases
64
 
 
65
 
    @classmethod
66
 
    def from_command(klass, command):
67
 
        """Factory to construct a CommandInfo from a command."""
68
 
        return klass(command.aliases)
69
 
 
70
 
 
71
 
class CommandRegistry(registry.Registry):
72
 
 
73
 
    @staticmethod
74
 
    def _get_name(command_name):
75
 
        if command_name.startswith("cmd_"):
76
 
            return _unsquish_command_name(command_name)
77
 
        else:
78
 
            return command_name
79
 
 
80
 
    def register(self, cmd, decorate=False):
81
 
        """Utility function to help register a command
82
 
 
83
 
        :param cmd: Command subclass to register
84
 
        :param decorate: If true, allow overriding an existing command
85
 
            of the same name; the old command is returned by this function.
86
 
            Otherwise it is an error to try to override an existing command.
87
 
        """
88
 
        k = cmd.__name__
89
 
        k_unsquished = self._get_name(k)
90
 
        try:
91
 
            previous = self.get(k_unsquished)
92
 
        except KeyError:
93
 
            previous = _builtin_commands().get(k_unsquished)
94
 
        info = CommandInfo.from_command(cmd)
95
 
        try:
96
 
            registry.Registry.register(self, k_unsquished, cmd,
97
 
                                       override_existing=decorate, info=info)
98
 
        except KeyError:
99
 
            trace.log_error('Two plugins defined the same command: %r' % k)
100
 
            trace.log_error('Not loading the one in %r' %
101
 
                            sys.modules[cmd.__module__])
102
 
            trace.log_error('Previously this command was registered from %r' %
103
 
                            sys.modules[previous.__module__])
104
 
        return previous
105
 
 
106
 
    def register_lazy(self, command_name, aliases, module_name):
107
 
        """Register a command without loading its module.
108
 
 
109
 
        :param command_name: The primary name of the command.
110
 
        :param aliases: A list of aliases for the command.
111
 
        :module_name: The module that the command lives in.
112
 
        """
113
 
        key = self._get_name(command_name)
114
 
        registry.Registry.register_lazy(self, key, module_name, command_name,
115
 
                                        info=CommandInfo(aliases))
116
 
 
117
 
 
118
 
plugin_cmds = CommandRegistry()
 
48
plugin_cmds = {}
119
49
 
120
50
 
121
51
def register_command(cmd, decorate=False):
 
52
    "Utility function to help register a command"
122
53
    global plugin_cmds
123
 
    return plugin_cmds.register(cmd, decorate)
 
54
    k = cmd.__name__
 
55
    if k.startswith("cmd_"):
 
56
        k_unsquished = _unsquish_command_name(k)
 
57
    else:
 
58
        k_unsquished = k
 
59
    if not plugin_cmds.has_key(k_unsquished):
 
60
        plugin_cmds[k_unsquished] = cmd
 
61
        mutter('registered plugin command %s', k_unsquished)      
 
62
        if decorate and k_unsquished in builtin_command_names():
 
63
            return _builtin_commands()[k_unsquished]
 
64
    elif decorate:
 
65
        result = plugin_cmds[k_unsquished]
 
66
        plugin_cmds[k_unsquished] = cmd
 
67
        return result
 
68
    else:
 
69
        log_error('Two plugins defined the same command: %r' % k)
 
70
        log_error('Not loading the one in %r' % sys.modules[cmd.__module__])
124
71
 
125
72
 
126
73
def _squish_command_name(cmd):
128
75
 
129
76
 
130
77
def _unsquish_command_name(cmd):
 
78
    assert cmd.startswith("cmd_")
131
79
    return cmd[4:].replace('_','-')
132
80
 
133
81
 
137
85
    builtins = bzrlib.builtins.__dict__
138
86
    for name in builtins:
139
87
        if name.startswith("cmd_"):
140
 
            real_name = _unsquish_command_name(name)
 
88
            real_name = _unsquish_command_name(name)        
141
89
            r[real_name] = builtins[name]
142
90
    return r
143
91
 
 
92
            
144
93
 
145
94
def builtin_command_names():
146
95
    """Return list of builtin command names."""
147
96
    return _builtin_commands().keys()
148
 
 
 
97
    
149
98
 
150
99
def plugin_command_names():
151
100
    return plugin_cmds.keys()
155
104
    """Return name->class mapping for all commands."""
156
105
    d = _builtin_commands()
157
106
    if plugins_override:
158
 
        d.update(plugin_cmds.iteritems())
 
107
        d.update(plugin_cmds)
159
108
    return d
160
109
 
161
 
 
 
110
    
162
111
def get_all_cmds(plugins_override=True):
163
112
    """Return canonical name and class for all registered commands."""
164
113
    for k, v in _get_cmd_dict(plugins_override=plugins_override).iteritems():
171
120
    plugins_override
172
121
        If true, plugin commands can override builtins.
173
122
    """
174
 
    try:
175
 
        cmd = _get_cmd_object(cmd_name, plugins_override)
176
 
        # Allow plugins to extend commands
177
 
        for hook in Command.hooks['extend_command']:
178
 
            hook(cmd)
179
 
        return cmd
180
 
    except KeyError:
181
 
        raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
182
 
 
183
 
 
184
 
def _get_cmd_object(cmd_name, plugins_override=True):
185
 
    """Worker for get_cmd_object which raises KeyError rather than BzrCommandError."""
186
123
    from bzrlib.externalcommand import ExternalCommand
187
124
 
188
 
    # We want only 'ascii' command names, but the user may have typed
189
 
    # in a Unicode name. In that case, they should just get a
190
 
    # 'command not found' error later.
191
 
    # In the future, we may actually support Unicode command names.
 
125
    cmd_name = str(cmd_name)            # not unicode
192
126
 
193
127
    # first look up this command under the specified name
194
 
    if plugins_override:
195
 
        try:
196
 
            return plugin_cmds.get(cmd_name)()
197
 
        except KeyError:
198
 
            pass
199
 
    cmds = _get_cmd_dict(plugins_override=False)
 
128
    cmds = _get_cmd_dict(plugins_override=plugins_override)
200
129
    try:
201
130
        return cmds[cmd_name]()
202
131
    except KeyError:
203
132
        pass
204
 
    if plugins_override:
205
 
        for key in plugin_cmds.keys():
206
 
            info = plugin_cmds.get_info(key)
207
 
            if cmd_name in info.aliases:
208
 
                return plugin_cmds.get(key)()
 
133
 
209
134
    # look for any command which claims this as an alias
210
135
    for real_cmd_name, cmd_class in cmds.iteritems():
211
136
        if cmd_name in cmd_class.aliases:
215
140
    if cmd_obj:
216
141
        return cmd_obj
217
142
 
218
 
    # look for plugins that provide this command but aren't installed
219
 
    for provider in command_providers_registry:
220
 
        try:
221
 
            plugin_metadata = provider.plugin_for_command(cmd_name)
222
 
        except errors.NoPluginAvailable:
223
 
            pass
224
 
        else:
225
 
            raise errors.CommandAvailableInPlugin(cmd_name,
226
 
                                                  plugin_metadata, provider)
227
 
    raise KeyError
 
143
    raise BzrCommandError("unknown command %r" % cmd_name)
228
144
 
229
145
 
230
146
class Command(object):
252
168
        List of argument forms, marked with whether they are optional,
253
169
        repeated, etc.
254
170
 
255
 
                Examples:
256
 
 
257
 
                ['to_location', 'from_branch?', 'file*']
258
 
 
259
 
                'to_location' is required
260
 
                'from_branch' is optional
261
 
                'file' can be specified 0 or more times
262
 
 
263
171
    takes_options
264
172
        List of options that may be given for this command.  These can
265
173
        be either strings, referring to globally-defined options,
268
176
    hidden
269
177
        If true, this command isn't advertised.  This is typically
270
178
        for commands intended for expert users.
271
 
 
272
 
    encoding_type
273
 
        Command objects will get a 'outf' attribute, which has been
274
 
        setup to properly handle encoding of unicode strings.
275
 
        encoding_type determines what will happen when characters cannot
276
 
        be encoded
277
 
            strict - abort if we cannot decode
278
 
            replace - put in a bogus character (typically '?')
279
 
            exact - do not encode sys.stdout
280
 
 
281
 
            NOTE: by default on Windows, sys.stdout is opened as a text
282
 
            stream, therefore LF line-endings are converted to CRLF.
283
 
            When a command uses encoding_type = 'exact', then
284
 
            sys.stdout is forced to be a binary stream, and line-endings
285
 
            will not mangled.
286
 
 
287
 
    :cvar hooks: An instance of CommandHooks.
288
179
    """
289
180
    aliases = []
290
181
    takes_args = []
291
182
    takes_options = []
292
 
    encoding_type = 'strict'
293
183
 
294
184
    hidden = False
295
 
 
 
185
    
296
186
    def __init__(self):
297
187
        """Construct an instance of this command."""
298
188
        if self.__doc__ == Command.__doc__:
299
189
            warn("No help message set for %r" % self)
300
 
        # List of standard options directly supported
301
 
        self.supported_std_options = []
302
 
 
303
 
    def _maybe_expand_globs(self, file_list):
304
 
        """Glob expand file_list if the platform does not do that itself.
305
 
 
306
 
        :return: A possibly empty list of unicode paths.
307
 
 
308
 
        Introduced in bzrlib 0.18.
309
 
        """
310
 
        if not file_list:
311
 
            file_list = []
312
 
        if sys.platform == 'win32':
313
 
            file_list = win32utils.glob_expand(file_list)
314
 
        return list(file_list)
315
 
 
316
 
    def _usage(self):
317
 
        """Return single-line grammar for this command.
318
 
 
319
 
        Only describes arguments, not options.
320
 
        """
321
 
        s = 'bzr ' + self.name() + ' '
322
 
        for aname in self.takes_args:
323
 
            aname = aname.upper()
324
 
            if aname[-1] in ['$', '+']:
325
 
                aname = aname[:-1] + '...'
326
 
            elif aname[-1] == '?':
327
 
                aname = '[' + aname[:-1] + ']'
328
 
            elif aname[-1] == '*':
329
 
                aname = '[' + aname[:-1] + '...]'
330
 
            s += aname + ' '
331
 
        s = s[:-1]      # remove last space
332
 
        return s
333
 
 
334
 
    def get_help_text(self, additional_see_also=None, plain=True,
335
 
                      see_also_as_links=False, verbose=True):
336
 
        """Return a text string with help for this command.
337
 
 
338
 
        :param additional_see_also: Additional help topics to be
339
 
            cross-referenced.
340
 
        :param plain: if False, raw help (reStructuredText) is
341
 
            returned instead of plain text.
342
 
        :param see_also_as_links: if True, convert items in 'See also'
343
 
            list to internal links (used by bzr_man rstx generator)
344
 
        :param verbose: if True, display the full help, otherwise
345
 
            leave out the descriptive sections and just display
346
 
            usage help (e.g. Purpose, Usage, Options) with a
347
 
            message explaining how to obtain full help.
348
 
        """
349
 
        doc = self.help()
350
 
        if doc is None:
351
 
            raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
352
 
 
353
 
        # Extract the summary (purpose) and sections out from the text
354
 
        purpose,sections,order = self._get_help_parts(doc)
355
 
 
356
 
        # If a custom usage section was provided, use it
357
 
        if sections.has_key('Usage'):
358
 
            usage = sections.pop('Usage')
359
 
        else:
360
 
            usage = self._usage()
361
 
 
362
 
        # The header is the purpose and usage
363
 
        result = ""
364
 
        result += ':Purpose: %s\n' % purpose
365
 
        if usage.find('\n') >= 0:
366
 
            result += ':Usage:\n%s\n' % usage
367
 
        else:
368
 
            result += ':Usage:   %s\n' % usage
369
 
        result += '\n'
370
 
 
371
 
        # Add the options
372
 
        options = option.get_optparser(self.options()).format_option_help()
373
 
        if options.startswith('Options:'):
374
 
            result += ':' + options
375
 
        elif options.startswith('options:'):
376
 
            # Python 2.4 version of optparse
377
 
            result += ':Options:' + options[len('options:'):]
378
 
        else:
379
 
            result += options
380
 
        result += '\n'
381
 
 
382
 
        if verbose:
383
 
            # Add the description, indenting it 2 spaces
384
 
            # to match the indentation of the options
385
 
            if sections.has_key(None):
386
 
                text = sections.pop(None)
387
 
                text = '\n  '.join(text.splitlines())
388
 
                result += ':%s:\n  %s\n\n' % ('Description',text)
389
 
 
390
 
            # Add the custom sections (e.g. Examples). Note that there's no need
391
 
            # to indent these as they must be indented already in the source.
392
 
            if sections:
393
 
                for label in order:
394
 
                    if sections.has_key(label):
395
 
                        result += ':%s:\n%s\n' % (label,sections[label])
396
 
                result += '\n'
397
 
        else:
398
 
            result += ("See bzr help %s for more details and examples.\n\n"
399
 
                % self.name())
400
 
 
401
 
        # Add the aliases, source (plug-in) and see also links, if any
402
 
        if self.aliases:
403
 
            result += ':Aliases:  '
404
 
            result += ', '.join(self.aliases) + '\n'
405
 
        plugin_name = self.plugin_name()
406
 
        if plugin_name is not None:
407
 
            result += ':From:     plugin "%s"\n' % plugin_name
408
 
        see_also = self.get_see_also(additional_see_also)
409
 
        if see_also:
410
 
            if not plain and see_also_as_links:
411
 
                see_also_links = []
412
 
                for item in see_also:
413
 
                    if item == 'topics':
414
 
                        # topics doesn't have an independent section
415
 
                        # so don't create a real link
416
 
                        see_also_links.append(item)
417
 
                    else:
418
 
                        # Use a reST link for this entry
419
 
                        see_also_links.append("`%s`_" % (item,))
420
 
                see_also = see_also_links
421
 
            result += ':See also: '
422
 
            result += ', '.join(see_also) + '\n'
423
 
 
424
 
        # If this will be rendered as plain text, convert it
425
 
        if plain:
426
 
            import bzrlib.help_topics
427
 
            result = bzrlib.help_topics.help_as_plain_text(result)
428
 
        return result
429
 
 
430
 
    @staticmethod
431
 
    def _get_help_parts(text):
432
 
        """Split help text into a summary and named sections.
433
 
 
434
 
        :return: (summary,sections,order) where summary is the top line and
435
 
            sections is a dictionary of the rest indexed by section name.
436
 
            order is the order the section appear in the text.
437
 
            A section starts with a heading line of the form ":xxx:".
438
 
            Indented text on following lines is the section value.
439
 
            All text found outside a named section is assigned to the
440
 
            default section which is given the key of None.
441
 
        """
442
 
        def save_section(sections, order, label, section):
443
 
            if len(section) > 0:
444
 
                if sections.has_key(label):
445
 
                    sections[label] += '\n' + section
446
 
                else:
447
 
                    order.append(label)
448
 
                    sections[label] = section
449
 
 
450
 
        lines = text.rstrip().splitlines()
451
 
        summary = lines.pop(0)
452
 
        sections = {}
453
 
        order = []
454
 
        label,section = None,''
455
 
        for line in lines:
456
 
            if line.startswith(':') and line.endswith(':') and len(line) > 2:
457
 
                save_section(sections, order, label, section)
458
 
                label,section = line[1:-1],''
459
 
            elif (label is not None) and len(line) > 1 and not line[0].isspace():
460
 
                save_section(sections, order, label, section)
461
 
                label,section = None,line
462
 
            else:
463
 
                if len(section) > 0:
464
 
                    section += '\n' + line
465
 
                else:
466
 
                    section = line
467
 
        save_section(sections, order, label, section)
468
 
        return summary, sections, order
469
 
 
470
 
    def get_help_topic(self):
471
 
        """Return the commands help topic - its name."""
472
 
        return self.name()
473
 
 
474
 
    def get_see_also(self, additional_terms=None):
475
 
        """Return a list of help topics that are related to this command.
476
 
 
477
 
        The list is derived from the content of the _see_also attribute. Any
478
 
        duplicates are removed and the result is in lexical order.
479
 
        :param additional_terms: Additional help topics to cross-reference.
480
 
        :return: A list of help topics.
481
 
        """
482
 
        see_also = set(getattr(self, '_see_also', []))
483
 
        if additional_terms:
484
 
            see_also.update(additional_terms)
485
 
        return sorted(see_also)
486
190
 
487
191
    def options(self):
488
192
        """Return dict of valid options for this command.
489
193
 
490
194
        Maps from long option name to option object."""
491
 
        r = Option.STD_OPTIONS.copy()
492
 
        std_names = r.keys()
 
195
        r = dict()
 
196
        r['help'] = Option.OPTIONS['help']
493
197
        for o in self.takes_options:
494
 
            if isinstance(o, basestring):
495
 
                o = option.Option.OPTIONS[o]
 
198
            if not isinstance(o, Option):
 
199
                o = Option.OPTIONS[o]
496
200
            r[o.name] = o
497
 
            if o.name in std_names:
498
 
                self.supported_std_options.append(o.name)
499
201
        return r
500
202
 
501
 
    def _setup_outf(self):
502
 
        """Return a file linked to stdout, which has proper encoding."""
503
 
        # Originally I was using self.stdout, but that looks
504
 
        # *way* too much like sys.stdout
505
 
        if self.encoding_type == 'exact':
506
 
            # force sys.stdout to be binary stream on win32
507
 
            if sys.platform == 'win32':
508
 
                fileno = getattr(sys.stdout, 'fileno', None)
509
 
                if fileno:
510
 
                    import msvcrt
511
 
                    msvcrt.setmode(fileno(), os.O_BINARY)
512
 
            self.outf = sys.stdout
513
 
            return
514
 
 
515
 
        output_encoding = osutils.get_terminal_encoding()
516
 
 
517
 
        self.outf = codecs.getwriter(output_encoding)(sys.stdout,
518
 
                        errors=self.encoding_type)
519
 
        # For whatever reason codecs.getwriter() does not advertise its encoding
520
 
        # it just returns the encoding of the wrapped file, which is completely
521
 
        # bogus. So set the attribute, so we can find the correct encoding later.
522
 
        self.outf.encoding = output_encoding
523
 
 
524
 
    def run_argv_aliases(self, argv, alias_argv=None):
525
 
        """Parse the command line and run with extra aliases in alias_argv."""
526
 
        if argv is None:
527
 
            warn("Passing None for [] is deprecated from bzrlib 0.10",
528
 
                 DeprecationWarning, stacklevel=2)
529
 
            argv = []
530
 
        args, opts = parse_args(self, argv, alias_argv)
531
 
 
532
 
        # Process the standard options
 
203
    def run_argv(self, argv):
 
204
        """Parse command line and run."""
 
205
        args, opts = parse_args(self, argv)
533
206
        if 'help' in opts:  # e.g. bzr add --help
534
 
            sys.stdout.write(self.get_help_text())
535
 
            return 0
536
 
        if 'usage' in opts:  # e.g. bzr add --usage
537
 
            sys.stdout.write(self.get_help_text(verbose=False))
538
 
            return 0
539
 
        trace.set_verbosity_level(option._verbosity_level)
540
 
        if 'verbose' in self.supported_std_options:
541
 
            opts['verbose'] = trace.is_verbose()
542
 
        elif opts.has_key('verbose'):
543
 
            del opts['verbose']
544
 
        if 'quiet' in self.supported_std_options:
545
 
            opts['quiet'] = trace.is_quiet()
546
 
        elif opts.has_key('quiet'):
547
 
            del opts['quiet']
548
 
 
 
207
            from bzrlib.help import help_on_command
 
208
            help_on_command(self.name())
 
209
            return 0
 
210
        # XXX: This should be handled by the parser
 
211
        allowed_names = self.options().keys()
 
212
        for oname in opts:
 
213
            if oname not in allowed_names:
 
214
                raise BzrCommandError("option '--%s' is not allowed for command %r"
 
215
                                      % (oname, self.name()))
549
216
        # mix arguments and options into one dictionary
550
217
        cmdargs = _match_argform(self.name(), self.takes_args, args)
551
218
        cmdopts = {}
555
222
        all_cmd_args = cmdargs.copy()
556
223
        all_cmd_args.update(cmdopts)
557
224
 
558
 
        self._setup_outf()
559
 
 
560
225
        return self.run(**all_cmd_args)
561
 
 
 
226
    
562
227
    def run(self):
563
228
        """Actually run the command.
564
229
 
569
234
        shell error code if not.  It's OK for this method to allow
570
235
        an exception to raise up.
571
236
        """
572
 
        raise NotImplementedError('no implementation of command %r'
573
 
                                  % self.name())
 
237
        raise NotImplementedError()
 
238
 
574
239
 
575
240
    def help(self):
576
241
        """Return help message for this class."""
577
 
        from inspect import getdoc
578
242
        if self.__doc__ is Command.__doc__:
579
243
            return None
580
244
        return getdoc(self)
582
246
    def name(self):
583
247
        return _unsquish_command_name(self.__class__.__name__)
584
248
 
585
 
    def plugin_name(self):
586
 
        """Get the name of the plugin that provides this command.
587
249
 
588
 
        :return: The name of the plugin or None if the command is builtin.
589
 
        """
590
 
        mod_parts = self.__module__.split('.')
591
 
        if len(mod_parts) >= 3 and mod_parts[1] == 'plugins':
592
 
            return mod_parts[2]
 
250
def parse_spec(spec):
 
251
    """
 
252
    >>> parse_spec(None)
 
253
    [None, None]
 
254
    >>> parse_spec("./")
 
255
    ['./', None]
 
256
    >>> parse_spec("../@")
 
257
    ['..', -1]
 
258
    >>> parse_spec("../f/@35")
 
259
    ['../f', 35]
 
260
    >>> parse_spec('./@revid:john@arbash-meinel.com-20050711044610-3ca0327c6a222f67')
 
261
    ['.', 'revid:john@arbash-meinel.com-20050711044610-3ca0327c6a222f67']
 
262
    """
 
263
    if spec is None:
 
264
        return [None, None]
 
265
    if '/@' in spec:
 
266
        parsed = spec.split('/@')
 
267
        assert len(parsed) == 2
 
268
        if parsed[1] == "":
 
269
            parsed[1] = -1
593
270
        else:
594
 
            return None
595
 
 
596
 
 
597
 
class CommandHooks(Hooks):
598
 
    """Hooks related to Command object creation/enumeration."""
599
 
 
600
 
    def __init__(self):
601
 
        """Create the default hooks.
602
 
 
603
 
        These are all empty initially, because by default nothing should get
604
 
        notified.
605
 
        """
606
 
        Hooks.__init__(self)
607
 
        self.create_hook(HookPoint('extend_command',
608
 
            "Called after creating a command object to allow modifications "
609
 
            "such as adding or removing options, docs etc. Called with the "
610
 
            "new bzrlib.commands.Command object.", (1, 13), None))
611
 
 
612
 
Command.hooks = CommandHooks()
613
 
 
614
 
 
615
 
def parse_args(command, argv, alias_argv=None):
 
271
            try:
 
272
                parsed[1] = int(parsed[1])
 
273
            except ValueError:
 
274
                pass # We can allow stuff like ./@revid:blahblahblah
 
275
            else:
 
276
                assert parsed[1] >=0
 
277
    else:
 
278
        parsed = [spec, None]
 
279
    return parsed
 
280
 
 
281
def parse_args(command, argv):
616
282
    """Parse command line.
617
 
 
 
283
    
618
284
    Arguments and options are parsed at this level before being passed
619
285
    down to specific command handlers.  This routine knows, from a
620
286
    lookup table, something about the available options, what optargs
621
287
    they take, and which commands will accept them.
622
288
    """
623
 
    # TODO: make it a method of the Command?
624
 
    parser = option.get_optparser(command.options())
625
 
    if alias_argv is not None:
626
 
        args = alias_argv + argv
627
 
    else:
628
 
        args = argv
629
 
 
630
 
    options, args = parser.parse_args(args)
631
 
    opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
632
 
                 v is not option.OptionParser.DEFAULT_VALUE])
 
289
    # TODO: chop up this beast; make it a method of the Command
 
290
    args = []
 
291
    opts = {}
 
292
 
 
293
    cmd_options = command.options()
 
294
    argsover = False
 
295
    while argv:
 
296
        a = argv.pop(0)
 
297
        if argsover:
 
298
            args.append(a)
 
299
            continue
 
300
        elif a == '--':
 
301
            # We've received a standalone -- No more flags
 
302
            argsover = True
 
303
            continue
 
304
        if a[0] == '-':
 
305
            # option names must not be unicode
 
306
            a = str(a)
 
307
            optarg = None
 
308
            if a[1] == '-':
 
309
                mutter("  got option %r" % a)
 
310
                if '=' in a:
 
311
                    optname, optarg = a[2:].split('=', 1)
 
312
                else:
 
313
                    optname = a[2:]
 
314
                if optname not in cmd_options:
 
315
                    raise BzrOptionError('unknown long option %r for command %s'
 
316
                        % (a, command.name()))
 
317
            else:
 
318
                shortopt = a[1:]
 
319
                if shortopt in Option.SHORT_OPTIONS:
 
320
                    # Multi-character options must have a space to delimit
 
321
                    # their value
 
322
                    # ^^^ what does this mean? mbp 20051014
 
323
                    optname = Option.SHORT_OPTIONS[shortopt].name
 
324
                else:
 
325
                    # Single character short options, can be chained,
 
326
                    # and have their value appended to their name
 
327
                    shortopt = a[1:2]
 
328
                    if shortopt not in Option.SHORT_OPTIONS:
 
329
                        # We didn't find the multi-character name, and we
 
330
                        # didn't find the single char name
 
331
                        raise BzrError('unknown short option %r' % a)
 
332
                    optname = Option.SHORT_OPTIONS[shortopt].name
 
333
 
 
334
                    if a[2:]:
 
335
                        # There are extra things on this option
 
336
                        # see if it is the value, or if it is another
 
337
                        # short option
 
338
                        optargfn = Option.OPTIONS[optname].type
 
339
                        if optargfn is None:
 
340
                            # This option does not take an argument, so the
 
341
                            # next entry is another short option, pack it back
 
342
                            # into the list
 
343
                            argv.insert(0, '-' + a[2:])
 
344
                        else:
 
345
                            # This option takes an argument, so pack it
 
346
                            # into the array
 
347
                            optarg = a[2:]
 
348
            
 
349
            if optname in opts:
 
350
                # XXX: Do we ever want to support this, e.g. for -r?
 
351
                raise BzrError('repeated option %r' % a)
 
352
                
 
353
            option_obj = cmd_options[optname]
 
354
            optargfn = option_obj.type
 
355
            if optargfn:
 
356
                if optarg == None:
 
357
                    if not argv:
 
358
                        raise BzrError('option %r needs an argument' % a)
 
359
                    else:
 
360
                        optarg = argv.pop(0)
 
361
                opts[optname] = optargfn(optarg)
 
362
            else:
 
363
                if optarg != None:
 
364
                    raise BzrError('option %r takes no argument' % optname)
 
365
                opts[optname] = True
 
366
        else:
 
367
            args.append(a)
633
368
    return args, opts
634
369
 
635
370
 
650
385
                argdict[argname + '_list'] = None
651
386
        elif ap[-1] == '+':
652
387
            if not args:
653
 
                raise errors.BzrCommandError("command %r needs one or more %s"
654
 
                                             % (cmd, argname.upper()))
 
388
                raise BzrCommandError("command %r needs one or more %s"
 
389
                        % (cmd, argname.upper()))
655
390
            else:
656
391
                argdict[argname + '_list'] = args[:]
657
392
                args = []
658
393
        elif ap[-1] == '$': # all but one
659
394
            if len(args) < 2:
660
 
                raise errors.BzrCommandError("command %r needs one or more %s"
661
 
                                             % (cmd, argname.upper()))
 
395
                raise BzrCommandError("command %r needs one or more %s"
 
396
                        % (cmd, argname.upper()))
662
397
            argdict[argname + '_list'] = args[:-1]
663
 
            args[:-1] = []
 
398
            args[:-1] = []                
664
399
        else:
665
400
            # just a plain arg
666
401
            argname = ap
667
402
            if not args:
668
 
                raise errors.BzrCommandError("command %r requires argument %s"
669
 
                               % (cmd, argname.upper()))
 
403
                raise BzrCommandError("command %r requires argument %s"
 
404
                        % (cmd, argname.upper()))
670
405
            else:
671
406
                argdict[argname] = args.pop(0)
672
 
 
 
407
            
673
408
    if args:
674
 
        raise errors.BzrCommandError("extra argument to command %s: %s"
675
 
                                     % (cmd, args[0]))
 
409
        raise BzrCommandError("extra argument to command %s: %s"
 
410
                              % (cmd, args[0]))
676
411
 
677
412
    return argdict
678
413
 
679
 
def apply_coveraged(dirname, the_callable, *args, **kwargs):
680
 
    # Cannot use "import trace", as that would import bzrlib.trace instead of
681
 
    # the standard library's trace.
682
 
    trace = __import__('trace')
683
 
 
684
 
    tracer = trace.Trace(count=1, trace=0)
685
 
    sys.settrace(tracer.globaltrace)
686
 
    threading.settrace(tracer.globaltrace)
687
 
 
688
 
    try:
689
 
        return exception_to_return_code(the_callable, *args, **kwargs)
690
 
    finally:
691
 
        sys.settrace(None)
692
 
        results = tracer.results()
693
 
        results.write_results(show_missing=1, summary=False,
694
 
                              coverdir=dirname)
695
414
 
696
415
 
697
416
def apply_profiled(the_callable, *args, **kwargs):
702
421
    try:
703
422
        prof = hotshot.Profile(pfname)
704
423
        try:
705
 
            ret = prof.runcall(exception_to_return_code, the_callable, *args,
706
 
                **kwargs) or 0
 
424
            ret = prof.runcall(the_callable, *args, **kwargs) or 0
707
425
        finally:
708
426
            prof.close()
709
427
        stats = hotshot.stats.load(pfname)
718
436
        os.remove(pfname)
719
437
 
720
438
 
721
 
def exception_to_return_code(the_callable, *args, **kwargs):
722
 
    """UI level helper for profiling and coverage.
723
 
 
724
 
    This transforms exceptions into a return value of 3. As such its only
725
 
    relevant to the UI layer, and should never be called where catching
726
 
    exceptions may be desirable.
727
 
    """
728
 
    try:
729
 
        return the_callable(*args, **kwargs)
730
 
    except (KeyboardInterrupt, Exception), e:
731
 
        # used to handle AssertionError and KeyboardInterrupt
732
 
        # specially here, but hopefully they're handled ok by the logger now
733
 
        exc_info = sys.exc_info()
734
 
        exitcode = trace.report_exception(exc_info, sys.stderr)
735
 
        if os.environ.get('BZR_PDB'):
736
 
            print '**** entering debugger'
737
 
            tb = exc_info[2]
738
 
            import pdb
739
 
            if sys.version_info[:2] < (2, 6):
740
 
                # XXX: we want to do
741
 
                #    pdb.post_mortem(tb)
742
 
                # but because pdb.post_mortem gives bad results for tracebacks
743
 
                # from inside generators, we do it manually.
744
 
                # (http://bugs.python.org/issue4150, fixed in Python 2.6)
745
 
 
746
 
                # Setup pdb on the traceback
747
 
                p = pdb.Pdb()
748
 
                p.reset()
749
 
                p.setup(tb.tb_frame, tb)
750
 
                # Point the debugger at the deepest frame of the stack
751
 
                p.curindex = len(p.stack) - 1
752
 
                p.curframe = p.stack[p.curindex][0]
753
 
                # Start the pdb prompt.
754
 
                p.print_stack_entry(p.stack[p.curindex])
755
 
                p.execRcLines()
756
 
                p.cmdloop()
757
 
            else:
758
 
                pdb.post_mortem(tb)
759
 
        return exitcode
760
 
 
761
 
 
762
 
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
763
 
    from bzrlib.lsprof import profile
764
 
    ret, stats = profile(exception_to_return_code, the_callable, *args, **kwargs)
765
 
    stats.sort()
766
 
    if filename is None:
767
 
        stats.pprint()
768
 
    else:
769
 
        stats.save(filename)
770
 
        trace.note('Profile data written to "%s".', filename)
771
 
    return ret
772
 
 
773
 
 
774
 
def shlex_split_unicode(unsplit):
775
 
    import shlex
776
 
    return [u.decode('utf-8') for u in shlex.split(unsplit.encode('utf-8'))]
777
 
 
778
 
 
779
 
def get_alias(cmd, config=None):
780
 
    """Return an expanded alias, or None if no alias exists.
781
 
 
782
 
    cmd
783
 
        Command to be checked for an alias.
784
 
    config
785
 
        Used to specify an alternative config to use,
786
 
        which is especially useful for testing.
787
 
        If it is unspecified, the global config will be used.
788
 
    """
789
 
    if config is None:
790
 
        import bzrlib.config
791
 
        config = bzrlib.config.GlobalConfig()
792
 
    alias = config.get_alias(cmd)
793
 
    if (alias):
794
 
        return shlex_split_unicode(alias)
795
 
    return None
796
 
 
797
 
 
798
439
def run_bzr(argv):
799
440
    """Execute a command.
800
441
 
 
442
    This is similar to main(), but without all the trappings for
 
443
    logging and error handling.  
 
444
    
801
445
    argv
802
446
       The command-line arguments, without the program name from argv[0]
803
 
       These should already be decoded. All library/test code calling
804
 
       run_bzr should be passing valid strings (don't need decoding).
805
 
 
 
447
    
806
448
    Returns a command status or raises an exception.
807
449
 
808
450
    Special master options: these must come before the command because
811
453
    --no-plugins
812
454
        Do not load plugin modules at all
813
455
 
814
 
    --no-aliases
815
 
        Do not allow aliases
816
 
 
817
456
    --builtin
818
457
        Only use builtin commands.  (Plugins are still allowed to change
819
458
        other behaviour.)
820
459
 
821
460
    --profile
822
 
        Run under the Python hotshot profiler.
823
 
 
824
 
    --lsprof
825
 
        Run under the Python lsprof profiler.
826
 
 
827
 
    --coverage
828
 
        Generate line coverage report in the specified directory.
 
461
        Run under the Python profiler.
829
462
    """
830
 
    argv = list(argv)
831
 
    trace.mutter("bzr arguments: %r", argv)
 
463
    argv = [a.decode(bzrlib.user_encoding) for a in argv]
832
464
 
833
 
    opt_lsprof = opt_profile = opt_no_plugins = opt_builtin =  \
834
 
                opt_no_aliases = False
835
 
    opt_lsprof_file = opt_coverage_dir = None
 
465
    opt_profile = opt_no_plugins = opt_builtin = False
836
466
 
837
467
    # --no-plugins is handled specially at a very early stage. We need
838
468
    # to load plugins before doing other command parsing so that they
839
469
    # can override commands, but this needs to happen first.
840
470
 
841
 
    argv_copy = []
842
 
    i = 0
843
 
    while i < len(argv):
844
 
        a = argv[i]
 
471
    for a in argv:
845
472
        if a == '--profile':
846
473
            opt_profile = True
847
 
        elif a == '--lsprof':
848
 
            opt_lsprof = True
849
 
        elif a == '--lsprof-file':
850
 
            opt_lsprof = True
851
 
            opt_lsprof_file = argv[i + 1]
852
 
            i += 1
853
474
        elif a == '--no-plugins':
854
475
            opt_no_plugins = True
855
 
        elif a == '--no-aliases':
856
 
            opt_no_aliases = True
857
476
        elif a == '--builtin':
858
477
            opt_builtin = True
859
 
        elif a == '--coverage':
860
 
            opt_coverage_dir = argv[i + 1]
861
 
            i += 1
862
 
        elif a.startswith('-D'):
863
 
            debug.debug_flags.add(a[2:])
864
 
        else:
865
 
            argv_copy.append(a)
866
 
        i += 1
867
 
 
868
 
    debug.set_debug_flags_from_config()
869
 
 
870
 
    argv = argv_copy
871
 
    if (not argv):
872
 
        from bzrlib.builtins import cmd_help
873
 
        cmd_help().run_argv_aliases([])
 
478
        else:
 
479
            break
 
480
        argv.remove(a)
 
481
 
 
482
    if (not argv) or (argv[0] == '--help'):
 
483
        from bzrlib.help import help
 
484
        if len(argv) > 1:
 
485
            help(argv[1])
 
486
        else:
 
487
            help()
874
488
        return 0
875
489
 
876
490
    if argv[0] == '--version':
877
 
        from bzrlib.builtins import cmd_version
878
 
        cmd_version().run_argv_aliases([])
 
491
        from bzrlib.builtins import show_version
 
492
        show_version()
879
493
        return 0
880
 
 
 
494
        
881
495
    if not opt_no_plugins:
882
496
        from bzrlib.plugin import load_plugins
883
497
        load_plugins()
884
 
    else:
885
 
        from bzrlib.plugin import disable_plugins
886
 
        disable_plugins()
887
 
 
888
 
    alias_argv = None
889
 
 
890
 
    if not opt_no_aliases:
891
 
        alias_argv = get_alias(argv[0])
892
 
        if alias_argv:
893
 
            user_encoding = osutils.get_user_encoding()
894
 
            alias_argv = [a.decode(user_encoding) for a in alias_argv]
895
 
            argv[0] = alias_argv.pop(0)
896
 
 
897
 
    cmd = argv.pop(0)
898
 
    # We want only 'ascii' command names, but the user may have typed
899
 
    # in a Unicode name. In that case, they should just get a
900
 
    # 'command not found' error later.
 
498
 
 
499
    cmd = str(argv.pop(0))
901
500
 
902
501
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
903
 
    run = cmd_obj.run_argv_aliases
904
 
    run_argv = [argv, alias_argv]
905
 
 
906
 
    try:
907
 
        # We can be called recursively (tests for example), but we don't want
908
 
        # the verbosity level to propagate.
909
 
        saved_verbosity_level = option._verbosity_level
910
 
        option._verbosity_level = 0
911
 
        if opt_lsprof:
912
 
            if opt_coverage_dir:
913
 
                trace.warning(
914
 
                    '--coverage ignored, because --lsprof is in use.')
915
 
            ret = apply_lsprofiled(opt_lsprof_file, run, *run_argv)
916
 
        elif opt_profile:
917
 
            if opt_coverage_dir:
918
 
                trace.warning(
919
 
                    '--coverage ignored, because --profile is in use.')
920
 
            ret = apply_profiled(run, *run_argv)
921
 
        elif opt_coverage_dir:
922
 
            ret = apply_coveraged(opt_coverage_dir, run, *run_argv)
923
 
        else:
924
 
            ret = run(*run_argv)
925
 
        if 'memory' in debug.debug_flags:
926
 
            trace.debug_memory('Process status after command:', short=False)
927
 
        return ret or 0
928
 
    finally:
929
 
        # reset, in case we may do other commands later within the same
930
 
        # process. Commands that want to execute sub-commands must propagate
931
 
        # --verbose in their own way.
932
 
        option._verbosity_level = saved_verbosity_level
933
 
 
 
502
 
 
503
    if opt_profile:
 
504
        ret = apply_profiled(cmd_obj.run_argv, argv)
 
505
    else:
 
506
        ret = cmd_obj.run_argv(argv)
 
507
    return ret or 0
934
508
 
935
509
def display_command(func):
936
 
    """Decorator that suppresses pipe/interrupt errors."""
937
510
    def ignore_pipe(*args, **kwargs):
938
511
        try:
939
 
            result = func(*args, **kwargs)
940
 
            sys.stdout.flush()
941
 
            return result
 
512
            return func(*args, **kwargs)
942
513
        except IOError, e:
943
 
            if getattr(e, 'errno', None) is None:
 
514
            if e.errno != errno.EPIPE:
944
515
                raise
945
 
            if e.errno != errno.EPIPE:
946
 
                # Win32 raises IOError with errno=0 on a broken pipe
947
 
                if sys.platform != 'win32' or (e.errno not in (0, errno.EINVAL)):
948
 
                    raise
949
 
            pass
950
516
        except KeyboardInterrupt:
951
517
            pass
952
518
    return ignore_pipe
953
519
 
954
 
 
955
520
def main(argv):
956
521
    import bzrlib.ui
957
 
    bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
958
 
        sys.stdin, sys.stdout, sys.stderr)
 
522
    bzrlib.trace.log_startup(argv)
 
523
    bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
959
524
 
960
 
    # Is this a final release version? If so, we should suppress warnings
961
 
    if bzrlib.version_info[3] == 'final':
962
 
        from bzrlib import symbol_versioning
963
 
        symbol_versioning.suppress_deprecation_warnings(override=False)
964
 
    try:
965
 
        user_encoding = osutils.get_user_encoding()
966
 
        argv = [a.decode(user_encoding) for a in argv[1:]]
967
 
    except UnicodeDecodeError:
968
 
        raise errors.BzrError(("Parameter '%r' is unsupported by the current "
969
 
                                                            "encoding." % a))
970
 
    ret = run_bzr_catch_errors(argv)
971
 
    trace.mutter("return code %d", ret)
972
 
    return ret
 
525
    return run_bzr_catch_errors(argv[1:])
973
526
 
974
527
 
975
528
def run_bzr_catch_errors(argv):
976
 
    """Run a bzr command with parameters as described by argv.
977
 
 
978
 
    This function assumed that that UI layer is setup, that symbol deprecations
979
 
    are already applied, and that unicode decoding has already been performed on argv.
980
 
    """
981
 
    return exception_to_return_code(run_bzr, argv)
982
 
 
983
 
 
984
 
def run_bzr_catch_user_errors(argv):
985
 
    """Run bzr and report user errors, but let internal errors propagate.
986
 
 
987
 
    This is used for the test suite, and might be useful for other programs
988
 
    that want to wrap the commandline interface.
989
 
    """
990
529
    try:
991
 
        return run_bzr(argv)
 
530
        try:
 
531
            return run_bzr(argv)
 
532
        finally:
 
533
            # do this here inside the exception wrappers to catch EPIPE
 
534
            sys.stdout.flush()
 
535
    except BzrCommandError, e:
 
536
        # command line syntax error, etc
 
537
        log_error(str(e))
 
538
        return 1
 
539
    except BzrError, e:
 
540
        bzrlib.trace.log_exception()
 
541
        return 1
 
542
    except AssertionError, e:
 
543
        bzrlib.trace.log_exception('assertion failed: ' + str(e))
 
544
        return 3
 
545
    except KeyboardInterrupt, e:
 
546
        bzrlib.trace.log_exception('interrupted')
 
547
        return 2
992
548
    except Exception, e:
993
 
        if (isinstance(e, (OSError, IOError))
994
 
            or not getattr(e, 'internal_error', True)):
995
 
            trace.report_exception(sys.exc_info(), sys.stderr)
996
 
            return 3
997
 
        else:
998
 
            raise
999
 
 
1000
 
 
1001
 
class HelpCommandIndex(object):
1002
 
    """A index for bzr help that returns commands."""
1003
 
 
1004
 
    def __init__(self):
1005
 
        self.prefix = 'commands/'
1006
 
 
1007
 
    def get_topics(self, topic):
1008
 
        """Search for topic amongst commands.
1009
 
 
1010
 
        :param topic: A topic to search for.
1011
 
        :return: A list which is either empty or contains a single
1012
 
            Command entry.
1013
 
        """
1014
 
        if topic and topic.startswith(self.prefix):
1015
 
            topic = topic[len(self.prefix):]
1016
 
        try:
1017
 
            cmd = _get_cmd_object(topic)
1018
 
        except KeyError:
1019
 
            return []
1020
 
        else:
1021
 
            return [cmd]
1022
 
 
1023
 
 
1024
 
class Provider(object):
1025
 
    '''Generic class to be overriden by plugins'''
1026
 
 
1027
 
    def plugin_for_command(self, cmd_name):
1028
 
        '''Takes a command and returns the information for that plugin
1029
 
 
1030
 
        :return: A dictionary with all the available information
1031
 
        for the requested plugin
1032
 
        '''
1033
 
        raise NotImplementedError
1034
 
 
1035
 
 
1036
 
class ProvidersRegistry(registry.Registry):
1037
 
    '''This registry exists to allow other providers to exist'''
1038
 
 
1039
 
    def __iter__(self):
1040
 
        for key, provider in self.iteritems():
1041
 
            yield provider
1042
 
 
1043
 
command_providers_registry = ProvidersRegistry()
1044
 
 
 
549
        import errno
 
550
        if (isinstance(e, IOError) 
 
551
            and hasattr(e, 'errno')
 
552
            and e.errno == errno.EPIPE):
 
553
            bzrlib.trace.note('broken pipe')
 
554
            return 2
 
555
        else:
 
556
            ## import pdb
 
557
            ## pdb.pm()
 
558
            bzrlib.trace.log_exception()
 
559
            return 2
1045
560
 
1046
561
if __name__ == '__main__':
1047
562
    sys.exit(main(sys.argv))