/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: Andrew Bennetts
  • Date: 2010-02-12 04:33:05 UTC
  • mfrom: (5031 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5032.
  • Revision ID: andrew.bennetts@canonical.com-20100212043305-ujdbsdoviql2t7i3
MergeĀ lp:bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 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,
43
44
    debug,
44
45
    errors,
45
46
    option,
46
47
    osutils,
47
48
    trace,
 
49
    ui,
48
50
    win32utils,
49
51
    )
50
52
""")
57
59
    deprecated_function,
58
60
    deprecated_in,
59
61
    deprecated_method,
60
 
    suppress_deprecation_warnings,
61
62
    )
62
63
 
63
64
 
184
185
    return plugin_cmds.keys()
185
186
 
186
187
 
187
 
@deprecated_function(deprecated_in((1, 17, 0)))
188
 
def get_all_cmds(plugins_override=False):
189
 
    """Return canonical name and class for most commands.
190
 
    
191
 
    NB: This does not return all commands since the introduction of
192
 
    command hooks, and returning the class is not sufficient to 
193
 
    get correctly setup commands, which is why it is deprecated.
194
 
 
195
 
    Use 'all_command_names' + 'get_cmd_object' instead.
196
 
    """
197
 
    d = _builtin_commands()
198
 
    if plugins_override:
199
 
        d.update(plugin_cmds.iteritems())
200
 
    for k, v in d.iteritems():
201
 
        yield k,v
202
 
 
203
 
 
204
188
def get_cmd_object(cmd_name, plugins_override=True):
205
189
    """Return the command object for a command.
206
190
 
383
367
            warn("No help message set for %r" % self)
384
368
        # List of standard options directly supported
385
369
        self.supported_std_options = []
386
 
 
 
370
        self._operation = cleanup.OperationWithCleanups(self.run)
 
371
    
 
372
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
373
        """Register a function to call after self.run returns or raises.
 
374
 
 
375
        Functions will be called in LIFO order.
 
376
        """
 
377
        self._operation.add_cleanup(cleanup_func, *args, **kwargs)
 
378
 
 
379
    def cleanup_now(self):
 
380
        """Execute and empty pending cleanup functions immediately.
 
381
 
 
382
        After cleanup_now all registered cleanups are forgotten.  add_cleanup
 
383
        may be called again after cleanup_now; these cleanups will be called
 
384
        after self.run returns or raises (or when cleanup_now is next called).
 
385
 
 
386
        This is useful for releasing expensive or contentious resources (such
 
387
        as write locks) before doing further work that does not require those
 
388
        resources (such as writing results to self.outf).
 
389
        """
 
390
        self._operation.cleanup_now()
 
391
        
387
392
    @deprecated_method(deprecated_in((2, 1, 0)))
388
393
    def _maybe_expand_globs(self, file_list):
389
394
        """Glob expand file_list if the platform does not do that itself.
510
515
                        # so don't create a real link
511
516
                        see_also_links.append(item)
512
517
                    else:
513
 
                        # Use a reST link for this entry
514
 
                        see_also_links.append("`%s`_" % (item,))
 
518
                        # Use a Sphinx link for this entry
 
519
                        link_text = ":doc:`%s <%s-help>`" % (item, item)
 
520
                        see_also_links.append(link_text)
515
521
                see_also = see_also_links
516
522
            result += ':See also: '
517
523
            result += ', '.join(see_also) + '\n'
595
601
 
596
602
    def _setup_outf(self):
597
603
        """Return a file linked to stdout, which has proper encoding."""
598
 
        # Originally I was using self.stdout, but that looks
599
 
        # *way* too much like sys.stdout
600
 
        if self.encoding_type == 'exact':
601
 
            # force sys.stdout to be binary stream on win32
602
 
            if sys.platform == 'win32':
603
 
                fileno = getattr(sys.stdout, 'fileno', None)
604
 
                if fileno:
605
 
                    import msvcrt
606
 
                    msvcrt.setmode(fileno(), os.O_BINARY)
607
 
            self.outf = sys.stdout
608
 
            return
609
 
 
610
 
        output_encoding = osutils.get_terminal_encoding()
611
 
 
612
 
        self.outf = codecs.getwriter(output_encoding)(sys.stdout,
613
 
                        errors=self.encoding_type)
614
 
        # For whatever reason codecs.getwriter() does not advertise its encoding
615
 
        # it just returns the encoding of the wrapped file, which is completely
616
 
        # bogus. So set the attribute, so we can find the correct encoding later.
617
 
        self.outf.encoding = output_encoding
 
604
        self.outf = ui.ui_factory.make_output_stream(
 
605
            encoding_type=self.encoding_type)
618
606
 
619
607
    def run_argv_aliases(self, argv, alias_argv=None):
620
608
        """Parse the command line and run with extra aliases in alias_argv."""
621
 
        if argv is None:
622
 
            warn("Passing None for [] is deprecated from bzrlib 0.10",
623
 
                 DeprecationWarning, stacklevel=2)
624
 
            argv = []
625
609
        args, opts = parse_args(self, argv, alias_argv)
626
610
 
627
611
        # Process the standard options
652
636
 
653
637
        self._setup_outf()
654
638
 
655
 
        return self.run(**all_cmd_args)
 
639
        return self.run_direct(**all_cmd_args)
 
640
 
 
641
    def run_direct(self, *args, **kwargs):
 
642
        """Call run directly with objects (without parsing an argv list)."""
 
643
        return self._operation.run_simple(*args, **kwargs)
656
644
 
657
645
    def run(self):
658
646
        """Actually run the command.
940
928
 
941
929
    --coverage
942
930
        Generate line coverage report in the specified directory.
 
931
 
 
932
    --concurrency
 
933
        Specify the number of processes that can be run concurrently (selftest).
943
934
    """
 
935
    trace.mutter("bazaar version: " + bzrlib.__version__)
944
936
    argv = list(argv)
945
937
    trace.mutter("bzr arguments: %r", argv)
946
938
 
970
962
            opt_no_aliases = True
971
963
        elif a == '--builtin':
972
964
            opt_builtin = True
 
965
        elif a == '--concurrency':
 
966
            os.environ['BZR_CONCURRENCY'] = argv[i + 1]
 
967
            i += 1
973
968
        elif a == '--coverage':
974
969
            opt_coverage_dir = argv[i + 1]
975
970
            i += 1
1082
1077
        "bzr plugin-provider-db check")
1083
1078
 
1084
1079
 
1085
 
def main(argv=None):
1086
 
    """Main entry point of command-line interface.
1087
 
 
1088
 
    :param argv: list of unicode command-line arguments similar to sys.argv.
1089
 
        argv[0] is script name usually, it will be ignored.
1090
 
        Don't pass here sys.argv because this list contains plain strings
1091
 
        and not unicode; pass None instead.
1092
 
 
1093
 
    :return: exit code of bzr command.
1094
 
    """
1095
 
    import bzrlib.ui
1096
 
    bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
1097
 
        sys.stdin, sys.stdout, sys.stderr)
1098
 
 
1099
 
    # Is this a final release version? If so, we should suppress warnings
1100
 
    if bzrlib.version_info[3] == 'final':
1101
 
        suppress_deprecation_warnings(override=True)
 
1080
 
 
1081
def _specified_or_unicode_argv(argv):
 
1082
    # For internal or testing use, argv can be passed.  Otherwise, get it from
 
1083
    # the process arguments in a unicode-safe way.
1102
1084
    if argv is None:
1103
 
        argv = osutils.get_unicode_argv()
 
1085
        return osutils.get_unicode_argv()
1104
1086
    else:
1105
1087
        new_argv = []
1106
1088
        try:
1112
1094
                    new_argv.append(a.decode('ascii'))
1113
1095
        except UnicodeDecodeError:
1114
1096
            raise errors.BzrError("argv should be list of unicode strings.")
1115
 
        argv = new_argv
 
1097
        return new_argv
 
1098
 
 
1099
 
 
1100
def main(argv=None):
 
1101
    """Main entry point of command-line interface.
 
1102
 
 
1103
    Typically `bzrlib.initialize` should be called first.
 
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
    argv = _specified_or_unicode_argv(argv)
1116
1113
    ret = run_bzr_catch_errors(argv)
 
1114
    bzrlib.ui.ui_factory.log_transport_activity(
 
1115
        display=('bytes' in debug.debug_flags))
1117
1116
    trace.mutter("return code %d", ret)
1118
 
    osutils.report_extension_load_failures()
1119
1117
    return ret
1120
1118
 
1121
1119
 
1125
1123
    This function assumed that that UI layer is setup, that symbol deprecations
1126
1124
    are already applied, and that unicode decoding has already been performed on argv.
1127
1125
    """
 
1126
    # done here so that they're covered for every test run
1128
1127
    install_bzr_command_hooks()
1129
1128
    return exception_to_return_code(run_bzr, argv)
1130
1129
 
1135
1134
    This is used for the test suite, and might be useful for other programs
1136
1135
    that want to wrap the commandline interface.
1137
1136
    """
 
1137
    # done here so that they're covered for every test run
1138
1138
    install_bzr_command_hooks()
1139
1139
    try:
1140
1140
        return run_bzr(argv)
1190
1190
            yield provider
1191
1191
 
1192
1192
command_providers_registry = ProvidersRegistry()
1193
 
 
1194
 
 
1195
 
if __name__ == '__main__':
1196
 
    sys.exit(main(sys.argv))