32
32
from warnings import warn
33
33
from inspect import getdoc
37
36
import bzrlib.trace
38
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
39
from bzrlib.errors import (BzrError,
37
from bzrlib.trace import mutter, note, log_error, warning
38
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError, NotBranchError
44
39
from bzrlib.revisionspec import RevisionSpec
45
40
from bzrlib import BZRDIR
46
41
from bzrlib.option import Option
59
54
if not plugin_cmds.has_key(k_unsquished):
60
55
plugin_cmds[k_unsquished] = cmd
61
56
mutter('registered plugin command %s', k_unsquished)
62
if decorate and k_unsquished in builtin_command_names():
63
return _builtin_commands()[k_unsquished]
65
result = plugin_cmds[k_unsquished]
66
plugin_cmds[k_unsquished] = cmd
69
58
log_error('Two plugins defined the same command: %r' % k)
70
59
log_error('Not loading the one in %r' % sys.modules[cmd.__module__])
168
157
List of argument forms, marked with whether they are optional,
173
['to_location', 'from_branch?', 'file*']
175
'to_location' is required
176
'from_branch' is optional
177
'file' can be specified 0 or more times
180
161
List of options that may be given for this command. These can
181
162
be either strings, referring to globally-defined options,
317
mutter(" got option %r", a)
299
mutter(" got option %r" % a)
319
301
optname, optarg = a[2:].split('=', 1)
322
304
if optname not in cmd_options:
323
raise BzrOptionError('unknown long option %r for command %s'
324
% (a, command.name()))
305
raise BzrCommandError('unknown long option %r for command %s'
327
309
if shortopt in Option.SHORT_OPTIONS:
357
if optname not in cmd_options:
358
raise BzrOptionError('unknown short option %r for command'
359
' %s' % (shortopt, command.name()))
360
339
if optname in opts:
361
340
# XXX: Do we ever want to support this, e.g. for -r?
362
341
raise BzrError('repeated option %r' % a)
447
426
os.remove(pfname)
450
def apply_lsprofiled(the_callable, *args, **kwargs):
451
from bzrlib.lsprof import profile
452
ret,stats = profile(the_callable,*args,**kwargs)
457
429
def run_bzr(argv):
458
430
"""Execute a command.
476
448
other behaviour.)
479
Run under the Python hotshot profiler.
482
Run under the Python lsprof profiler.
451
Run under the Python profiler.
453
# Load all of the transport methods
454
import bzrlib.transport.local, bzrlib.transport.http
484
456
argv = [a.decode(bzrlib.user_encoding) for a in argv]
486
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = False
458
opt_profile = opt_no_plugins = opt_builtin = False
488
460
# --no-plugins is handled specially at a very early stage. We need
489
461
# to load plugins before doing other command parsing so that they
526
494
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
530
ret = apply_lsprofiled(cmd_obj.run_argv, argv)
532
ret = apply_profiled(cmd_obj.run_argv, argv)
534
ret = cmd_obj.run_argv(argv)
537
# reset, in case we may do other commands later within the same process
540
def display_command(func):
541
"""Decorator that suppresses pipe/interrupt errors."""
542
def ignore_pipe(*args, **kwargs):
544
result = func(*args, **kwargs)
548
if not hasattr(e, 'errno'):
550
if e.errno != errno.EPIPE:
553
except KeyboardInterrupt:
497
ret = apply_profiled(cmd_obj.run_argv, argv)
499
ret = cmd_obj.run_argv(argv)
560
from bzrlib.ui.text import TextUIFactory
561
## bzrlib.trace.enable_default_logging()
562
505
bzrlib.trace.log_startup(argv)
563
bzrlib.ui.ui_factory = TextUIFactory()
564
ret = run_bzr_catch_errors(argv[1:])
565
mutter("return code %d", ret)
506
bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
508
return run_bzr_catch_errors(argv[1:])
569
511
def run_bzr_catch_errors(argv):
574
516
# do this here inside the exception wrappers to catch EPIPE
575
517
sys.stdout.flush()
518
except BzrCommandError, e:
519
# command line syntax error, etc
523
bzrlib.trace.log_exception()
525
except AssertionError, e:
526
bzrlib.trace.log_exception('assertion failed: ' + str(e))
528
except KeyboardInterrupt, e:
529
bzrlib.trace.log_exception('interrupted')
576
531
except Exception, e:
577
# used to handle AssertionError and KeyboardInterrupt
578
# specially here, but hopefully they're handled ok by the logger now
580
533
if (isinstance(e, IOError)
581
534
and hasattr(e, 'errno')
582
535
and e.errno == errno.EPIPE):
583
536
bzrlib.trace.note('broken pipe')
586
541
bzrlib.trace.log_exception()
587
if os.environ.get('BZR_PDB'):
588
print '**** entering debugger'
590
pdb.post_mortem(sys.exc_traceback)
593
544
if __name__ == '__main__':
594
545
sys.exit(main(sys.argv))