/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Marco Pantaleoni
  • Date: 2010-03-25 14:22:41 UTC
  • mto: This revision was merged to the branch mainline in revision 5174.
  • Revision ID: panta@elasticworld.org-20100325142241-vtl1hshva7uiz3ap
Applied suggestions from merge reviewer (John A Meinel):

- removed duplication of checks for presence of hooks.
- adapted to bazaar style guideline
- simplified test cases callback

Show diffs side-by-side

added added

removed removed

Lines of Context:
182
182
    import bzrlib.builtins
183
183
    for cmd_class in _scan_module_for_commands(bzrlib.builtins).values():
184
184
        builtin_command_registry.register(cmd_class)
185
 
    bzrlib.builtins._register_lazy_builtins()
 
185
    # lazy builtins
 
186
    builtin_command_registry.register_lazy('cmd_bundle_info',
 
187
        [],
 
188
        'bzrlib.bundle.commands')
186
189
 
187
190
 
188
191
def _scan_module_for_commands(module):
222
225
    Use of all_command_names() is encouraged rather than builtin_command_names
223
226
    and/or plugin_command_names.
224
227
    """
225
 
    _register_builtin_commands()
226
228
    return builtin_command_registry.keys()
227
229
 
228
230
 
398
400
            will not mangled.
399
401
 
400
402
    :cvar hooks: An instance of CommandHooks.
401
 
    :ivar __doc__: The help shown by 'bzr help command' for this command.
402
 
        This is set by assigning explicitly to __doc__ so that -OO can
403
 
        be used::
404
 
 
405
 
        class Foo(Command):
406
 
            __doc__ = "My help goes here"
407
403
    """
408
404
    aliases = []
409
405
    takes_args = []
414
410
 
415
411
    def __init__(self):
416
412
        """Construct an instance of this command."""
 
413
        if self.__doc__ == Command.__doc__:
 
414
            warn("No help message set for %r" % self)
417
415
        # List of standard options directly supported
418
416
        self.supported_std_options = []
419
 
        self._setup_run()
 
417
        self._operation = cleanup.OperationWithCleanups(self.run)
420
418
 
421
419
    def add_cleanup(self, cleanup_func, *args, **kwargs):
422
420
        """Register a function to call after self.run returns or raises.
434
432
 
435
433
        This is useful for releasing expensive or contentious resources (such
436
434
        as write locks) before doing further work that does not require those
437
 
        resources (such as writing results to self.outf). Note though, that
438
 
        as it releases all resources, this may release locks that the command
439
 
        wants to hold, so use should be done with care.
 
435
        resources (such as writing results to self.outf).
440
436
        """
441
437
        self._operation.cleanup_now()
442
438
 
487
483
            message explaining how to obtain full help.
488
484
        """
489
485
        doc = self.help()
490
 
        if not doc:
491
 
            doc = "No help for this command."
 
486
        if doc is None:
 
487
            raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
492
488
 
493
489
        # Extract the summary (purpose) and sections out from the text
494
490
        purpose,sections,order = self._get_help_parts(doc)
687
683
 
688
684
        self._setup_outf()
689
685
 
690
 
        return self.run(**all_cmd_args)
691
 
 
692
 
    def _setup_run(self):
693
 
        """Wrap the defined run method on self with a cleanup.
694
 
 
695
 
        This is called by __init__ to make the Command be able to be run
696
 
        by just calling run(), as it could be before cleanups were added.
697
 
 
698
 
        If a different form of cleanups are in use by your Command subclass,
699
 
        you can override this method.
700
 
        """
701
 
        class_run = self.run
702
 
        def run(*args, **kwargs):
703
 
            self._operation = cleanup.OperationWithCleanups(class_run)
704
 
            try:
705
 
                return self._operation.run_simple(*args, **kwargs)
706
 
            finally:
707
 
                del self._operation
708
 
        self.run = run
709
 
 
710
 
    @deprecated_method(deprecated_in((2, 2, 0)))
 
686
        return self.run_direct(**all_cmd_args)
 
687
 
711
688
    def run_direct(self, *args, **kwargs):
712
 
        """Deprecated thunk from bzrlib 2.1."""
713
 
        return self.run(*args, **kwargs)
 
689
        """Call run directly with objects (without parsing an argv list)."""
 
690
        return self._operation.run_simple(*args, **kwargs)
714
691
 
715
692
    def run(self):
716
693
        """Actually run the command.
721
698
        Return 0 or None if the command was successful, or a non-zero
722
699
        shell error code if not.  It's OK for this method to allow
723
700
        an exception to raise up.
724
 
 
725
 
        This method is automatically wrapped by Command.__init__ with a 
726
 
        cleanup operation, stored as self._operation. This can be used
727
 
        via self.add_cleanup to perform automatic cleanups at the end of
728
 
        run().
729
 
 
730
 
        The argument for run are assembled by introspection. So for instance,
731
 
        if your command takes an argument files, you would declare::
732
 
 
733
 
            def run(self, files=None):
734
 
                pass
735
701
        """
736
702
        raise NotImplementedError('no implementation of command %r'
737
703
                                  % self.name())
1055
1021
        elif a == '--coverage':
1056
1022
            opt_coverage_dir = argv[i + 1]
1057
1023
            i += 1
1058
 
        elif a == '--profile-imports':
1059
 
            pass # already handled in startup script Bug #588277
1060
1024
        elif a.startswith('-D'):
1061
1025
            debug.debug_flags.add(a[2:])
1062
1026
        else:
1084
1048
    if not opt_no_aliases:
1085
1049
        alias_argv = get_alias(argv[0])
1086
1050
        if alias_argv:
 
1051
            user_encoding = osutils.get_user_encoding()
 
1052
            alias_argv = [a.decode(user_encoding) for a in alias_argv]
1087
1053
            argv[0] = alias_argv.pop(0)
1088
1054
 
1089
1055
    cmd = argv.pop(0)
 
1056
    # We want only 'ascii' command names, but the user may have typed
 
1057
    # in a Unicode name. In that case, they should just get a
 
1058
    # 'command not found' error later.
 
1059
 
1090
1060
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
1091
1061
    run = cmd_obj.run_argv_aliases
1092
1062
    run_argv = [argv, alias_argv]