1
# Copyright (C) 2004, 2005 by Canonical Ltd
1
# Copyright (C) 2006 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
32
33
from warnings import warn
33
from inspect import getdoc
37
from bzrlib.errors import (BzrError,
42
from bzrlib.option import Option
43
from bzrlib.revisionspec import RevisionSpec
44
from bzrlib.symbol_versioning import *
37
45
import bzrlib.trace
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
from bzrlib.option import Option
46
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
47
def register_command(cmd):
48
"Utility function to help register a command"
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.
51
61
if k.startswith("cmd_"):
216
@deprecated_method(zero_eight)
193
217
def run_argv(self, argv):
194
"""Parse command line and run."""
195
args, opts = parse_args(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)
196
227
if 'help' in opts: # e.g. bzr add --help
197
228
from bzrlib.help import help_on_command
198
229
help_on_command(self.name())
280
311
# TODO: chop up this beast; make it a method of the Command
284
316
cmd_options = command.options()
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
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
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:])
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
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)
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)
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)
344
option_obj = cmd_options[optname]
345
optargfn = option_obj.type
349
raise BzrError('option %r needs an argument' % a)
352
opts[optname] = optargfn(optarg)
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
355
raise BzrError('option %r takes no argument' % optname)
412
proc_aliasarg = False # Done with alias argv
359
413
return args, opts
445
522
Do not load plugin modules at all
448
528
Only use builtin commands. (Plugins are still allowed to change
449
529
other behaviour.)
452
Run under the Python profiler.
532
Run under the Python hotshot profiler.
535
Run under the Python lsprof profiler.
454
537
argv = [a.decode(bzrlib.user_encoding) for a in argv]
456
opt_profile = opt_no_plugins = opt_builtin = False
539
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = \
540
opt_no_aliases = False
541
opt_lsprof_file = None
458
543
# --no-plugins is handled specially at a very early stage. We need
459
544
# to load plugins before doing other command parsing so that they
460
545
# can override commands, but this needs to happen first.
463
551
if a == '--profile':
464
552
opt_profile = True
553
elif a == '--lsprof':
555
elif a == '--lsprof-file':
556
opt_lsprof_file = argv[i + 1]
465
558
elif a == '--no-plugins':
466
559
opt_no_plugins = True
560
elif a == '--no-aliases':
561
opt_no_aliases = True
467
562
elif a == '--builtin':
468
563
opt_builtin = True
564
elif a in ('--quiet', '-q'):
473
if (not argv) or (argv[0] == '--help'):
474
from bzrlib.help import help
572
from bzrlib.builtins import cmd_help
573
cmd_help().run_argv_aliases([])
481
576
if argv[0] == '--version':
486
581
if not opt_no_plugins:
487
582
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)
490
596
cmd = str(argv.pop(0))
492
598
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
495
ret = apply_profiled(cmd_obj.run_argv, argv)
599
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
600
run = cmd_obj.run_argv
497
ret = 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
500
618
def display_command(func):
619
"""Decorator that suppresses pipe/interrupt errors."""
501
620
def ignore_pipe(*args, **kwargs):
503
func(*args, **kwargs)
622
result = func(*args, **kwargs)
504
625
except IOError, e:
626
if not hasattr(e, 'errno'):
505
628
if e.errno != errno.EPIPE:
631
except KeyboardInterrupt:
507
633
return ignore_pipe
638
from bzrlib.ui.text import TextUIFactory
639
## bzrlib.trace.enable_default_logging()
511
640
bzrlib.trace.log_startup(argv)
512
bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
514
return run_bzr_catch_errors(argv[1:])
641
bzrlib.ui.ui_factory = TextUIFactory()
642
ret = run_bzr_catch_errors(argv[1:])
643
mutter("return code %d", ret)
517
647
def run_bzr_catch_errors(argv):
522
652
# do this here inside the exception wrappers to catch EPIPE
523
653
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')
537
654
except Exception, e:
655
# used to handle AssertionError and KeyboardInterrupt
656
# specially here, but hopefully they're handled ok by the logger now
539
658
if (isinstance(e, IOError)
540
659
and hasattr(e, 'errno')
541
660
and e.errno == errno.EPIPE):
542
661
bzrlib.trace.note('broken pipe')
547
664
bzrlib.trace.log_exception()
665
if os.environ.get('BZR_PDB'):
666
print '**** entering debugger'
668
pdb.post_mortem(sys.exc_traceback)
550
671
if __name__ == '__main__':
551
672
sys.exit(main(sys.argv))