1
# Copyright (C) 2006, 2008 Canonical Ltd
1
# Copyright (C) 2005-2010 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
382
385
warn("No help message set for %r" % self)
383
386
# List of standard options directly supported
384
387
self.supported_std_options = []
388
self._operation = cleanup.OperationWithCleanups(self.run)
390
def add_cleanup(self, cleanup_func, *args, **kwargs):
391
"""Register a function to call after self.run returns or raises.
393
Functions will be called in LIFO order.
395
self._operation.add_cleanup(cleanup_func, *args, **kwargs)
397
def cleanup_now(self):
398
"""Execute and empty pending cleanup functions immediately.
400
After cleanup_now all registered cleanups are forgotten. add_cleanup
401
may be called again after cleanup_now; these cleanups will be called
402
after self.run returns or raises (or when cleanup_now is next called).
404
This is useful for releasing expensive or contentious resources (such
405
as write locks) before doing further work that does not require those
406
resources (such as writing results to self.outf).
408
self._operation.cleanup_now()
410
@deprecated_method(deprecated_in((2, 1, 0)))
386
411
def _maybe_expand_globs(self, file_list):
387
412
"""Glob expand file_list if the platform does not do that itself.
414
Not used anymore, now that the bzr command-line parser globs on
389
417
:return: A possibly empty list of unicode paths.
391
419
Introduced in bzrlib 0.18.
395
if sys.platform == 'win32':
396
file_list = win32utils.glob_expand(file_list)
397
return list(file_list)
399
423
def _usage(self):
400
424
"""Return single-line grammar for this command.
509
533
# so don't create a real link
510
534
see_also_links.append(item)
512
# Use a reST link for this entry
513
see_also_links.append("`%s`_" % (item,))
536
# Use a Sphinx link for this entry
537
link_text = ":doc:`%s <%s-help>`" % (item, item)
538
see_also_links.append(link_text)
514
539
see_also = see_also_links
515
540
result += ':See also: '
516
541
result += ', '.join(see_also) + '\n'
595
620
def _setup_outf(self):
596
621
"""Return a file linked to stdout, which has proper encoding."""
597
# Originally I was using self.stdout, but that looks
598
# *way* too much like sys.stdout
599
if self.encoding_type == 'exact':
600
# force sys.stdout to be binary stream on win32
601
if sys.platform == 'win32':
602
fileno = getattr(sys.stdout, 'fileno', None)
605
msvcrt.setmode(fileno(), os.O_BINARY)
606
self.outf = sys.stdout
609
output_encoding = osutils.get_terminal_encoding()
611
self.outf = codecs.getwriter(output_encoding)(sys.stdout,
612
errors=self.encoding_type)
613
# For whatever reason codecs.getwriter() does not advertise its encoding
614
# it just returns the encoding of the wrapped file, which is completely
615
# bogus. So set the attribute, so we can find the correct encoding later.
616
self.outf.encoding = output_encoding
622
self.outf = ui.ui_factory.make_output_stream(
623
encoding_type=self.encoding_type)
618
625
def run_argv_aliases(self, argv, alias_argv=None):
619
626
"""Parse the command line and run with extra aliases in alias_argv."""
652
659
self._setup_outf()
654
return self.run(**all_cmd_args)
661
return self.run_direct(**all_cmd_args)
663
def run_direct(self, *args, **kwargs):
664
"""Call run directly with objects (without parsing an argv list)."""
665
return self._operation.run_simple(*args, **kwargs)
657
668
"""Actually run the command.
941
952
Generate line coverage report in the specified directory.
955
Specify the number of processes that can be run concurrently (selftest).
957
trace.mutter("bazaar version: " + bzrlib.__version__)
943
958
argv = list(argv)
944
959
trace.mutter("bzr arguments: %r", argv)
1113
1131
raise errors.BzrError("argv should be list of unicode strings.")
1114
1132
argv = new_argv
1115
1133
ret = run_bzr_catch_errors(argv)
1134
bzrlib.ui.ui_factory.log_transport_activity(
1135
display=('bytes' in debug.debug_flags))
1116
1136
trace.mutter("return code %d", ret)
1117
1137
osutils.report_extension_load_failures()