~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-09 20:33:43 UTC
  • mto: (5029.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5030.
  • Revision ID: v.ladeuil+lp@free.fr-20100209203343-ktxx7t0xvptvjnt1
Move TestingPathFilteringServer to bzrlib.tests.test_server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 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
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
""")
56
58
from bzrlib.symbol_versioning import (
57
59
    deprecated_function,
58
60
    deprecated_in,
 
61
    deprecated_method,
59
62
    suppress_deprecation_warnings,
60
63
    )
61
64
 
101
104
            registry.Registry.register(self, k_unsquished, cmd,
102
105
                                       override_existing=decorate, info=info)
103
106
        except KeyError:
104
 
            trace.log_error('Two plugins defined the same command: %r' % k)
105
 
            trace.log_error('Not loading the one in %r' %
106
 
                            sys.modules[cmd.__module__])
107
 
            trace.log_error('Previously this command was registered from %r' %
108
 
                            sys.modules[previous.__module__])
 
107
            trace.warning('Two plugins defined the same command: %r' % k)
 
108
            trace.warning('Not loading the one in %r' %
 
109
                sys.modules[cmd.__module__])
 
110
            trace.warning('Previously this command was registered from %r' %
 
111
                sys.modules[previous.__module__])
109
112
        return previous
110
113
 
111
114
    def register_lazy(self, command_name, aliases, module_name):
382
385
            warn("No help message set for %r" % self)
383
386
        # List of standard options directly supported
384
387
        self.supported_std_options = []
385
 
 
 
388
        self._operation = cleanup.OperationWithCleanups(self.run)
 
389
    
 
390
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
391
        """Register a function to call after self.run returns or raises.
 
392
 
 
393
        Functions will be called in LIFO order.
 
394
        """
 
395
        self._operation.add_cleanup(cleanup_func, *args, **kwargs)
 
396
 
 
397
    def cleanup_now(self):
 
398
        """Execute and empty pending cleanup functions immediately.
 
399
 
 
400
        After cleanup_now all registered cleanups are forgotten.  add_cleanup
 
401
        may be called again after cleanup_now; these cleanups will be called
 
402
        after self.run returns or raises (or when cleanup_now is next called).
 
403
 
 
404
        This is useful for releasing expensive or contentious resources (such
 
405
        as write locks) before doing further work that does not require those
 
406
        resources (such as writing results to self.outf).
 
407
        """
 
408
        self._operation.cleanup_now()
 
409
        
 
410
    @deprecated_method(deprecated_in((2, 1, 0)))
386
411
    def _maybe_expand_globs(self, file_list):
387
412
        """Glob expand file_list if the platform does not do that itself.
388
413
 
 
414
        Not used anymore, now that the bzr command-line parser globs on
 
415
        Windows.
 
416
 
389
417
        :return: A possibly empty list of unicode paths.
390
418
 
391
419
        Introduced in bzrlib 0.18.
392
420
        """
393
 
        if not file_list:
394
 
            file_list = []
395
 
        if sys.platform == 'win32':
396
 
            file_list = win32utils.glob_expand(file_list)
397
 
        return list(file_list)
 
421
        return file_list
398
422
 
399
423
    def _usage(self):
400
424
        """Return single-line grammar for this command.
457
481
        # so we get <https://bugs.launchpad.net/bzr/+bug/249908>.  -- mbp
458
482
        # 20090319
459
483
        options = option.get_optparser(self.options()).format_option_help()
 
484
        # XXX: According to the spec, ReST option lists actually don't support 
 
485
        # options like --1.9 so that causes syntax errors (in Sphinx at least).
 
486
        # As that pattern always appears in the commands that break, we trap
 
487
        # on that and then format that block of 'format' options as a literal
 
488
        # block.
 
489
        if not plain and options.find('  --1.9  ') != -1:
 
490
            options = options.replace(' format:\n', ' format::\n\n', 1)
460
491
        if options.startswith('Options:'):
461
492
            result += ':' + options
462
493
        elif options.startswith('options:'):
502
533
                        # so don't create a real link
503
534
                        see_also_links.append(item)
504
535
                    else:
505
 
                        # Use a reST link for this entry
506
 
                        see_also_links.append("`%s`_" % (item,))
 
536
                        # Use a Sphinx link for this entry
 
537
                        link_text = ":doc:`%s <%s-help>`" % (item, item)
 
538
                        see_also_links.append(link_text)
507
539
                see_also = see_also_links
508
540
            result += ':See also: '
509
541
            result += ', '.join(see_also) + '\n'
587
619
 
588
620
    def _setup_outf(self):
589
621
        """Return a file linked to stdout, which has proper encoding."""
590
 
        # Originally I was using self.stdout, but that looks
591
 
        # *way* too much like sys.stdout
592
 
        if self.encoding_type == 'exact':
593
 
            # force sys.stdout to be binary stream on win32
594
 
            if sys.platform == 'win32':
595
 
                fileno = getattr(sys.stdout, 'fileno', None)
596
 
                if fileno:
597
 
                    import msvcrt
598
 
                    msvcrt.setmode(fileno(), os.O_BINARY)
599
 
            self.outf = sys.stdout
600
 
            return
601
 
 
602
 
        output_encoding = osutils.get_terminal_encoding()
603
 
 
604
 
        self.outf = codecs.getwriter(output_encoding)(sys.stdout,
605
 
                        errors=self.encoding_type)
606
 
        # For whatever reason codecs.getwriter() does not advertise its encoding
607
 
        # it just returns the encoding of the wrapped file, which is completely
608
 
        # bogus. So set the attribute, so we can find the correct encoding later.
609
 
        self.outf.encoding = output_encoding
 
622
        self.outf = ui.ui_factory.make_output_stream(
 
623
            encoding_type=self.encoding_type)
610
624
 
611
625
    def run_argv_aliases(self, argv, alias_argv=None):
612
626
        """Parse the command line and run with extra aliases in alias_argv."""
644
658
 
645
659
        self._setup_outf()
646
660
 
647
 
        return self.run(**all_cmd_args)
 
661
        return self.run_direct(**all_cmd_args)
 
662
 
 
663
    def run_direct(self, *args, **kwargs):
 
664
        """Call run directly with objects (without parsing an argv list)."""
 
665
        return self._operation.run_simple(*args, **kwargs)
648
666
 
649
667
    def run(self):
650
668
        """Actually run the command.
932
950
 
933
951
    --coverage
934
952
        Generate line coverage report in the specified directory.
 
953
 
 
954
    --concurrency
 
955
        Specify the number of processes that can be run concurrently (selftest).
935
956
    """
 
957
    trace.mutter("bazaar version: " + bzrlib.__version__)
936
958
    argv = list(argv)
937
959
    trace.mutter("bzr arguments: %r", argv)
938
960
 
962
984
            opt_no_aliases = True
963
985
        elif a == '--builtin':
964
986
            opt_builtin = True
 
987
        elif a == '--concurrency':
 
988
            os.environ['BZR_CONCURRENCY'] = argv[i + 1]
 
989
            i += 1
965
990
        elif a == '--coverage':
966
991
            opt_coverage_dir = argv[i + 1]
967
992
            i += 1
1028
1053
            ret = apply_coveraged(opt_coverage_dir, run, *run_argv)
1029
1054
        else:
1030
1055
            ret = run(*run_argv)
1031
 
        if 'memory' in debug.debug_flags:
1032
 
            trace.debug_memory('Process status after command:', short=False)
1033
1056
        return ret or 0
1034
1057
    finally:
1035
1058
        # reset, in case we may do other commands later within the same
1036
1059
        # process. Commands that want to execute sub-commands must propagate
1037
1060
        # --verbose in their own way.
 
1061
        if 'memory' in debug.debug_flags:
 
1062
            trace.debug_memory('Process status after command:', short=False)
1038
1063
        option._verbosity_level = saved_verbosity_level
1039
1064
 
1040
1065
 
1090
1115
 
1091
1116
    # Is this a final release version? If so, we should suppress warnings
1092
1117
    if bzrlib.version_info[3] == 'final':
1093
 
        suppress_deprecation_warnings(override=False)
 
1118
        suppress_deprecation_warnings(override=True)
1094
1119
    if argv is None:
1095
1120
        argv = osutils.get_unicode_argv()
1096
1121
    else:
1106
1131
            raise errors.BzrError("argv should be list of unicode strings.")
1107
1132
        argv = new_argv
1108
1133
    ret = run_bzr_catch_errors(argv)
 
1134
    bzrlib.ui.ui_factory.log_transport_activity(
 
1135
        display=('bytes' in debug.debug_flags))
1109
1136
    trace.mutter("return code %d", ret)
 
1137
    osutils.report_extension_load_failures()
1110
1138
    return ret
1111
1139
 
1112
1140