1
# Copyright (C) 2006 by Canonical Ltd
1
# Copyright (C) 2004, 2005 by Canonical Ltd
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
32
from warnings import warn
33
from inspect import getdoc
37
from bzrlib.errors import (BzrError,
38
from bzrlib.trace import mutter, note, log_error, warning
39
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError, NotBranchError
40
from bzrlib.revisionspec import RevisionSpec
41
from bzrlib import BZRDIR
42
42
from bzrlib.option import Option
43
from bzrlib.revisionspec import RevisionSpec
44
from bzrlib.symbol_versioning import *
46
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
51
def register_command(cmd, decorate=False):
52
"""Utility function to help register a command
54
:param cmd: Command subclass to register
55
:param decorate: If true, allow overriding an existing command
56
of the same name; the old command is returned by this function.
57
Otherwise it is an error to try to override an existing command.
47
def register_command(cmd):
48
"Utility function to help register a command"
61
51
if k.startswith("cmd_"):
216
@deprecated_method(zero_eight)
217
193
def run_argv(self, argv):
218
"""Parse command line and run.
220
See run_argv_aliases for the 0.8 and beyond api.
222
return self.run_argv_aliases(argv)
224
def run_argv_aliases(self, argv, alias_argv=None):
225
"""Parse the command line and run with extra aliases in alias_argv."""
226
args, opts = parse_args(self, argv, alias_argv)
194
"""Parse command line and run."""
195
args, opts = parse_args(self, argv)
227
196
if 'help' in opts: # e.g. bzr add --help
228
197
from bzrlib.help import help_on_command
229
198
help_on_command(self.name())
311
280
# TODO: chop up this beast; make it a method of the Command
316
284
cmd_options = command.options()
318
proc_aliasarg = True # Are we processing alias_argv now?
319
for proc_argv in alias_argv, argv:
326
# We've received a standalone -- No more flags
330
# option names must not be unicode
334
mutter(" got option %r", a)
336
optname, optarg = a[2:].split('=', 1)
339
if optname not in cmd_options:
340
raise BzrOptionError('unknown long option %r for'
345
if shortopt in Option.SHORT_OPTIONS:
346
# Multi-character options must have a space to delimit
348
# ^^^ what does this mean? mbp 20051014
349
optname = Option.SHORT_OPTIONS[shortopt].name
351
# Single character short options, can be chained,
352
# and have their value appended to their name
354
if shortopt not in Option.SHORT_OPTIONS:
355
# We didn't find the multi-character name, and we
356
# didn't find the single char name
357
raise BzrError('unknown short option %r' % a)
358
optname = Option.SHORT_OPTIONS[shortopt].name
292
# We've received a standalone -- No more flags
296
# option names must not be unicode
300
mutter(" got option %r" % a)
302
optname, optarg = a[2:].split('=', 1)
305
if optname not in cmd_options:
306
raise BzrCommandError('unknown long option %r for command %s'
307
% (a, command.name()))
310
if shortopt in Option.SHORT_OPTIONS:
311
# Multi-character options must have a space to delimit
313
# ^^^ what does this mean? mbp 20051014
314
optname = Option.SHORT_OPTIONS[shortopt].name
316
# Single character short options, can be chained,
317
# and have their value appended to their name
319
if shortopt not in Option.SHORT_OPTIONS:
320
# We didn't find the multi-character name, and we
321
# didn't find the single char name
322
raise BzrError('unknown short option %r' % a)
323
optname = Option.SHORT_OPTIONS[shortopt].name
361
# There are extra things on this option
362
# see if it is the value, or if it is another
364
optargfn = Option.OPTIONS[optname].type
366
# This option does not take an argument, so the
367
# next entry is another short option, pack it
369
proc_argv.insert(0, '-' + a[2:])
371
# This option takes an argument, so pack it
326
# There are extra things on this option
327
# see if it is the value, or if it is another
329
optargfn = Option.OPTIONS[optname].type
331
# This option does not take an argument, so the
332
# next entry is another short option, pack it back
334
argv.insert(0, '-' + a[2:])
336
# This option takes an argument, so pack it
341
# XXX: Do we ever want to support this, e.g. for -r?
342
raise BzrError('repeated option %r' % a)
375
if optname not in cmd_options:
376
raise BzrOptionError('unknown short option %r for'
378
(shortopt, command.name()))
380
# XXX: Do we ever want to support this, e.g. for -r?
382
raise BzrError('repeated option %r' % a)
383
elif optname in alias_opts:
384
# Replace what's in the alias with what's in the real
386
del alias_opts[optname]
388
proc_argv.insert(0, a)
344
option_obj = cmd_options[optname]
345
optargfn = option_obj.type
349
raise BzrError('option %r needs an argument' % a)
391
raise BzrError('repeated option %r' % a)
393
option_obj = cmd_options[optname]
394
optargfn = option_obj.type
398
raise BzrError('option %r needs an argument' % a)
400
optarg = proc_argv.pop(0)
401
opts[optname] = optargfn(optarg)
403
alias_opts[optname] = optargfn(optarg)
406
raise BzrError('option %r takes no argument' % optname)
409
alias_opts[optname] = True
352
opts[optname] = optargfn(optarg)
412
proc_aliasarg = False # Done with alias argv
355
raise BzrError('option %r takes no argument' % optname)
413
359
return args, opts
522
445
Do not load plugin modules at all
528
448
Only use builtin commands. (Plugins are still allowed to change
529
449
other behaviour.)
532
Run under the Python hotshot profiler.
535
Run under the Python lsprof profiler.
452
Run under the Python profiler.
537
454
argv = [a.decode(bzrlib.user_encoding) for a in argv]
539
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = \
540
opt_no_aliases = False
541
opt_lsprof_file = None
456
opt_profile = opt_no_plugins = opt_builtin = False
543
458
# --no-plugins is handled specially at a very early stage. We need
544
459
# to load plugins before doing other command parsing so that they
545
460
# can override commands, but this needs to happen first.
551
463
if a == '--profile':
552
464
opt_profile = True
553
elif a == '--lsprof':
555
elif a == '--lsprof-file':
556
opt_lsprof_file = argv[i + 1]
558
465
elif a == '--no-plugins':
559
466
opt_no_plugins = True
560
elif a == '--no-aliases':
561
opt_no_aliases = True
562
467
elif a == '--builtin':
563
468
opt_builtin = True
564
elif a in ('--quiet', '-q'):
572
from bzrlib.builtins import cmd_help
573
cmd_help().run_argv_aliases([])
473
if (not argv) or (argv[0] == '--help'):
474
from bzrlib.help import help
576
481
if argv[0] == '--version':
581
486
if not opt_no_plugins:
582
487
from bzrlib.plugin import load_plugins
585
from bzrlib.plugin import disable_plugins
590
if not opt_no_aliases:
591
alias_argv = get_alias(argv[0])
593
alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
594
argv[0] = alias_argv.pop(0)
596
490
cmd = str(argv.pop(0))
598
492
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
599
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
600
run = cmd_obj.run_argv
495
ret = apply_profiled(cmd_obj.run_argv, argv)
603
run = cmd_obj.run_argv_aliases
604
run_argv = [argv, alias_argv]
608
ret = apply_lsprofiled(opt_lsprof_file, run, *run_argv)
610
ret = apply_profiled(run, *run_argv)
615
# reset, in case we may do other commands later within the same process
497
ret = cmd_obj.run_argv(argv)
618
500
def display_command(func):
619
"""Decorator that suppresses pipe/interrupt errors."""
620
501
def ignore_pipe(*args, **kwargs):
622
result = func(*args, **kwargs)
503
func(*args, **kwargs)
625
504
except IOError, e:
626
if not hasattr(e, 'errno'):
628
505
if e.errno != errno.EPIPE:
631
except KeyboardInterrupt:
633
507
return ignore_pipe
638
from bzrlib.ui.text import TextUIFactory
639
## bzrlib.trace.enable_default_logging()
640
511
bzrlib.trace.log_startup(argv)
641
bzrlib.ui.ui_factory = TextUIFactory()
642
ret = run_bzr_catch_errors(argv[1:])
643
mutter("return code %d", ret)
512
bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
514
return run_bzr_catch_errors(argv[1:])
647
517
def run_bzr_catch_errors(argv):
652
522
# do this here inside the exception wrappers to catch EPIPE
653
523
sys.stdout.flush()
524
except BzrCommandError, e:
525
# command line syntax error, etc
529
bzrlib.trace.log_exception()
531
except AssertionError, e:
532
bzrlib.trace.log_exception('assertion failed: ' + str(e))
534
except KeyboardInterrupt, e:
535
bzrlib.trace.log_exception('interrupted')
654
537
except Exception, e:
655
# used to handle AssertionError and KeyboardInterrupt
656
# specially here, but hopefully they're handled ok by the logger now
658
539
if (isinstance(e, IOError)
659
540
and hasattr(e, 'errno')
660
541
and e.errno == errno.EPIPE):
661
542
bzrlib.trace.note('broken pipe')
664
547
bzrlib.trace.log_exception()
665
if os.environ.get('BZR_PDB'):
666
print '**** entering debugger'
668
pdb.post_mortem(sys.exc_traceback)
671
550
if __name__ == '__main__':
672
551
sys.exit(main(sys.argv))