/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: Jelmer Vernooij
  • Date: 2010-03-21 21:39:33 UTC
  • mfrom: (5102 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5143.
  • Revision ID: jelmer@samba.org-20100321213933-fexeh9zcoz8oaju2
merge bzr.dev.

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
40
40
 
41
41
import bzrlib
42
42
from bzrlib import (
 
43
    cleanup,
 
44
    cmdline,
43
45
    debug,
44
46
    errors,
45
47
    option,
53
55
from bzrlib.hooks import HookPoint, Hooks
54
56
# Compatibility - Option used to be in commands.
55
57
from bzrlib.option import Option
 
58
from bzrlib.plugin import disable_plugins, load_plugins
56
59
from bzrlib import registry
57
60
from bzrlib.symbol_versioning import (
58
61
    deprecated_function,
59
62
    deprecated_in,
60
63
    deprecated_method,
61
 
    suppress_deprecation_warnings,
62
64
    )
63
65
 
64
66
 
185
187
    return plugin_cmds.keys()
186
188
 
187
189
 
188
 
@deprecated_function(deprecated_in((1, 17, 0)))
189
 
def get_all_cmds(plugins_override=False):
190
 
    """Return canonical name and class for most commands.
191
 
    
192
 
    NB: This does not return all commands since the introduction of
193
 
    command hooks, and returning the class is not sufficient to 
194
 
    get correctly setup commands, which is why it is deprecated.
195
 
 
196
 
    Use 'all_command_names' + 'get_cmd_object' instead.
197
 
    """
198
 
    d = _builtin_commands()
199
 
    if plugins_override:
200
 
        d.update(plugin_cmds.iteritems())
201
 
    for k, v in d.iteritems():
202
 
        yield k,v
203
 
 
204
 
 
205
190
def get_cmd_object(cmd_name, plugins_override=True):
206
191
    """Return the command object for a command.
207
192
 
214
199
        raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
215
200
 
216
201
 
217
 
def _get_cmd_object(cmd_name, plugins_override=True):
 
202
def _get_cmd_object(cmd_name, plugins_override=True, check_missing=True):
218
203
    """Get a command object.
219
204
 
220
205
    :param cmd_name: The name of the command.
221
206
    :param plugins_override: Allow plugins to override builtins.
 
207
    :param check_missing: Look up commands not found in the regular index via
 
208
        the get_missing_command hook.
222
209
    :return: A Command object instance
223
210
    :raises KeyError: If no command is found.
224
211
    """
234
221
            # We've found a non-plugin command, don't permit it to be
235
222
            # overridden.
236
223
            break
237
 
    if cmd is None:
 
224
    if cmd is None and check_missing:
238
225
        for hook in Command.hooks['get_missing_command']:
239
226
            cmd = hook(cmd_name)
240
227
            if cmd is not None:
384
371
            warn("No help message set for %r" % self)
385
372
        # List of standard options directly supported
386
373
        self.supported_std_options = []
 
374
        self._operation = cleanup.OperationWithCleanups(self.run)
 
375
 
 
376
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
377
        """Register a function to call after self.run returns or raises.
 
378
 
 
379
        Functions will be called in LIFO order.
 
380
        """
 
381
        self._operation.add_cleanup(cleanup_func, *args, **kwargs)
 
382
 
 
383
    def cleanup_now(self):
 
384
        """Execute and empty pending cleanup functions immediately.
 
385
 
 
386
        After cleanup_now all registered cleanups are forgotten.  add_cleanup
 
387
        may be called again after cleanup_now; these cleanups will be called
 
388
        after self.run returns or raises (or when cleanup_now is next called).
 
389
 
 
390
        This is useful for releasing expensive or contentious resources (such
 
391
        as write locks) before doing further work that does not require those
 
392
        resources (such as writing results to self.outf).
 
393
        """
 
394
        self._operation.cleanup_now()
387
395
 
388
396
    @deprecated_method(deprecated_in((2, 1, 0)))
389
397
    def _maybe_expand_globs(self, file_list):
511
519
                        # so don't create a real link
512
520
                        see_also_links.append(item)
513
521
                    else:
514
 
                        # Use a reST link for this entry
515
 
                        see_also_links.append("`%s`_" % (item,))
 
522
                        # Use a Sphinx link for this entry
 
523
                        link_text = ":doc:`%s <%s-help>`" % (item, item)
 
524
                        see_also_links.append(link_text)
516
525
                see_also = see_also_links
517
526
            result += ':See also: '
518
527
            result += ', '.join(see_also) + '\n'
601
610
 
602
611
    def run_argv_aliases(self, argv, alias_argv=None):
603
612
        """Parse the command line and run with extra aliases in alias_argv."""
604
 
        if argv is None:
605
 
            warn("Passing None for [] is deprecated from bzrlib 0.10",
606
 
                 DeprecationWarning, stacklevel=2)
607
 
            argv = []
608
613
        args, opts = parse_args(self, argv, alias_argv)
609
614
 
610
615
        # Process the standard options
635
640
 
636
641
        self._setup_outf()
637
642
 
638
 
        return self.run(**all_cmd_args)
 
643
        return self.run_direct(**all_cmd_args)
 
644
 
 
645
    def run_direct(self, *args, **kwargs):
 
646
        """Call run directly with objects (without parsing an argv list)."""
 
647
        return self._operation.run_simple(*args, **kwargs)
639
648
 
640
649
    def run(self):
641
650
        """Actually run the command.
868
877
    return ret
869
878
 
870
879
 
 
880
@deprecated_function(deprecated_in((2, 2, 0)))
871
881
def shlex_split_unicode(unsplit):
872
 
    import shlex
873
 
    return [u.decode('utf-8') for u in shlex.split(unsplit.encode('utf-8'))]
 
882
    return cmdline.split(unsplit)
874
883
 
875
884
 
876
885
def get_alias(cmd, config=None):
888
897
        config = bzrlib.config.GlobalConfig()
889
898
    alias = config.get_alias(cmd)
890
899
    if (alias):
891
 
        return shlex_split_unicode(alias)
 
900
        return cmdline.split(alias)
892
901
    return None
893
902
 
894
903
 
895
 
def run_bzr(argv):
 
904
def run_bzr(argv, load_plugins=load_plugins, disable_plugins=disable_plugins):
896
905
    """Execute a command.
897
906
 
898
 
    argv
899
 
       The command-line arguments, without the program name from argv[0]
900
 
       These should already be decoded. All library/test code calling
901
 
       run_bzr should be passing valid strings (don't need decoding).
902
 
 
903
 
    Returns a command status or raises an exception.
 
907
    :param argv: The command-line arguments, without the program name from
 
908
        argv[0] These should already be decoded. All library/test code calling
 
909
        run_bzr should be passing valid strings (don't need decoding).
 
910
    :param load_plugins: What function to call when triggering plugin loading.
 
911
        This function should take no arguments and cause all plugins to be
 
912
        loaded.
 
913
    :param disable_plugins: What function to call when disabling plugin
 
914
        loading. This function should take no arguments and cause all plugin
 
915
        loading to be prohibited (so that code paths in your application that
 
916
        know about some plugins possibly being present will fail to import
 
917
        those plugins even if they are installed.)
 
918
    :return: Returns a command exit code or raises an exception.
904
919
 
905
920
    Special master options: these must come before the command because
906
921
    they control how the command is interpreted.
927
942
    --concurrency
928
943
        Specify the number of processes that can be run concurrently (selftest).
929
944
    """
 
945
    trace.mutter("bazaar version: " + bzrlib.__version__)
930
946
    argv = list(argv)
931
947
    trace.mutter("bzr arguments: %r", argv)
932
948
 
970
986
 
971
987
    debug.set_debug_flags_from_config()
972
988
 
 
989
    if not opt_no_plugins:
 
990
        load_plugins()
 
991
    else:
 
992
        disable_plugins()
 
993
 
973
994
    argv = argv_copy
974
995
    if (not argv):
975
 
        from bzrlib.builtins import cmd_help
976
 
        cmd_help().run_argv_aliases([])
 
996
        get_cmd_object('help').run_argv_aliases([])
977
997
        return 0
978
998
 
979
999
    if argv[0] == '--version':
980
 
        from bzrlib.builtins import cmd_version
981
 
        cmd_version().run_argv_aliases([])
 
1000
        get_cmd_object('version').run_argv_aliases([])
982
1001
        return 0
983
1002
 
984
 
    if not opt_no_plugins:
985
 
        from bzrlib.plugin import load_plugins
986
 
        load_plugins()
987
 
    else:
988
 
        from bzrlib.plugin import disable_plugins
989
 
        disable_plugins()
990
 
 
991
1003
    alias_argv = None
992
1004
 
993
1005
    if not opt_no_aliases:
1071
1083
        "bzr plugin-provider-db check")
1072
1084
 
1073
1085
 
1074
 
def main(argv=None):
1075
 
    """Main entry point of command-line interface.
1076
 
 
1077
 
    :param argv: list of unicode command-line arguments similar to sys.argv.
1078
 
        argv[0] is script name usually, it will be ignored.
1079
 
        Don't pass here sys.argv because this list contains plain strings
1080
 
        and not unicode; pass None instead.
1081
 
 
1082
 
    :return: exit code of bzr command.
1083
 
    """
1084
 
    import bzrlib.ui
1085
 
    bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
1086
 
        sys.stdin, sys.stdout, sys.stderr)
1087
 
 
1088
 
    # Is this a final release version? If so, we should suppress warnings
1089
 
    if bzrlib.version_info[3] == 'final':
1090
 
        suppress_deprecation_warnings(override=True)
 
1086
 
 
1087
def _specified_or_unicode_argv(argv):
 
1088
    # For internal or testing use, argv can be passed.  Otherwise, get it from
 
1089
    # the process arguments in a unicode-safe way.
1091
1090
    if argv is None:
1092
 
        argv = osutils.get_unicode_argv()
 
1091
        return osutils.get_unicode_argv()
1093
1092
    else:
1094
1093
        new_argv = []
1095
1094
        try:
1101
1100
                    new_argv.append(a.decode('ascii'))
1102
1101
        except UnicodeDecodeError:
1103
1102
            raise errors.BzrError("argv should be list of unicode strings.")
1104
 
        argv = new_argv
 
1103
        return new_argv
 
1104
 
 
1105
 
 
1106
def main(argv=None):
 
1107
    """Main entry point of command-line interface.
 
1108
 
 
1109
    Typically `bzrlib.initialize` should be called first.
 
1110
 
 
1111
    :param argv: list of unicode command-line arguments similar to sys.argv.
 
1112
        argv[0] is script name usually, it will be ignored.
 
1113
        Don't pass here sys.argv because this list contains plain strings
 
1114
        and not unicode; pass None instead.
 
1115
 
 
1116
    :return: exit code of bzr command.
 
1117
    """
 
1118
    argv = _specified_or_unicode_argv(argv)
1105
1119
    ret = run_bzr_catch_errors(argv)
 
1120
    bzrlib.ui.ui_factory.log_transport_activity(
 
1121
        display=('bytes' in debug.debug_flags))
1106
1122
    trace.mutter("return code %d", ret)
1107
 
    osutils.report_extension_load_failures()
1108
1123
    return ret
1109
1124
 
1110
1125
 
1114
1129
    This function assumed that that UI layer is setup, that symbol deprecations
1115
1130
    are already applied, and that unicode decoding has already been performed on argv.
1116
1131
    """
 
1132
    # done here so that they're covered for every test run
1117
1133
    install_bzr_command_hooks()
1118
1134
    return exception_to_return_code(run_bzr, argv)
1119
1135
 
1124
1140
    This is used for the test suite, and might be useful for other programs
1125
1141
    that want to wrap the commandline interface.
1126
1142
    """
 
1143
    # done here so that they're covered for every test run
1127
1144
    install_bzr_command_hooks()
1128
1145
    try:
1129
1146
        return run_bzr(argv)
1152
1169
        if topic and topic.startswith(self.prefix):
1153
1170
            topic = topic[len(self.prefix):]
1154
1171
        try:
1155
 
            cmd = _get_cmd_object(topic)
 
1172
            cmd = _get_cmd_object(topic, check_missing=False)
1156
1173
        except KeyError:
1157
1174
            return []
1158
1175
        else:
1179
1196
            yield provider
1180
1197
 
1181
1198
command_providers_registry = ProvidersRegistry()
1182
 
 
1183
 
 
1184
 
if __name__ == '__main__':
1185
 
    sys.exit(main(sys.argv))