/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
30
31
import errno
31
32
import threading
 
33
from warnings import warn
32
34
 
33
35
import bzrlib
34
36
from bzrlib import (
40
42
    osutils,
41
43
    trace,
42
44
    ui,
 
45
    win32utils,
43
46
    )
44
47
""")
45
48
 
46
 
from bzrlib.hooks import Hooks
 
49
from bzrlib.hooks import HookPoint, Hooks
47
50
# Compatibility - Option used to be in commands.
48
51
from bzrlib.option import Option
49
52
from bzrlib.plugin import disable_plugins, load_plugins
219
222
    Use of all_command_names() is encouraged rather than builtin_command_names
220
223
    and/or plugin_command_names.
221
224
    """
222
 
    _register_builtin_commands()
223
225
    return builtin_command_registry.keys()
224
226
 
225
227
 
273
275
    # Allow plugins to extend commands
274
276
    for hook in Command.hooks['extend_command']:
275
277
        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
 
 
404
399
    :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"
412
400
    """
413
401
    aliases = []
414
402
    takes_args = []
415
403
    takes_options = []
416
404
    encoding_type = 'strict'
417
 
    invoked_as = None
418
405
 
419
406
    hidden = False
420
407
 
421
408
    def __init__(self):
422
409
        """Construct an instance of this command."""
 
410
        if self.__doc__ == Command.__doc__:
 
411
            warn("No help message set for %r" % self)
423
412
        # List of standard options directly supported
424
413
        self.supported_std_options = []
425
414
        self._setup_run()
493
482
            message explaining how to obtain full help.
494
483
        """
495
484
        doc = self.help()
496
 
        if not doc:
497
 
            doc = "No help for this command."
 
485
        if doc is None:
 
486
            raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
498
487
 
499
488
        # Extract the summary (purpose) and sections out from the text
500
489
        purpose,sections,order = self._get_help_parts(doc)
520
509
        # so we get <https://bugs.launchpad.net/bzr/+bug/249908>.  -- mbp
521
510
        # 20090319
522
511
        options = option.get_optparser(self.options()).format_option_help()
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:
 
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:
530
518
            options = options.replace(' format:\n', ' format::\n\n', 1)
531
519
        if options.startswith('Options:'):
532
520
            result += ':' + options
694
682
 
695
683
        self._setup_outf()
696
684
 
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)
 
685
        return self.run(**all_cmd_args)
706
686
 
707
687
    def _setup_run(self):
708
688
        """Wrap the defined run method on self with a cleanup.
759
739
        return getdoc(self)
760
740
 
761
741
    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
 
        """
766
742
        return _unsquish_command_name(self.__class__.__name__)
767
743
 
768
744
    def plugin_name(self):
786
762
        These are all empty initially, because by default nothing should get
787
763
        notified.
788
764
        """
789
 
        Hooks.__init__(self, "bzrlib.commands", "Command.hooks")
790
 
        self.add_hook('extend_command',
 
765
        Hooks.__init__(self)
 
766
        self.create_hook(HookPoint('extend_command',
791
767
            "Called after creating a command object to allow modifications "
792
768
            "such as adding or removing options, docs etc. Called with the "
793
 
            "new bzrlib.commands.Command object.", (1, 13))
794
 
        self.add_hook('get_command',
 
769
            "new bzrlib.commands.Command object.", (1, 13), None))
 
770
        self.create_hook(HookPoint('get_command',
795
771
            "Called when creating a single command. Called with "
796
772
            "(cmd_or_None, command_name). get_command should either return "
797
773
            "the cmd_or_None parameter, or a replacement Command object that "
798
774
            "should be used for the command. Note that the Command.hooks "
799
775
            "hooks are core infrastructure. Many users will prefer to use "
800
776
            "bzrlib.commands.register_command or plugin_cmds.register_lazy.",
801
 
            (1, 17))
802
 
        self.add_hook('get_missing_command',
 
777
            (1, 17), None))
 
778
        self.create_hook(HookPoint('get_missing_command',
803
779
            "Called when creating a single command if no command could be "
804
780
            "found. Called with (command_name). get_missing_command should "
805
781
            "either return None, or a Command object to be used for the "
806
 
            "command.", (1, 17))
807
 
        self.add_hook('list_commands',
 
782
            "command.", (1, 17), None))
 
783
        self.create_hook(HookPoint('list_commands',
808
784
            "Called when enumerating commands. Called with a set of "
809
785
            "cmd_name strings for all the commands found so far. This set "
810
786
            " is safe to mutate - e.g. to remove a command. "
811
787
            "list_commands should return the updated set of command names.",
812
 
            (1, 17))
 
788
            (1, 17), None))
813
789
 
814
790
Command.hooks = CommandHooks()
815
791
 
829
805
    else:
830
806
        args = argv
831
807
 
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
 
 
 
808
    options, args = parser.parse_args(args)
839
809
    opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
840
810
                 v is not option.OptionParser.DEFAULT_VALUE])
841
811
    return args, opts
1045
1015
        Specify the number of processes that can be run concurrently (selftest).
1046
1016
    """
1047
1017
    trace.mutter("bazaar version: " + bzrlib.__version__)
1048
 
    argv = _specified_or_unicode_argv(argv)
 
1018
    argv = list(argv)
1049
1019
    trace.mutter("bzr arguments: %r", argv)
1050
1020
 
1051
1021
    opt_lsprof = opt_profile = opt_no_plugins = opt_builtin =  \
1080
1050
        elif a == '--coverage':
1081
1051
            opt_coverage_dir = argv[i + 1]
1082
1052
            i += 1
1083
 
        elif a == '--profile-imports':
1084
 
            pass # already handled in startup script Bug #588277
1085
1053
        elif a.startswith('-D'):
1086
1054
            debug.debug_flags.add(a[2:])
1087
1055
        else:
1109
1077
    if not opt_no_aliases:
1110
1078
        alias_argv = get_alias(argv[0])
1111
1079
        if alias_argv:
 
1080
            user_encoding = osutils.get_user_encoding()
 
1081
            alias_argv = [a.decode(user_encoding) for a in alias_argv]
1112
1082
            argv[0] = alias_argv.pop(0)
1113
1083
 
1114
1084
    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
 
1115
1089
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
1116
1090
    run = cmd_obj.run_argv_aliases
1117
1091
    run_argv = [argv, alias_argv]
1191
1165
        new_argv = []
1192
1166
        try:
1193
1167
            # ensure all arguments are unicode strings
1194
 
            for a in argv:
 
1168
            for a in argv[1:]:
1195
1169
                if isinstance(a, unicode):
1196
1170
                    new_argv.append(a)
1197
1171
                else:
1213
1187
 
1214
1188
    :return: exit code of bzr command.
1215
1189
    """
1216
 
    if argv is not None:
1217
 
        argv = argv[1:]
 
1190
    argv = _specified_or_unicode_argv(argv)
1218
1191
    _register_builtin_commands()
1219
1192
    ret = run_bzr_catch_errors(argv)
 
1193
    bzrlib.ui.ui_factory.log_transport_activity(
 
1194
        display=('bytes' in debug.debug_flags))
1220
1195
    trace.mutter("return code %d", ret)
1221
1196
    return ret
1222
1197