~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: John Arbash Meinel
  • Date: 2010-09-25 20:08:01 UTC
  • mfrom: (5444 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5445.
  • Revision ID: john@arbash-meinel.com-20100925200801-7uf0ux3uwxo9i3x0
Merge bzr.dev 5444 to resolve some small text conflicts.

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
273
276
    # Allow plugins to extend commands
274
277
    for hook in Command.hooks['extend_command']:
275
278
        hook(cmd)
276
 
    if getattr(cmd, 'invoked_as', None) is None:
277
 
        cmd.invoked_as = cmd_name
278
279
    return cmd
279
280
 
280
281
 
396
397
            sys.stdout is forced to be a binary stream, and line-endings
397
398
            will not mangled.
398
399
 
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
400
    :cvar hooks: An instance of CommandHooks.
405
 
 
406
401
    :ivar __doc__: The help shown by 'bzr help command' for this command.
407
402
        This is set by assigning explicitly to __doc__ so that -OO can
408
403
        be used::
414
409
    takes_args = []
415
410
    takes_options = []
416
411
    encoding_type = 'strict'
417
 
    invoked_as = None
418
412
 
419
413
    hidden = False
420
414
 
693
687
 
694
688
        self._setup_outf()
695
689
 
696
 
        try:
697
 
            return self.run(**all_cmd_args)
698
 
        finally:
699
 
            # reset it, so that other commands run in the same process won't
700
 
            # inherit state. Before we reset it, log any activity, so that it
701
 
            # gets properly tracked.
702
 
            ui.ui_factory.log_transport_activity(
703
 
                display=('bytes' in debug.debug_flags))
704
 
            trace.set_verbosity_level(0)
 
690
        return self.run(**all_cmd_args)
705
691
 
706
692
    def _setup_run(self):
707
693
        """Wrap the defined run method on self with a cleanup.
758
744
        return getdoc(self)
759
745
 
760
746
    def name(self):
761
 
        """Return the canonical name for this command.
762
 
 
763
 
        The name under which it was actually invoked is available in invoked_as.
764
 
        """
765
747
        return _unsquish_command_name(self.__class__.__name__)
766
748
 
767
749
    def plugin_name(self):
785
767
        These are all empty initially, because by default nothing should get
786
768
        notified.
787
769
        """
788
 
        Hooks.__init__(self, "bzrlib.commands", "Command.hooks")
789
 
        self.add_hook('extend_command',
 
770
        Hooks.__init__(self)
 
771
        self.create_hook(HookPoint('extend_command',
790
772
            "Called after creating a command object to allow modifications "
791
773
            "such as adding or removing options, docs etc. Called with the "
792
 
            "new bzrlib.commands.Command object.", (1, 13))
793
 
        self.add_hook('get_command',
 
774
            "new bzrlib.commands.Command object.", (1, 13), None))
 
775
        self.create_hook(HookPoint('get_command',
794
776
            "Called when creating a single command. Called with "
795
777
            "(cmd_or_None, command_name). get_command should either return "
796
778
            "the cmd_or_None parameter, or a replacement Command object that "
797
779
            "should be used for the command. Note that the Command.hooks "
798
780
            "hooks are core infrastructure. Many users will prefer to use "
799
781
            "bzrlib.commands.register_command or plugin_cmds.register_lazy.",
800
 
            (1, 17))
801
 
        self.add_hook('get_missing_command',
 
782
            (1, 17), None))
 
783
        self.create_hook(HookPoint('get_missing_command',
802
784
            "Called when creating a single command if no command could be "
803
785
            "found. Called with (command_name). get_missing_command should "
804
786
            "either return None, or a Command object to be used for the "
805
 
            "command.", (1, 17))
806
 
        self.add_hook('list_commands',
 
787
            "command.", (1, 17), None))
 
788
        self.create_hook(HookPoint('list_commands',
807
789
            "Called when enumerating commands. Called with a set of "
808
790
            "cmd_name strings for all the commands found so far. This set "
809
791
            " is safe to mutate - e.g. to remove a command. "
810
792
            "list_commands should return the updated set of command names.",
811
 
            (1, 17))
 
793
            (1, 17), None))
812
794
 
813
795
Command.hooks = CommandHooks()
814
796
 
828
810
    else:
829
811
        args = argv
830
812
 
831
 
    # for python 2.5 and later, optparse raises this exception if a non-ascii
832
 
    # option name is given.  See http://bugs.python.org/issue2931
833
 
    try:
834
 
        options, args = parser.parse_args(args)
835
 
    except UnicodeEncodeError,e:
836
 
        raise errors.BzrCommandError('Only ASCII permitted in option names')
837
 
 
 
813
    options, args = parser.parse_args(args)
838
814
    opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
839
815
                 v is not option.OptionParser.DEFAULT_VALUE])
840
816
    return args, opts
1044
1020
        Specify the number of processes that can be run concurrently (selftest).
1045
1021
    """
1046
1022
    trace.mutter("bazaar version: " + bzrlib.__version__)
1047
 
    argv = _specified_or_unicode_argv(argv)
 
1023
    argv = list(argv)
1048
1024
    trace.mutter("bzr arguments: %r", argv)
1049
1025
 
1050
1026
    opt_lsprof = opt_profile = opt_no_plugins = opt_builtin =  \
1190
1166
        new_argv = []
1191
1167
        try:
1192
1168
            # ensure all arguments are unicode strings
1193
 
            for a in argv:
 
1169
            for a in argv[1:]:
1194
1170
                if isinstance(a, unicode):
1195
1171
                    new_argv.append(a)
1196
1172
                else:
1212
1188
 
1213
1189
    :return: exit code of bzr command.
1214
1190
    """
1215
 
    if argv is not None:
1216
 
        argv = argv[1:]
 
1191
    argv = _specified_or_unicode_argv(argv)
1217
1192
    _register_builtin_commands()
1218
1193
    ret = run_bzr_catch_errors(argv)
 
1194
    bzrlib.ui.ui_factory.log_transport_activity(
 
1195
        display=('bytes' in debug.debug_flags))
1219
1196
    trace.mutter("return code %d", ret)
1220
1197
    return ret
1221
1198