/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/commands.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-05-16 13:49:58 UTC
  • mfrom: (5851.2.2 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20110516134958-xkhcnr3lt1tc8ogt
(mbp) _format_version_tuple handles more different input patterns (Martin
 Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
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
27
27
 
28
28
from bzrlib.lazy_import import lazy_import
29
29
lazy_import(globals(), """
30
 
import codecs
31
30
import errno
32
31
import threading
33
 
from warnings import warn
34
32
 
35
33
import bzrlib
36
34
from bzrlib import (
42
40
    osutils,
43
41
    trace,
44
42
    ui,
45
 
    win32utils,
46
43
    )
47
44
""")
48
45
 
49
 
from bzrlib.hooks import HookPoint, Hooks
 
46
from bzrlib.hooks import Hooks
50
47
# Compatibility - Option used to be in commands.
51
48
from bzrlib.option import Option
52
49
from bzrlib.plugin import disable_plugins, load_plugins
222
219
    Use of all_command_names() is encouraged rather than builtin_command_names
223
220
    and/or plugin_command_names.
224
221
    """
 
222
    _register_builtin_commands()
225
223
    return builtin_command_registry.keys()
226
224
 
227
225
 
275
273
    # Allow plugins to extend commands
276
274
    for hook in Command.hooks['extend_command']:
277
275
        hook(cmd)
 
276
    if getattr(cmd, 'invoked_as', None) is None:
 
277
        cmd.invoked_as = cmd_name
278
278
    return cmd
279
279
 
280
280
 
396
396
            sys.stdout is forced to be a binary stream, and line-endings
397
397
            will not mangled.
398
398
 
 
399
    :ivar invoked_as:
 
400
        A string indicating the real name under which this command was
 
401
        invoked, before expansion of aliases. 
 
402
        (This may be None if the command was constructed and run in-process.)
 
403
 
399
404
    :cvar hooks: An instance of CommandHooks.
 
405
 
 
406
    :ivar __doc__: The help shown by 'bzr help command' for this command.
 
407
        This is set by assigning explicitly to __doc__ so that -OO can
 
408
        be used::
 
409
 
 
410
        class Foo(Command):
 
411
            __doc__ = "My help goes here"
400
412
    """
401
413
    aliases = []
402
414
    takes_args = []
403
415
    takes_options = []
404
416
    encoding_type = 'strict'
 
417
    invoked_as = None
405
418
 
406
419
    hidden = False
407
420
 
408
421
    def __init__(self):
409
422
        """Construct an instance of this command."""
410
 
        if self.__doc__ == Command.__doc__:
411
 
            warn("No help message set for %r" % self)
412
423
        # List of standard options directly supported
413
424
        self.supported_std_options = []
414
425
        self._setup_run()
482
493
            message explaining how to obtain full help.
483
494
        """
484
495
        doc = self.help()
485
 
        if doc is None:
486
 
            raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
 
496
        if not doc:
 
497
            doc = "No help for this command."
487
498
 
488
499
        # Extract the summary (purpose) and sections out from the text
489
500
        purpose,sections,order = self._get_help_parts(doc)
509
520
        # so we get <https://bugs.launchpad.net/bzr/+bug/249908>.  -- mbp
510
521
        # 20090319
511
522
        options = option.get_optparser(self.options()).format_option_help()
512
 
        # XXX: According to the spec, ReST option lists actually don't support 
513
 
        # options like --1.9 so that causes syntax errors (in Sphinx at least).
514
 
        # As that pattern always appears in the commands that break, we trap
515
 
        # on that and then format that block of 'format' options as a literal
516
 
        # block.
517
 
        if not plain and options.find('  --1.9  ') != -1:
 
523
        # FIXME: According to the spec, ReST option lists actually don't
 
524
        # support options like --1.14 so that causes syntax errors (in Sphinx
 
525
        # at least).  As that pattern always appears in the commands that
 
526
        # break, we trap on that and then format that block of 'format' options
 
527
        # as a literal block. We use the most recent format still listed so we
 
528
        # don't have to do that too often -- vila 20110514
 
529
        if not plain and options.find('  --1.14  ') != -1:
518
530
            options = options.replace(' format:\n', ' format::\n\n', 1)
519
531
        if options.startswith('Options:'):
520
532
            result += ':' + options
682
694
 
683
695
        self._setup_outf()
684
696
 
685
 
        return self.run(**all_cmd_args)
 
697
        try:
 
698
            return self.run(**all_cmd_args)
 
699
        finally:
 
700
            # reset it, so that other commands run in the same process won't
 
701
            # inherit state. Before we reset it, log any activity, so that it
 
702
            # gets properly tracked.
 
703
            ui.ui_factory.log_transport_activity(
 
704
                display=('bytes' in debug.debug_flags))
 
705
            trace.set_verbosity_level(0)
686
706
 
687
707
    def _setup_run(self):
688
708
        """Wrap the defined run method on self with a cleanup.
739
759
        return getdoc(self)
740
760
 
741
761
    def name(self):
 
762
        """Return the canonical name for this command.
 
763
 
 
764
        The name under which it was actually invoked is available in invoked_as.
 
765
        """
742
766
        return _unsquish_command_name(self.__class__.__name__)
743
767
 
744
768
    def plugin_name(self):
762
786
        These are all empty initially, because by default nothing should get
763
787
        notified.
764
788
        """
765
 
        Hooks.__init__(self)
766
 
        self.create_hook(HookPoint('extend_command',
 
789
        Hooks.__init__(self, "bzrlib.commands", "Command.hooks")
 
790
        self.add_hook('extend_command',
767
791
            "Called after creating a command object to allow modifications "
768
792
            "such as adding or removing options, docs etc. Called with the "
769
 
            "new bzrlib.commands.Command object.", (1, 13), None))
770
 
        self.create_hook(HookPoint('get_command',
 
793
            "new bzrlib.commands.Command object.", (1, 13))
 
794
        self.add_hook('get_command',
771
795
            "Called when creating a single command. Called with "
772
796
            "(cmd_or_None, command_name). get_command should either return "
773
797
            "the cmd_or_None parameter, or a replacement Command object that "
774
798
            "should be used for the command. Note that the Command.hooks "
775
799
            "hooks are core infrastructure. Many users will prefer to use "
776
800
            "bzrlib.commands.register_command or plugin_cmds.register_lazy.",
777
 
            (1, 17), None))
778
 
        self.create_hook(HookPoint('get_missing_command',
 
801
            (1, 17))
 
802
        self.add_hook('get_missing_command',
779
803
            "Called when creating a single command if no command could be "
780
804
            "found. Called with (command_name). get_missing_command should "
781
805
            "either return None, or a Command object to be used for the "
782
 
            "command.", (1, 17), None))
783
 
        self.create_hook(HookPoint('list_commands',
 
806
            "command.", (1, 17))
 
807
        self.add_hook('list_commands',
784
808
            "Called when enumerating commands. Called with a set of "
785
809
            "cmd_name strings for all the commands found so far. This set "
786
810
            " is safe to mutate - e.g. to remove a command. "
787
811
            "list_commands should return the updated set of command names.",
788
 
            (1, 17), None))
 
812
            (1, 17))
789
813
 
790
814
Command.hooks = CommandHooks()
791
815
 
805
829
    else:
806
830
        args = argv
807
831
 
808
 
    options, args = parser.parse_args(args)
 
832
    # for python 2.5 and later, optparse raises this exception if a non-ascii
 
833
    # option name is given.  See http://bugs.python.org/issue2931
 
834
    try:
 
835
        options, args = parser.parse_args(args)
 
836
    except UnicodeEncodeError,e:
 
837
        raise errors.BzrCommandError('Only ASCII permitted in option names')
 
838
 
809
839
    opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
810
840
                 v is not option.OptionParser.DEFAULT_VALUE])
811
841
    return args, opts
1015
1045
        Specify the number of processes that can be run concurrently (selftest).
1016
1046
    """
1017
1047
    trace.mutter("bazaar version: " + bzrlib.__version__)
1018
 
    argv = list(argv)
 
1048
    argv = _specified_or_unicode_argv(argv)
1019
1049
    trace.mutter("bzr arguments: %r", argv)
1020
1050
 
1021
1051
    opt_lsprof = opt_profile = opt_no_plugins = opt_builtin =  \
1050
1080
        elif a == '--coverage':
1051
1081
            opt_coverage_dir = argv[i + 1]
1052
1082
            i += 1
 
1083
        elif a == '--profile-imports':
 
1084
            pass # already handled in startup script Bug #588277
1053
1085
        elif a.startswith('-D'):
1054
1086
            debug.debug_flags.add(a[2:])
1055
1087
        else:
1077
1109
    if not opt_no_aliases:
1078
1110
        alias_argv = get_alias(argv[0])
1079
1111
        if alias_argv:
1080
 
            user_encoding = osutils.get_user_encoding()
1081
 
            alias_argv = [a.decode(user_encoding) for a in alias_argv]
1082
1112
            argv[0] = alias_argv.pop(0)
1083
1113
 
1084
1114
    cmd = argv.pop(0)
1085
 
    # We want only 'ascii' command names, but the user may have typed
1086
 
    # in a Unicode name. In that case, they should just get a
1087
 
    # 'command not found' error later.
1088
 
 
1089
1115
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
1090
1116
    run = cmd_obj.run_argv_aliases
1091
1117
    run_argv = [argv, alias_argv]
1165
1191
        new_argv = []
1166
1192
        try:
1167
1193
            # ensure all arguments are unicode strings
1168
 
            for a in argv[1:]:
 
1194
            for a in argv:
1169
1195
                if isinstance(a, unicode):
1170
1196
                    new_argv.append(a)
1171
1197
                else:
1187
1213
 
1188
1214
    :return: exit code of bzr command.
1189
1215
    """
1190
 
    argv = _specified_or_unicode_argv(argv)
 
1216
    if argv is not None:
 
1217
        argv = argv[1:]
1191
1218
    _register_builtin_commands()
1192
1219
    ret = run_bzr_catch_errors(argv)
1193
 
    bzrlib.ui.ui_factory.log_transport_activity(
1194
 
        display=('bytes' in debug.debug_flags))
1195
1220
    trace.mutter("return code %d", ret)
1196
1221
    return ret
1197
1222