13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
# TODO: probably should say which arguments are candidates for glob
51
51
from bzrlib import registry
53
from bzrlib.hooks import HookPoint, Hooks
53
from bzrlib.hooks import Hooks
54
54
from bzrlib.option import Option
333
333
def get_help_text(self, additional_see_also=None, plain=True,
334
see_also_as_links=False, verbose=True):
334
see_also_as_links=False):
335
335
"""Return a text string with help for this command.
337
337
:param additional_see_also: Additional help topics to be
340
340
returned instead of plain text.
341
341
:param see_also_as_links: if True, convert items in 'See also'
342
342
list to internal links (used by bzr_man rstx generator)
343
:param verbose: if True, display the full help, otherwise
344
leave out the descriptive sections and just display
345
usage help (e.g. Purpose, Usage, Options) with a
346
message explaining how to obtain full help.
348
344
doc = self.help()
378
374
result += options
382
# Add the description, indenting it 2 spaces
383
# to match the indentation of the options
384
if sections.has_key(None):
385
text = sections.pop(None)
386
text = '\n '.join(text.splitlines())
387
result += ':%s:\n %s\n\n' % ('Description',text)
377
# Add the description, indenting it 2 spaces
378
# to match the indentation of the options
379
if sections.has_key(None):
380
text = sections.pop(None)
381
text = '\n '.join(text.splitlines())
382
result += ':%s:\n %s\n\n' % ('Description',text)
389
# Add the custom sections (e.g. Examples). Note that there's no need
390
# to indent these as they must be indented already in the source.
393
if sections.has_key(label):
394
result += ':%s:\n%s\n' % (label,sections[label])
397
result += ("See bzr help %s for more details and examples.\n\n"
384
# Add the custom sections (e.g. Examples). Note that there's no need
385
# to indent these as they must be indented already in the source.
388
if sections.has_key(label):
389
result += ':%s:\n%s\n\n' % (label,sections[label])
400
391
# Add the aliases, source (plug-in) and see also links, if any
532
523
if 'help' in opts: # e.g. bzr add --help
533
524
sys.stdout.write(self.get_help_text())
535
if 'usage' in opts: # e.g. bzr add --usage
536
sys.stdout.write(self.get_help_text(verbose=False))
538
526
trace.set_verbosity_level(option._verbosity_level)
539
527
if 'verbose' in self.supported_std_options:
540
528
opts['verbose'] = trace.is_verbose()
605
593
Hooks.__init__(self)
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))
594
# Introduced in 1.13:
595
# invoked after creating a command object to allow modifications such
596
# as adding or removing options, docs etc. Invoked with the command
598
self['extend_command'] = []
611
600
Command.hooks = CommandHooks()
683
672
tracer = trace.Trace(count=1, trace=0)
684
673
sys.settrace(tracer.globaltrace)
687
return exception_to_return_code(the_callable, *args, **kwargs)
690
results = tracer.results()
691
results.write_results(show_missing=1, summary=False,
675
ret = the_callable(*args, **kwargs)
678
results = tracer.results()
679
results.write_results(show_missing=1, summary=False,
695
683
def apply_profiled(the_callable, *args, **kwargs):
701
689
prof = hotshot.Profile(pfname)
703
ret = prof.runcall(exception_to_return_code, the_callable, *args,
691
ret = prof.runcall(the_callable, *args, **kwargs) or 0
707
694
stats = hotshot.stats.load(pfname)
716
703
os.remove(pfname)
719
def exception_to_return_code(the_callable, *args, **kwargs):
720
"""UI level helper for profiling and coverage.
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.
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'
737
if sys.version_info[:2] < (2, 6):
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)
744
# Setup pdb on the traceback
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])
760
706
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
761
707
from bzrlib.lsprof import profile
762
ret, stats = profile(exception_to_return_code, the_callable, *args, **kwargs)
708
ret, stats = profile(the_callable, *args, **kwargs)
764
710
if filename is None:
973
919
def run_bzr_catch_errors(argv):
974
"""Run a bzr command with parameters as described by argv.
920
# Note: The except clause logic below should be kept in sync with the
921
# profile() routine in lsprof.py.
924
except (KeyboardInterrupt, Exception), e:
925
# used to handle AssertionError and KeyboardInterrupt
926
# specially here, but hopefully they're handled ok by the logger now
927
exc_info = sys.exc_info()
928
exitcode = trace.report_exception(exc_info, sys.stderr)
929
if os.environ.get('BZR_PDB'):
930
print '**** entering debugger'
933
if sys.version_info[:2] < (2, 6):
935
# pdb.post_mortem(tb)
936
# but because pdb.post_mortem gives bad results for tracebacks
937
# from inside generators, we do it manually.
938
# (http://bugs.python.org/issue4150, fixed in Python 2.6)
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.
979
return exception_to_return_code(run_bzr, argv)
940
# Setup pdb on the traceback
943
p.setup(tb.tb_frame, tb)
944
# Point the debugger at the deepest frame of the stack
945
p.curindex = len(p.stack) - 1
946
p.curframe = p.stack[p.curindex]
947
# Start the pdb prompt.
948
p.print_stack_entry(p.stack[p.curindex])
982
956
def run_bzr_catch_user_errors(argv):