/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

merge bzr.dev r4154

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 
51
51
from bzrlib import registry
52
52
# Compatibility
53
 
from bzrlib.hooks import Hooks
 
53
from bzrlib.hooks import HookPoint, Hooks
54
54
from bzrlib.option import Option
55
55
 
56
56
 
603
603
        notified.
604
604
        """
605
605
        Hooks.__init__(self)
606
 
        # Introduced in 1.13:
607
 
        # invoked after creating a command object to allow modifications such
608
 
        # as adding or removing options, docs etc. Invoked with the command
609
 
        # object.
610
 
        self['extend_command'] = []
 
606
        self.create_hook(HookPoint('extend_command',
 
607
            "Called after creating a command object to allow modifications "
 
608
            "such as adding or removing options, docs etc. Called with the "
 
609
            "new bzrlib.commands.Command object.", (1, 13), None))
611
610
 
612
611
Command.hooks = CommandHooks()
613
612
 
684
683
    tracer = trace.Trace(count=1, trace=0)
685
684
    sys.settrace(tracer.globaltrace)
686
685
 
687
 
    ret = the_callable(*args, **kwargs)
688
 
 
689
 
    sys.settrace(None)
690
 
    results = tracer.results()
691
 
    results.write_results(show_missing=1, summary=False,
692
 
                          coverdir=dirname)
 
686
    try:
 
687
        return exception_to_return_code(the_callable, *args, **kwargs)
 
688
    finally:
 
689
        sys.settrace(None)
 
690
        results = tracer.results()
 
691
        results.write_results(show_missing=1, summary=False,
 
692
                              coverdir=dirname)
693
693
 
694
694
 
695
695
def apply_profiled(the_callable, *args, **kwargs):
700
700
    try:
701
701
        prof = hotshot.Profile(pfname)
702
702
        try:
703
 
            ret = prof.runcall(the_callable, *args, **kwargs) or 0
 
703
            ret = prof.runcall(exception_to_return_code, the_callable, *args,
 
704
                **kwargs) or 0
704
705
        finally:
705
706
            prof.close()
706
707
        stats = hotshot.stats.load(pfname)
715
716
        os.remove(pfname)
716
717
 
717
718
 
 
719
def exception_to_return_code(the_callable, *args, **kwargs):
 
720
    """UI level helper for profiling and coverage.
 
721
 
 
722
    This transforms exceptions into a return value of 3. As such its only
 
723
    relevant to the UI layer, and should never be called where catching
 
724
    exceptions may be desirable.
 
725
    """
 
726
    try:
 
727
        return the_callable(*args, **kwargs)
 
728
    except (KeyboardInterrupt, Exception), e:
 
729
        # used to handle AssertionError and KeyboardInterrupt
 
730
        # specially here, but hopefully they're handled ok by the logger now
 
731
        exc_info = sys.exc_info()
 
732
        exitcode = trace.report_exception(exc_info, sys.stderr)
 
733
        if os.environ.get('BZR_PDB'):
 
734
            print '**** entering debugger'
 
735
            tb = exc_info[2]
 
736
            import pdb
 
737
            if sys.version_info[:2] < (2, 6):
 
738
                # XXX: we want to do
 
739
                #    pdb.post_mortem(tb)
 
740
                # but because pdb.post_mortem gives bad results for tracebacks
 
741
                # from inside generators, we do it manually.
 
742
                # (http://bugs.python.org/issue4150, fixed in Python 2.6)
 
743
 
 
744
                # Setup pdb on the traceback
 
745
                p = pdb.Pdb()
 
746
                p.reset()
 
747
                p.setup(tb.tb_frame, tb)
 
748
                # Point the debugger at the deepest frame of the stack
 
749
                p.curindex = len(p.stack) - 1
 
750
                p.curframe = p.stack[p.curindex][0]
 
751
                # Start the pdb prompt.
 
752
                p.print_stack_entry(p.stack[p.curindex])
 
753
                p.execRcLines()
 
754
                p.cmdloop()
 
755
            else:
 
756
                pdb.post_mortem(tb)
 
757
        return exitcode
 
758
 
 
759
 
718
760
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
719
761
    from bzrlib.lsprof import profile
720
 
    ret, stats = profile(the_callable, *args, **kwargs)
 
762
    ret, stats = profile(exception_to_return_code, the_callable, *args, **kwargs)
721
763
    stats.sort()
722
764
    if filename is None:
723
765
        stats.pprint()
821
863
            argv_copy.append(a)
822
864
        i += 1
823
865
 
 
866
    debug.set_debug_flags_from_config()
 
867
 
824
868
    argv = argv_copy
825
869
    if (not argv):
826
870
        from bzrlib.builtins import cmd_help
927
971
 
928
972
 
929
973
def run_bzr_catch_errors(argv):
930
 
    # Note: The except clause logic below should be kept in sync with the
931
 
    # profile() routine in lsprof.py.
932
 
    try:
933
 
        return run_bzr(argv)
934
 
    except (KeyboardInterrupt, Exception), e:
935
 
        # used to handle AssertionError and KeyboardInterrupt
936
 
        # specially here, but hopefully they're handled ok by the logger now
937
 
        exc_info = sys.exc_info()
938
 
        exitcode = trace.report_exception(exc_info, sys.stderr)
939
 
        if os.environ.get('BZR_PDB'):
940
 
            print '**** entering debugger'
941
 
            tb = exc_info[2]
942
 
            import pdb
943
 
            if sys.version_info[:2] < (2, 6):
944
 
                # XXX: we want to do
945
 
                #    pdb.post_mortem(tb)
946
 
                # but because pdb.post_mortem gives bad results for tracebacks
947
 
                # from inside generators, we do it manually.
948
 
                # (http://bugs.python.org/issue4150, fixed in Python 2.6)
 
974
    """Run a bzr command with parameters as described by argv.
949
975
 
950
 
                # Setup pdb on the traceback
951
 
                p = pdb.Pdb()
952
 
                p.reset()
953
 
                p.setup(tb.tb_frame, tb)
954
 
                # Point the debugger at the deepest frame of the stack
955
 
                p.curindex = len(p.stack) - 1
956
 
                p.curframe = p.stack[p.curindex]
957
 
                # Start the pdb prompt.
958
 
                p.print_stack_entry(p.stack[p.curindex])
959
 
                p.execRcLines()
960
 
                p.cmdloop()
961
 
            else:
962
 
                pdb.post_mortem(tb)
963
 
        return exitcode
 
976
    This function assumed that that UI layer is setup, that symbol deprecations
 
977
    are already applied, and that unicode decoding has already been performed on argv.
 
978
    """
 
979
    return exception_to_return_code(run_bzr, argv)
964
980
 
965
981
 
966
982
def run_bzr_catch_user_errors(argv):