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 *
36
45
import bzrlib.trace
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
41
from bzrlib.option import Option
46
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
46
def register_command(cmd):
51
def register_command(cmd, decorate=False):
47
52
"Utility function to help register a command"
211
@deprecated_method(zero_eight)
192
212
def run_argv(self, argv):
193
"""Parse command line and run."""
194
args, opts = parse_args(self, argv)
213
"""Parse command line and run.
215
See run_argv_aliases for the 0.8 and beyond api.
217
return self.run_argv_aliases(argv)
219
def run_argv_aliases(self, argv, alias_argv=None):
220
"""Parse the command line and run with extra aliases in alias_argv."""
221
args, opts = parse_args(self, argv, alias_argv)
195
222
if 'help' in opts: # e.g. bzr add --help
196
223
from bzrlib.help import help_on_command
197
224
help_on_command(self.name())
279
306
# TODO: chop up this beast; make it a method of the Command
283
311
cmd_options = command.options()
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
313
proc_aliasarg = True # Are we processing alias_argv now?
314
for proc_argv in alias_argv, argv:
321
# We've received a standalone -- No more flags
325
# option names must not be unicode
329
mutter(" got option %r", a)
331
optname, optarg = a[2:].split('=', 1)
334
if optname not in cmd_options:
335
raise BzrOptionError('unknown long option %r for'
340
if shortopt in Option.SHORT_OPTIONS:
341
# Multi-character options must have a space to delimit
343
# ^^^ what does this mean? mbp 20051014
344
optname = Option.SHORT_OPTIONS[shortopt].name
346
# Single character short options, can be chained,
347
# and have their value appended to their name
349
if shortopt not in Option.SHORT_OPTIONS:
350
# We didn't find the multi-character name, and we
351
# didn't find the single char name
352
raise BzrError('unknown short option %r' % a)
353
optname = Option.SHORT_OPTIONS[shortopt].name
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:])
356
# There are extra things on this option
357
# see if it is the value, or if it is another
359
optargfn = Option.OPTIONS[optname].type
361
# This option does not take an argument, so the
362
# next entry is another short option, pack it
364
proc_argv.insert(0, '-' + a[2:])
366
# This option takes an argument, so pack it
370
if optname not in cmd_options:
371
raise BzrOptionError('unknown short option %r for'
373
(shortopt, command.name()))
375
# XXX: Do we ever want to support this, e.g. for -r?
377
raise BzrError('repeated option %r' % a)
378
elif optname in alias_opts:
379
# Replace what's in the alias with what's in the real
381
del alias_opts[optname]
383
proc_argv.insert(0, a)
386
raise BzrError('repeated option %r' % a)
388
option_obj = cmd_options[optname]
389
optargfn = option_obj.type
393
raise BzrError('option %r needs an argument' % a)
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)
343
option_obj = cmd_options[optname]
344
optargfn = option_obj.type
348
raise BzrError('option %r needs an argument' % a)
351
opts[optname] = optargfn(optarg)
395
optarg = proc_argv.pop(0)
396
opts[optname] = optargfn(optarg)
398
alias_opts[optname] = optargfn(optarg)
401
raise BzrError('option %r takes no argument' % optname)
404
alias_opts[optname] = True
354
raise BzrError('option %r takes no argument' % optname)
407
proc_aliasarg = False # Done with alias argv
358
408
return args, opts
444
517
Do not load plugin modules at all
447
523
Only use builtin commands. (Plugins are still allowed to change
448
524
other behaviour.)
451
Run under the Python profiler.
527
Run under the Python hotshot profiler.
530
Run under the Python lsprof profiler.
453
532
argv = [a.decode(bzrlib.user_encoding) for a in argv]
455
opt_profile = opt_no_plugins = opt_builtin = False
534
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = \
535
opt_no_aliases = False
536
opt_lsprof_file = None
457
538
# --no-plugins is handled specially at a very early stage. We need
458
539
# to load plugins before doing other command parsing so that they
459
540
# can override commands, but this needs to happen first.
462
546
if a == '--profile':
463
547
opt_profile = True
548
elif a == '--lsprof':
550
elif a == '--lsprof-file':
551
opt_lsprof_file = argv[i + 1]
464
553
elif a == '--no-plugins':
465
554
opt_no_plugins = True
555
elif a == '--no-aliases':
556
opt_no_aliases = True
466
557
elif a == '--builtin':
467
558
opt_builtin = True
559
elif a in ('--quiet', '-q'):
472
566
if (not argv) or (argv[0] == '--help'):
473
567
from bzrlib.help import help
474
568
if len(argv) > 1:
485
579
if not opt_no_plugins:
486
580
from bzrlib.plugin import load_plugins
583
from bzrlib.plugin import disable_plugins
588
if not opt_no_aliases:
589
alias_argv = get_alias(argv[0])
591
alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
592
argv[0] = alias_argv.pop(0)
489
594
cmd = str(argv.pop(0))
491
596
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
494
ret = apply_profiled(cmd_obj.run_argv, argv)
597
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
598
run = cmd_obj.run_argv
496
ret = cmd_obj.run_argv(argv)
601
run = cmd_obj.run_argv_aliases
602
run_argv = [argv, alias_argv]
606
ret = apply_lsprofiled(opt_lsprof_file, run, *run_argv)
608
ret = apply_profiled(run, *run_argv)
613
# reset, in case we may do other commands later within the same process
616
def display_command(func):
617
"""Decorator that suppresses pipe/interrupt errors."""
618
def ignore_pipe(*args, **kwargs):
620
result = func(*args, **kwargs)
624
if not hasattr(e, 'errno'):
626
if e.errno != errno.EPIPE:
629
except KeyboardInterrupt:
636
from bzrlib.ui.text import TextUIFactory
637
## bzrlib.trace.enable_default_logging()
502
638
bzrlib.trace.log_startup(argv)
503
bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
505
return run_bzr_catch_errors(argv[1:])
639
bzrlib.ui.ui_factory = TextUIFactory()
640
ret = run_bzr_catch_errors(argv[1:])
641
mutter("return code %d", ret)
508
645
def run_bzr_catch_errors(argv):
513
650
# do this here inside the exception wrappers to catch EPIPE
514
651
sys.stdout.flush()
515
except BzrCommandError, e:
516
# command line syntax error, etc
520
bzrlib.trace.log_exception()
522
except AssertionError, e:
523
bzrlib.trace.log_exception('assertion failed: ' + str(e))
525
except KeyboardInterrupt, e:
526
bzrlib.trace.log_exception('interrupted')
528
652
except Exception, e:
653
# used to handle AssertionError and KeyboardInterrupt
654
# specially here, but hopefully they're handled ok by the logger now
530
656
if (isinstance(e, IOError)
531
657
and hasattr(e, 'errno')
532
658
and e.errno == errno.EPIPE):
533
659
bzrlib.trace.note('broken pipe')
538
662
bzrlib.trace.log_exception()
663
if os.environ.get('BZR_PDB'):
664
print '**** entering debugger'
666
pdb.post_mortem(sys.exc_traceback)
541
669
if __name__ == '__main__':
542
670
sys.exit(main(sys.argv))