28
28
# TODO: "--profile=cum", to change sort order. Is there any value in leaving
29
29
# the profile output behind so it can be interactively examined?
34
from bzrlib.lazy_import import lazy_import
35
lazy_import(globals(), """
33
38
from warnings import warn
37
from bzrlib.errors import (BzrError,
50
from bzrlib.symbol_versioning import (
42
57
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
128
140
from bzrlib.externalcommand import ExternalCommand
130
cmd_name = str(cmd_name) # not unicode
142
# We want only 'ascii' command names, but the user may have typed
143
# in a Unicode name. In that case, they should just get a
144
# 'command not found' error later.
145
# In the future, we may actually support Unicode command names.
132
147
# first look up this command under the specified name
133
148
cmds = _get_cmd_dict(plugins_override=plugins_override)
190
205
If true, this command isn't advertised. This is typically
191
206
for commands intended for expert users.
209
Command objects will get a 'outf' attribute, which has been
210
setup to properly handle encoding of unicode strings.
211
encoding_type determines what will happen when characters cannot
213
strict - abort if we cannot decode
214
replace - put in a bogus character (typically '?')
215
exact - do not encode sys.stdout
217
NOTE: by default on Windows, sys.stdout is opened as a text
218
stream, therefore LF line-endings are converted to CRLF.
219
When a command uses encoding_type = 'exact', then
220
sys.stdout is forced to be a binary stream, and line-endings
195
226
takes_options = []
227
encoding_type = 'strict'
207
239
Maps from long option name to option object."""
209
r['help'] = Option.OPTIONS['help']
241
r['help'] = option.Option.OPTIONS['help']
210
242
for o in self.takes_options:
211
if not isinstance(o, Option):
212
o = Option.OPTIONS[o]
243
if isinstance(o, basestring):
244
o = option.Option.OPTIONS[o]
248
def _setup_outf(self):
249
"""Return a file linked to stdout, which has proper encoding."""
250
assert self.encoding_type in ['strict', 'exact', 'replace']
252
# Originally I was using self.stdout, but that looks
253
# *way* too much like sys.stdout
254
if self.encoding_type == 'exact':
255
# force sys.stdout to be binary stream on win32
256
if sys.platform == 'win32':
257
fileno = getattr(sys.stdout, 'fileno', None)
260
msvcrt.setmode(fileno(), os.O_BINARY)
261
self.outf = sys.stdout
264
output_encoding = osutils.get_terminal_encoding()
266
# use 'replace' so that we don't abort if trying to write out
267
# in e.g. the default C locale.
268
self.outf = codecs.getwriter(output_encoding)(sys.stdout, errors=self.encoding_type)
269
# For whatever reason codecs.getwriter() does not advertise its encoding
270
# it just returns the encoding of the wrapped file, which is completely
271
# bogus. So set the attribute, so we can find the correct encoding later.
272
self.outf.encoding = output_encoding
216
274
@deprecated_method(zero_eight)
217
275
def run_argv(self, argv):
218
276
"""Parse command line and run.
224
282
def run_argv_aliases(self, argv, alias_argv=None):
225
283
"""Parse the command line and run with extra aliases in alias_argv."""
285
warn("Passing None for [] is deprecated from bzrlib 0.10",
286
DeprecationWarning, stacklevel=2)
226
288
args, opts = parse_args(self, argv, alias_argv)
227
289
if 'help' in opts: # e.g. bzr add --help
228
290
from bzrlib.help import help_on_command
229
291
help_on_command(self.name())
231
# XXX: This should be handled by the parser
232
allowed_names = self.options().keys()
234
if oname not in allowed_names:
235
raise BzrCommandError("option '--%s' is not allowed for"
236
" command %r" % (oname, self.name()))
237
293
# mix arguments and options into one dictionary
238
294
cmdargs = _match_argform(self.name(), self.takes_args, args)
308
380
lookup table, something about the available options, what optargs
309
381
they take, and which commands will accept them.
311
# TODO: chop up this beast; make it a method of the Command
316
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
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)
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
412
proc_aliasarg = False # Done with alias argv
383
# TODO: make it a method of the Command?
384
parser = option.get_optparser(command.options())
385
if alias_argv is not None:
386
args = alias_argv + argv
390
options, args = parser.parse_args(args)
391
opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
392
v is not option.OptionParser.DEFAULT_VALUE])
413
393
return args, opts
430
410
argdict[argname + '_list'] = None
431
411
elif ap[-1] == '+':
433
raise BzrCommandError("command %r needs one or more %s"
434
% (cmd, argname.upper()))
413
raise errors.BzrCommandError("command %r needs one or more %s"
414
% (cmd, argname.upper()))
436
416
argdict[argname + '_list'] = args[:]
438
418
elif ap[-1] == '$': # all but one
439
419
if len(args) < 2:
440
raise BzrCommandError("command %r needs one or more %s"
441
% (cmd, argname.upper()))
420
raise errors.BzrCommandError("command %r needs one or more %s"
421
% (cmd, argname.upper()))
442
422
argdict[argname + '_list'] = args[:-1]
445
425
# just a plain arg
448
raise BzrCommandError("command %r requires argument %s"
449
% (cmd, argname.upper()))
428
raise errors.BzrCommandError("command %r requires argument %s"
429
% (cmd, argname.upper()))
451
431
argdict[argname] = args.pop(0)
454
raise BzrCommandError("extra argument to command %s: %s"
434
raise errors.BzrCommandError("extra argument to command %s: %s"
499
"""Return an expanded alias, or None if no alias exists"""
501
alias = bzrlib.config.GlobalConfig().get_alias(cmd)
478
def get_alias(cmd, config=None):
479
"""Return an expanded alias, or None if no alias exists.
482
Command to be checked for an alias.
484
Used to specify an alternative config to use,
485
which is especially useful for testing.
486
If it is unspecified, the global config will be used.
490
config = bzrlib.config.GlobalConfig()
491
alias = config.get_alias(cmd)
503
return alias.split(' ')
494
return [a.decode('utf-8') for a in shlex.split(alias.encode('utf-8'))]
593
590
alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
594
591
argv[0] = alias_argv.pop(0)
596
cmd = str(argv.pop(0))
594
# We want only 'ascii' command names, but the user may have typed
595
# in a Unicode name. In that case, they should just get a
596
# 'command not found' error later.
598
598
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
599
599
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
638
640
from bzrlib.ui.text import TextUIFactory
639
## bzrlib.trace.enable_default_logging()
640
bzrlib.trace.log_startup(argv)
641
641
bzrlib.ui.ui_factory = TextUIFactory()
642
ret = run_bzr_catch_errors(argv[1:])
643
mutter("return code %d", ret)
642
argv = [a.decode(bzrlib.user_encoding) for a in argv[1:]]
643
ret = run_bzr_catch_errors(argv)
644
trace.mutter("return code %d", ret)
647
648
def run_bzr_catch_errors(argv):
652
# do this here inside the exception wrappers to catch EPIPE
651
# do this here inside the exception wrappers to catch EPIPE
653
except (KeyboardInterrupt, Exception), e:
655
654
# used to handle AssertionError and KeyboardInterrupt
656
655
# specially here, but hopefully they're handled ok by the logger now
658
if (isinstance(e, IOError)
659
and hasattr(e, 'errno')
660
and e.errno == errno.EPIPE):
661
bzrlib.trace.note('broken pipe')
664
bzrlib.trace.log_exception()
665
if os.environ.get('BZR_PDB'):
666
print '**** entering debugger'
668
pdb.post_mortem(sys.exc_traceback)
656
trace.report_exception(sys.exc_info(), sys.stderr)
657
if os.environ.get('BZR_PDB'):
658
print '**** entering debugger'
660
pdb.post_mortem(sys.exc_traceback)
671
663
if __name__ == '__main__':
672
664
sys.exit(main(sys.argv))