~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2010-02-25 06:17:27 UTC
  • mfrom: (5055 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5057.
  • Revision ID: mbp@sourcefrog.net-20100225061727-4sd9lt0qmdc6087t
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008, 2009 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
41
41
import bzrlib
42
42
from bzrlib import (
43
43
    cleanup,
 
44
    cmdline,
44
45
    debug,
45
46
    errors,
46
47
    option,
54
55
from bzrlib.hooks import HookPoint, Hooks
55
56
# Compatibility - Option used to be in commands.
56
57
from bzrlib.option import Option
 
58
from bzrlib.plugin import disable_plugins, load_plugins
57
59
from bzrlib import registry
58
60
from bzrlib.symbol_versioning import (
59
61
    deprecated_function,
60
62
    deprecated_in,
61
63
    deprecated_method,
62
 
    suppress_deprecation_warnings,
63
64
    )
64
65
 
65
66
 
186
187
    return plugin_cmds.keys()
187
188
 
188
189
 
189
 
@deprecated_function(deprecated_in((1, 17, 0)))
190
 
def get_all_cmds(plugins_override=False):
191
 
    """Return canonical name and class for most commands.
192
 
    
193
 
    NB: This does not return all commands since the introduction of
194
 
    command hooks, and returning the class is not sufficient to 
195
 
    get correctly setup commands, which is why it is deprecated.
196
 
 
197
 
    Use 'all_command_names' + 'get_cmd_object' instead.
198
 
    """
199
 
    d = _builtin_commands()
200
 
    if plugins_override:
201
 
        d.update(plugin_cmds.iteritems())
202
 
    for k, v in d.iteritems():
203
 
        yield k,v
204
 
 
205
 
 
206
190
def get_cmd_object(cmd_name, plugins_override=True):
207
191
    """Return the command object for a command.
208
192
 
624
608
 
625
609
    def run_argv_aliases(self, argv, alias_argv=None):
626
610
        """Parse the command line and run with extra aliases in alias_argv."""
627
 
        if argv is None:
628
 
            warn("Passing None for [] is deprecated from bzrlib 0.10",
629
 
                 DeprecationWarning, stacklevel=2)
630
 
            argv = []
631
611
        args, opts = parse_args(self, argv, alias_argv)
632
612
 
633
613
        # Process the standard options
895
875
    return ret
896
876
 
897
877
 
898
 
def shlex_split_unicode(unsplit):
899
 
    import shlex
900
 
    return [u.decode('utf-8') for u in shlex.split(unsplit.encode('utf-8'))]
901
 
 
902
 
 
903
878
def get_alias(cmd, config=None):
904
879
    """Return an expanded alias, or None if no alias exists.
905
880
 
915
890
        config = bzrlib.config.GlobalConfig()
916
891
    alias = config.get_alias(cmd)
917
892
    if (alias):
918
 
        return shlex_split_unicode(alias)
 
893
        return cmdline.split(alias)
919
894
    return None
920
895
 
921
896
 
922
 
def run_bzr(argv):
 
897
def run_bzr(argv, load_plugins=load_plugins, disable_plugins=disable_plugins):
923
898
    """Execute a command.
924
899
 
925
 
    argv
926
 
       The command-line arguments, without the program name from argv[0]
927
 
       These should already be decoded. All library/test code calling
928
 
       run_bzr should be passing valid strings (don't need decoding).
929
 
 
930
 
    Returns a command status or raises an exception.
 
900
    :param argv: The command-line arguments, without the program name from
 
901
        argv[0] These should already be decoded. All library/test code calling
 
902
        run_bzr should be passing valid strings (don't need decoding).
 
903
    :param load_plugins: What function to call when triggering plugin loading.
 
904
        This function should take no arguments and cause all plugins to be
 
905
        loaded.
 
906
    :param disable_plugins: What function to call when disabling plugin
 
907
        loading. This function should take no arguments and cause all plugin
 
908
        loading to be prohibited (so that code paths in your application that
 
909
        know about some plugins possibly being present will fail to import
 
910
        those plugins even if they are installed.)
 
911
    :return: Returns a command exit code or raises an exception.
931
912
 
932
913
    Special master options: these must come before the command because
933
914
    they control how the command is interpreted.
998
979
 
999
980
    debug.set_debug_flags_from_config()
1000
981
 
 
982
    if not opt_no_plugins:
 
983
        load_plugins()
 
984
    else:
 
985
        disable_plugins()
 
986
 
1001
987
    argv = argv_copy
1002
988
    if (not argv):
1003
 
        from bzrlib.builtins import cmd_help
1004
 
        cmd_help().run_argv_aliases([])
 
989
        get_cmd_object('help').run_argv_aliases([])
1005
990
        return 0
1006
991
 
1007
992
    if argv[0] == '--version':
1008
 
        from bzrlib.builtins import cmd_version
1009
 
        cmd_version().run_argv_aliases([])
 
993
        get_cmd_object('version').run_argv_aliases([])
1010
994
        return 0
1011
995
 
1012
 
    if not opt_no_plugins:
1013
 
        from bzrlib.plugin import load_plugins
1014
 
        load_plugins()
1015
 
    else:
1016
 
        from bzrlib.plugin import disable_plugins
1017
 
        disable_plugins()
1018
 
 
1019
996
    alias_argv = None
1020
997
 
1021
998
    if not opt_no_aliases:
1099
1076
        "bzr plugin-provider-db check")
1100
1077
 
1101
1078
 
1102
 
def main(argv=None):
1103
 
    """Main entry point of command-line interface.
1104
 
 
1105
 
    :param argv: list of unicode command-line arguments similar to sys.argv.
1106
 
        argv[0] is script name usually, it will be ignored.
1107
 
        Don't pass here sys.argv because this list contains plain strings
1108
 
        and not unicode; pass None instead.
1109
 
 
1110
 
    :return: exit code of bzr command.
1111
 
    """
1112
 
    import bzrlib.ui
1113
 
    bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
1114
 
        sys.stdin, sys.stdout, sys.stderr)
1115
 
 
1116
 
    # Is this a final release version? If so, we should suppress warnings
1117
 
    if bzrlib.version_info[3] == 'final':
1118
 
        suppress_deprecation_warnings(override=True)
 
1079
 
 
1080
def _specified_or_unicode_argv(argv):
 
1081
    # For internal or testing use, argv can be passed.  Otherwise, get it from
 
1082
    # the process arguments in a unicode-safe way.
1119
1083
    if argv is None:
1120
 
        argv = osutils.get_unicode_argv()
 
1084
        return osutils.get_unicode_argv()
1121
1085
    else:
1122
1086
        new_argv = []
1123
1087
        try:
1129
1093
                    new_argv.append(a.decode('ascii'))
1130
1094
        except UnicodeDecodeError:
1131
1095
            raise errors.BzrError("argv should be list of unicode strings.")
1132
 
        argv = new_argv
 
1096
        return new_argv
 
1097
 
 
1098
 
 
1099
def main(argv=None):
 
1100
    """Main entry point of command-line interface.
 
1101
 
 
1102
    Typically `bzrlib.initialize` should be called first.
 
1103
 
 
1104
    :param argv: list of unicode command-line arguments similar to sys.argv.
 
1105
        argv[0] is script name usually, it will be ignored.
 
1106
        Don't pass here sys.argv because this list contains plain strings
 
1107
        and not unicode; pass None instead.
 
1108
 
 
1109
    :return: exit code of bzr command.
 
1110
    """
 
1111
    argv = _specified_or_unicode_argv(argv)
1133
1112
    ret = run_bzr_catch_errors(argv)
1134
1113
    bzrlib.ui.ui_factory.log_transport_activity(
1135
1114
        display=('bytes' in debug.debug_flags))
1136
1115
    trace.mutter("return code %d", ret)
1137
 
    osutils.report_extension_load_failures()
1138
1116
    return ret
1139
1117
 
1140
1118
 
1144
1122
    This function assumed that that UI layer is setup, that symbol deprecations
1145
1123
    are already applied, and that unicode decoding has already been performed on argv.
1146
1124
    """
 
1125
    # done here so that they're covered for every test run
1147
1126
    install_bzr_command_hooks()
1148
1127
    return exception_to_return_code(run_bzr, argv)
1149
1128
 
1154
1133
    This is used for the test suite, and might be useful for other programs
1155
1134
    that want to wrap the commandline interface.
1156
1135
    """
 
1136
    # done here so that they're covered for every test run
1157
1137
    install_bzr_command_hooks()
1158
1138
    try:
1159
1139
        return run_bzr(argv)
1209
1189
            yield provider
1210
1190
 
1211
1191
command_providers_registry = ProvidersRegistry()
1212
 
 
1213
 
 
1214
 
if __name__ == '__main__':
1215
 
    sys.exit(main(sys.argv))