~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-01-14 00:01:32 UTC
  • mfrom: (4957.1.1 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100114000132-3p3rabnonjw3gzqb
(jam) Merge bzr.stable, bringing in bug fixes #175839, #504390

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2008, 2009 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,
45
44
    debug,
46
45
    errors,
47
46
    option,
55
54
from bzrlib.hooks import HookPoint, Hooks
56
55
# Compatibility - Option used to be in commands.
57
56
from bzrlib.option import Option
58
 
from bzrlib.plugin import disable_plugins, load_plugins
59
57
from bzrlib import registry
60
58
from bzrlib.symbol_versioning import (
61
59
    deprecated_function,
62
60
    deprecated_in,
63
61
    deprecated_method,
 
62
    suppress_deprecation_warnings,
64
63
    )
65
64
 
66
65
 
187
186
    return plugin_cmds.keys()
188
187
 
189
188
 
 
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
 
190
206
def get_cmd_object(cmd_name, plugins_override=True):
191
207
    """Return the command object for a command.
192
208
 
608
624
 
609
625
    def run_argv_aliases(self, argv, alias_argv=None):
610
626
        """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 = []
611
631
        args, opts = parse_args(self, argv, alias_argv)
612
632
 
613
633
        # Process the standard options
875
895
    return ret
876
896
 
877
897
 
 
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
 
878
903
def get_alias(cmd, config=None):
879
904
    """Return an expanded alias, or None if no alias exists.
880
905
 
890
915
        config = bzrlib.config.GlobalConfig()
891
916
    alias = config.get_alias(cmd)
892
917
    if (alias):
893
 
        return cmdline.split(alias)
 
918
        return shlex_split_unicode(alias)
894
919
    return None
895
920
 
896
921
 
897
 
def run_bzr(argv, load_plugins=load_plugins, disable_plugins=disable_plugins):
 
922
def run_bzr(argv):
898
923
    """Execute a command.
899
924
 
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.
 
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.
912
931
 
913
932
    Special master options: these must come before the command because
914
933
    they control how the command is interpreted.
979
998
 
980
999
    debug.set_debug_flags_from_config()
981
1000
 
 
1001
    argv = argv_copy
 
1002
    if (not argv):
 
1003
        from bzrlib.builtins import cmd_help
 
1004
        cmd_help().run_argv_aliases([])
 
1005
        return 0
 
1006
 
 
1007
    if argv[0] == '--version':
 
1008
        from bzrlib.builtins import cmd_version
 
1009
        cmd_version().run_argv_aliases([])
 
1010
        return 0
 
1011
 
982
1012
    if not opt_no_plugins:
 
1013
        from bzrlib.plugin import load_plugins
983
1014
        load_plugins()
984
1015
    else:
 
1016
        from bzrlib.plugin import disable_plugins
985
1017
        disable_plugins()
986
1018
 
987
 
    argv = argv_copy
988
 
    if (not argv):
989
 
        get_cmd_object('help').run_argv_aliases([])
990
 
        return 0
991
 
 
992
 
    if argv[0] == '--version':
993
 
        get_cmd_object('version').run_argv_aliases([])
994
 
        return 0
995
 
 
996
1019
    alias_argv = None
997
1020
 
998
1021
    if not opt_no_aliases:
1076
1099
        "bzr plugin-provider-db check")
1077
1100
 
1078
1101
 
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.
 
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)
1083
1119
    if argv is None:
1084
 
        return osutils.get_unicode_argv()
 
1120
        argv = osutils.get_unicode_argv()
1085
1121
    else:
1086
1122
        new_argv = []
1087
1123
        try:
1093
1129
                    new_argv.append(a.decode('ascii'))
1094
1130
        except UnicodeDecodeError:
1095
1131
            raise errors.BzrError("argv should be list of unicode strings.")
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)
 
1132
        argv = new_argv
1112
1133
    ret = run_bzr_catch_errors(argv)
1113
1134
    bzrlib.ui.ui_factory.log_transport_activity(
1114
1135
        display=('bytes' in debug.debug_flags))
1115
1136
    trace.mutter("return code %d", ret)
 
1137
    osutils.report_extension_load_failures()
1116
1138
    return ret
1117
1139
 
1118
1140
 
1122
1144
    This function assumed that that UI layer is setup, that symbol deprecations
1123
1145
    are already applied, and that unicode decoding has already been performed on argv.
1124
1146
    """
1125
 
    # done here so that they're covered for every test run
1126
1147
    install_bzr_command_hooks()
1127
1148
    return exception_to_return_code(run_bzr, argv)
1128
1149
 
1133
1154
    This is used for the test suite, and might be useful for other programs
1134
1155
    that want to wrap the commandline interface.
1135
1156
    """
1136
 
    # done here so that they're covered for every test run
1137
1157
    install_bzr_command_hooks()
1138
1158
    try:
1139
1159
        return run_bzr(argv)
1189
1209
            yield provider
1190
1210
 
1191
1211
command_providers_registry = ProvidersRegistry()
 
1212
 
 
1213
 
 
1214
if __name__ == '__main__':
 
1215
    sys.exit(main(sys.argv))