~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Andrew Bennetts
  • Date: 2010-01-15 03:58:20 UTC
  • mfrom: (4963 +trunk)
  • mto: (4973.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4975.
  • Revision ID: andrew.bennetts@canonical.com-20100115035820-ilb3t36swgzq6v1l
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) 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
""")
383
385
            warn("No help message set for %r" % self)
384
386
        # List of standard options directly supported
385
387
        self.supported_std_options = []
386
 
 
 
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
        
387
410
    @deprecated_method(deprecated_in((2, 1, 0)))
388
411
    def _maybe_expand_globs(self, file_list):
389
412
        """Glob expand file_list if the platform does not do that itself.
510
533
                        # so don't create a real link
511
534
                        see_also_links.append(item)
512
535
                    else:
513
 
                        # Use a reST link for this entry
514
 
                        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)
515
539
                see_also = see_also_links
516
540
            result += ':See also: '
517
541
            result += ', '.join(see_also) + '\n'
595
619
 
596
620
    def _setup_outf(self):
597
621
        """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
 
622
        self.outf = ui.ui_factory.make_output_stream(
 
623
            encoding_type=self.encoding_type)
618
624
 
619
625
    def run_argv_aliases(self, argv, alias_argv=None):
620
626
        """Parse the command line and run with extra aliases in alias_argv."""
652
658
 
653
659
        self._setup_outf()
654
660
 
655
 
        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)
656
666
 
657
667
    def run(self):
658
668
        """Actually run the command.
944
954
    --concurrency
945
955
        Specify the number of processes that can be run concurrently (selftest).
946
956
    """
 
957
    trace.mutter("bazaar version: " + bzrlib.__version__)
947
958
    argv = list(argv)
948
959
    trace.mutter("bzr arguments: %r", argv)
949
960
 
1120
1131
            raise errors.BzrError("argv should be list of unicode strings.")
1121
1132
        argv = new_argv
1122
1133
    ret = run_bzr_catch_errors(argv)
 
1134
    bzrlib.ui.ui_factory.log_transport_activity(
 
1135
        display=('bytes' in debug.debug_flags))
1123
1136
    trace.mutter("return code %d", ret)
1124
1137
    osutils.report_extension_load_failures()
1125
1138
    return ret