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,
37
from bzrlib.trace import mutter, note, log_error, warning
38
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError, NotBranchError
39
from bzrlib.revisionspec import RevisionSpec
40
from bzrlib import BZRDIR
42
41
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.
46
def register_command(cmd):
47
"Utility function to help register a command"
61
50
if k.startswith("cmd_"):
216
@deprecated_method(zero_eight)
217
192
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)
193
"""Parse command line and run."""
194
args, opts = parse_args(self, argv)
227
195
if 'help' in opts: # e.g. bzr add --help
228
196
from bzrlib.help import help_on_command
229
197
help_on_command(self.name())
311
279
# TODO: chop up this beast; make it a method of the Command
316
283
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
291
# We've received a standalone -- No more flags
295
# option names must not be unicode
299
mutter(" got option %r" % a)
301
optname, optarg = a[2:].split('=', 1)
304
if optname not in cmd_options:
305
raise BzrCommandError('unknown long option %r for command %s'
309
if shortopt in Option.SHORT_OPTIONS:
310
# Multi-character options must have a space to delimit
312
# ^^^ what does this mean? mbp 20051014
313
optname = Option.SHORT_OPTIONS[shortopt].name
315
# Single character short options, can be chained,
316
# and have their value appended to their name
318
if shortopt not in Option.SHORT_OPTIONS:
319
# We didn't find the multi-character name, and we
320
# didn't find the single char name
321
raise BzrError('unknown short option %r' % a)
322
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
325
# There are extra things on this option
326
# see if it is the value, or if it is another
328
optargfn = Option.OPTIONS[optname].type
330
# This option does not take an argument, so the
331
# next entry is another short option, pack it back
333
argv.insert(0, '-' + a[2:])
335
# This option takes an argument, so pack it
340
# XXX: Do we ever want to support this, e.g. for -r?
341
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)
343
option_obj = cmd_options[optname]
344
optargfn = option_obj.type
348
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
351
opts[optname] = optargfn(optarg)
412
proc_aliasarg = False # Done with alias argv
354
raise BzrError('option %r takes no argument' % optname)
413
358
return args, opts
522
444
Do not load plugin modules at all
528
447
Only use builtin commands. (Plugins are still allowed to change
529
448
other behaviour.)
532
Run under the Python hotshot profiler.
535
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
537
456
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
458
opt_profile = opt_no_plugins = opt_builtin = False
543
460
# --no-plugins is handled specially at a very early stage. We need
544
461
# to load plugins before doing other command parsing so that they
545
462
# can override commands, but this needs to happen first.
551
465
if a == '--profile':
552
466
opt_profile = True
553
elif a == '--lsprof':
555
elif a == '--lsprof-file':
556
opt_lsprof_file = argv[i + 1]
558
467
elif a == '--no-plugins':
559
468
opt_no_plugins = True
560
elif a == '--no-aliases':
561
opt_no_aliases = True
562
469
elif a == '--builtin':
563
470
opt_builtin = True
564
elif a in ('--quiet', '-q'):
572
from bzrlib.builtins import cmd_help
573
cmd_help().run_argv_aliases([])
475
if (not argv) or (argv[0] == '--help'):
476
from bzrlib.help import help
576
483
if argv[0] == '--version':
581
488
if not opt_no_plugins:
582
489
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
492
cmd = str(argv.pop(0))
598
494
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
497
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
618
def display_command(func):
619
"""Decorator that suppresses pipe/interrupt errors."""
620
def ignore_pipe(*args, **kwargs):
622
result = func(*args, **kwargs)
626
if not hasattr(e, 'errno'):
628
if e.errno != errno.EPIPE:
631
except KeyboardInterrupt:
499
ret = cmd_obj.run_argv(argv)
638
from bzrlib.ui.text import TextUIFactory
639
## bzrlib.trace.enable_default_logging()
640
505
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)
506
bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
508
return run_bzr_catch_errors(argv[1:])
647
511
def run_bzr_catch_errors(argv):
652
516
# do this here inside the exception wrappers to catch EPIPE
653
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')
654
531
except Exception, e:
655
# used to handle AssertionError and KeyboardInterrupt
656
# specially here, but hopefully they're handled ok by the logger now
658
533
if (isinstance(e, IOError)
659
534
and hasattr(e, 'errno')
660
535
and e.errno == errno.EPIPE):
661
536
bzrlib.trace.note('broken pipe')
664
541
bzrlib.trace.log_exception()
665
if os.environ.get('BZR_PDB'):
666
print '**** entering debugger'
668
pdb.post_mortem(sys.exc_traceback)
671
544
if __name__ == '__main__':
672
545
sys.exit(main(sys.argv))