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
38
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
39
from bzrlib.errors import (BzrError,
38
from bzrlib.errors import (BzrError,
43
from bzrlib.option import Option
44
44
from bzrlib.revisionspec import RevisionSpec
45
from bzrlib import BZRDIR
46
from bzrlib.option import Option
45
from bzrlib.symbol_versioning import *
47
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
51
52
def register_command(cmd, decorate=False):
52
"Utility function to help register a command"
53
"""Utility function to help register a command
55
:param cmd: Command subclass to register
56
:param decorate: If true, allow overriding an existing command
57
of the same name; the old command is returned by this function.
58
Otherwise it is an error to try to override an existing command.
55
62
if k.startswith("cmd_"):
228
def _setup_outf(self):
229
"""Return a file linked to stdout, which has proper encoding."""
230
assert self.encoding_type in ['strict', 'exact', 'replace']
232
# Originally I was using self.stdout, but that looks
233
# *way* too much like sys.stdout
234
if self.encoding_type == 'exact':
235
self.outf = sys.stdout
238
output_encoding = getattr(sys.stdout, 'encoding', None)
239
if not output_encoding:
240
input_encoding = getattr(sys.stdin, 'encoding', None)
241
if not input_encoding:
242
output_encoding = bzrlib.user_encoding
243
mutter('encoding stdout as bzrlib.user_encoding %r', output_encoding)
245
output_encoding = input_encoding
246
mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
248
mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
250
# use 'replace' so that we don't abort if trying to write out
251
# in e.g. the default C locale.
252
self.outf = codecs.getwriter(output_encoding)(sys.stdout, errors=self.encoding_type)
253
# For whatever reason codecs.getwriter() does not advertise its encoding
254
# it just returns the encoding of the wrapped file, which is completely
255
# bogus. So set the attribute, so we can find the correct encoding later.
256
self.outf.encoding = output_encoding
258
@deprecated_method(zero_eight)
211
259
def run_argv(self, argv):
212
"""Parse command line and run."""
213
args, opts = parse_args(self, argv)
260
"""Parse command line and run.
262
See run_argv_aliases for the 0.8 and beyond api.
264
return self.run_argv_aliases(argv)
266
def run_argv_aliases(self, argv, alias_argv=None):
267
"""Parse the command line and run with extra aliases in alias_argv."""
268
args, opts = parse_args(self, argv, alias_argv)
214
269
if 'help' in opts: # e.g. bzr add --help
215
270
from bzrlib.help import help_on_command
216
271
help_on_command(self.name())
297
355
# TODO: chop up this beast; make it a method of the Command
301
360
cmd_options = command.options()
309
# We've received a standalone -- No more flags
313
# option names must not be unicode
317
mutter(" got option %r", a)
319
optname, optarg = a[2:].split('=', 1)
322
if optname not in cmd_options:
323
raise BzrOptionError('unknown long option %r for command %s'
324
% (a, command.name()))
327
if shortopt in Option.SHORT_OPTIONS:
328
# Multi-character options must have a space to delimit
330
# ^^^ what does this mean? mbp 20051014
331
optname = Option.SHORT_OPTIONS[shortopt].name
333
# Single character short options, can be chained,
334
# and have their value appended to their name
336
if shortopt not in Option.SHORT_OPTIONS:
337
# We didn't find the multi-character name, and we
338
# didn't find the single char name
339
raise BzrError('unknown short option %r' % a)
340
optname = Option.SHORT_OPTIONS[shortopt].name
362
proc_aliasarg = True # Are we processing alias_argv now?
363
for proc_argv in alias_argv, argv:
370
# We've received a standalone -- No more flags
374
# option names must not be unicode
378
mutter(" got option %r", a)
380
optname, optarg = a[2:].split('=', 1)
383
if optname not in cmd_options:
384
raise BzrOptionError('unknown long option %r for'
389
if shortopt in Option.SHORT_OPTIONS:
390
# Multi-character options must have a space to delimit
392
# ^^^ what does this mean? mbp 20051014
393
optname = Option.SHORT_OPTIONS[shortopt].name
395
# Single character short options, can be chained,
396
# and have their value appended to their name
398
if shortopt not in Option.SHORT_OPTIONS:
399
# We didn't find the multi-character name, and we
400
# didn't find the single char name
401
raise BzrError('unknown short option %r' % a)
402
optname = Option.SHORT_OPTIONS[shortopt].name
343
# There are extra things on this option
344
# see if it is the value, or if it is another
346
optargfn = Option.OPTIONS[optname].type
348
# This option does not take an argument, so the
349
# next entry is another short option, pack it back
351
argv.insert(0, '-' + a[2:])
405
# There are extra things on this option
406
# see if it is the value, or if it is another
408
optargfn = Option.OPTIONS[optname].type
410
# This option does not take an argument, so the
411
# next entry is another short option, pack it
413
proc_argv.insert(0, '-' + a[2:])
415
# This option takes an argument, so pack it
419
if optname not in cmd_options:
420
raise BzrOptionError('unknown short option %r for'
422
(shortopt, command.name()))
424
# XXX: Do we ever want to support this, e.g. for -r?
426
raise BzrError('repeated option %r' % a)
427
elif optname in alias_opts:
428
# Replace what's in the alias with what's in the real
430
del alias_opts[optname]
432
proc_argv.insert(0, a)
435
raise BzrError('repeated option %r' % a)
437
option_obj = cmd_options[optname]
438
optargfn = option_obj.type
442
raise BzrError('option %r needs an argument' % a)
353
# This option takes an argument, so pack it
357
if optname not in cmd_options:
358
raise BzrOptionError('unknown short option %r for command'
359
' %s' % (shortopt, command.name()))
361
# XXX: Do we ever want to support this, e.g. for -r?
362
raise BzrError('repeated option %r' % a)
364
option_obj = cmd_options[optname]
365
optargfn = option_obj.type
369
raise BzrError('option %r needs an argument' % a)
372
opts[optname] = optargfn(optarg)
444
optarg = proc_argv.pop(0)
445
opts[optname] = optargfn(optarg)
447
alias_opts[optname] = optargfn(optarg)
450
raise BzrError('option %r takes no argument' % optname)
453
alias_opts[optname] = True
375
raise BzrError('option %r takes no argument' % optname)
456
proc_aliasarg = False # Done with alias argv
379
457
return args, opts
447
525
os.remove(pfname)
450
def apply_lsprofiled(the_callable, *args, **kwargs):
528
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
451
529
from bzrlib.lsprof import profile
452
ret,stats = profile(the_callable,*args,**kwargs)
531
ret, stats = profile(the_callable, *args, **kwargs)
537
cPickle.dump(stats, open(filename, 'w'), 2)
538
print 'Profile data written to %r.' % filename
543
"""Return an expanded alias, or None if no alias exists"""
545
alias = bzrlib.config.GlobalConfig().get_alias(cmd)
547
return alias.split(' ')
457
551
def run_bzr(argv):
458
552
"""Execute a command.
482
581
Run under the Python lsprof profiler.
484
argv = [a.decode(bzrlib.user_encoding) for a in argv]
486
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = False
585
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = \
586
opt_no_aliases = False
587
opt_lsprof_file = None
488
589
# --no-plugins is handled specially at a very early stage. We need
489
590
# to load plugins before doing other command parsing so that they
490
591
# can override commands, but this needs to happen first.
493
597
if a == '--profile':
494
598
opt_profile = True
495
599
elif a == '--lsprof':
496
600
opt_lsprof = True
601
elif a == '--lsprof-file':
602
opt_lsprof_file = argv[i + 1]
497
604
elif a == '--no-plugins':
498
605
opt_no_plugins = True
606
elif a == '--no-aliases':
607
opt_no_aliases = True
499
608
elif a == '--builtin':
500
609
opt_builtin = True
501
610
elif a in ('--quiet', '-q'):
507
if (not argv) or (argv[0] == '--help'):
508
from bzrlib.help import help
618
from bzrlib.builtins import cmd_help
619
cmd_help().run_argv_aliases([])
515
622
if argv[0] == '--version':
520
627
if not opt_no_plugins:
521
628
from bzrlib.plugin import load_plugins
631
from bzrlib.plugin import disable_plugins
636
if not opt_no_aliases:
637
alias_argv = get_alias(argv[0])
639
alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
640
argv[0] = alias_argv.pop(0)
524
642
cmd = str(argv.pop(0))
526
644
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
645
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
646
run = cmd_obj.run_argv
649
run = cmd_obj.run_argv_aliases
650
run_argv = [argv, alias_argv]
530
ret = apply_lsprofiled(cmd_obj.run_argv, argv)
654
ret = apply_lsprofiled(opt_lsprof_file, run, *run_argv)
531
655
elif opt_profile:
532
ret = apply_profiled(cmd_obj.run_argv, argv)
656
ret = apply_profiled(run, *run_argv)
534
ret = cmd_obj.run_argv(argv)
537
661
# reset, in case we may do other commands later within the same process