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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
# TODO: probably should say which arguments are candidates for glob
51
51
from bzrlib import registry
53
from bzrlib.hooks import Hooks
53
from bzrlib.hooks import HookPoint, Hooks
54
54
from bzrlib.option import Option
139
139
real_name = _unsquish_command_name(name)
140
140
r[real_name] = builtins[name]
144
144
def builtin_command_names():
145
145
"""Return list of builtin command names."""
146
146
return _builtin_commands().keys()
149
149
def plugin_command_names():
150
150
return plugin_cmds.keys()
157
157
d.update(plugin_cmds.iteritems())
161
161
def get_all_cmds(plugins_override=True):
162
162
"""Return canonical name and class for all registered commands."""
163
163
for k, v in _get_cmd_dict(plugins_override=plugins_override).iteritems():
302
302
def _maybe_expand_globs(self, file_list):
303
303
"""Glob expand file_list if the platform does not do that itself.
305
305
:return: A possibly empty list of unicode paths.
307
307
Introduced in bzrlib 0.18.
333
333
def get_help_text(self, additional_see_also=None, plain=True,
334
see_also_as_links=False):
334
see_also_as_links=False, verbose=True):
335
335
"""Return a text string with help for this command.
337
337
:param additional_see_also: Additional help topics to be
338
338
cross-referenced.
339
339
:param plain: if False, raw help (reStructuredText) is
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.
344
348
doc = self.help()
374
378
result += options
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)
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)
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])
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"
391
400
# Add the aliases, source (plug-in) and see also links, if any
464
473
def get_see_also(self, additional_terms=None):
465
474
"""Return a list of help topics that are related to this command.
467
476
The list is derived from the content of the _see_also attribute. Any
468
477
duplicates are removed and the result is in lexical order.
469
478
:param additional_terms: Additional help topics to cross-reference.
523
532
if 'help' in opts: # e.g. bzr add --help
524
533
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))
526
538
trace.set_verbosity_level(option._verbosity_level)
527
539
if 'verbose' in self.supported_std_options:
528
540
opts['verbose'] = trace.is_verbose()
593
605
Hooks.__init__(self)
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'] = []
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))
600
611
Command.hooks = CommandHooks()
603
614
def parse_args(command, argv, alias_argv=None):
604
615
"""Parse command line.
606
617
Arguments and options are parsed at this level before being passed
607
618
down to specific command handlers. This routine knows, from a
608
619
lookup table, something about the available options, what optargs
672
683
tracer = trace.Trace(count=1, trace=0)
673
684
sys.settrace(tracer.globaltrace)
675
ret = the_callable(*args, **kwargs)
678
results = tracer.results()
679
results.write_results(show_missing=1, summary=False,
687
return exception_to_return_code(the_callable, *args, **kwargs)
690
results = tracer.results()
691
results.write_results(show_missing=1, summary=False,
683
695
def apply_profiled(the_callable, *args, **kwargs):
689
701
prof = hotshot.Profile(pfname)
691
ret = prof.runcall(the_callable, *args, **kwargs) or 0
703
ret = prof.runcall(exception_to_return_code, the_callable, *args,
694
707
stats = hotshot.stats.load(pfname)
703
716
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])
706
760
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
707
761
from bzrlib.lsprof import profile
708
ret, stats = profile(the_callable, *args, **kwargs)
762
ret, stats = profile(exception_to_return_code, the_callable, *args, **kwargs)
710
764
if filename is None:
746
800
The command-line arguments, without the program name from argv[0]
747
801
These should already be decoded. All library/test code calling
748
802
run_bzr should be passing valid strings (don't need decoding).
750
804
Returns a command status or raises an exception.
752
806
Special master options: these must come before the command because
917
973
def run_bzr_catch_errors(argv):
918
# Note: The except clause logic below should be kept in sync with the
919
# profile() routine in lsprof.py.
922
except (KeyboardInterrupt, Exception), e:
923
# used to handle AssertionError and KeyboardInterrupt
924
# specially here, but hopefully they're handled ok by the logger now
925
exc_info = sys.exc_info()
926
exitcode = trace.report_exception(exc_info, sys.stderr)
927
if os.environ.get('BZR_PDB'):
928
print '**** entering debugger'
931
if sys.version_info[:2] < (2, 6):
933
# pdb.post_mortem(tb)
934
# but because pdb.post_mortem gives bad results for tracebacks
935
# from inside generators, we do it manually.
936
# (http://bugs.python.org/issue4150, fixed in Python 2.6)
938
# Setup pdb on the traceback
941
p.setup(tb.tb_frame, tb)
942
# Point the debugger at the deepest frame of the stack
943
p.curindex = len(p.stack) - 1
944
p.curframe = p.stack[p.curindex]
945
# Start the pdb prompt.
946
p.print_stack_entry(p.stack[p.curindex])
974
"""Run a bzr command with parameters as described by argv.
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)
954
982
def run_bzr_catch_user_errors(argv):
997
1025
def plugin_for_command(self, cmd_name):
998
1026
'''Takes a command and returns the information for that plugin
1000
:return: A dictionary with all the available information
1028
:return: A dictionary with all the available information
1001
1029
for the requested plugin
1003
1031
raise NotImplementedError