~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2010-03-26 10:25:47 UTC
  • mfrom: (5110.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5118.
  • Revision ID: mbp@canonical.com-20100326102547-74ta65m7w7ecjebq
merge 2.1.1, including fetch format warning, back to trunk

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):
411
414
            warn("No help message set for %r" % self)
412
415
        # List of standard options directly supported
413
416
        self.supported_std_options = []
414
 
        self._setup_run()
 
417
        self._operation = cleanup.OperationWithCleanups(self.run)
415
418
 
416
419
    def add_cleanup(self, cleanup_func, *args, **kwargs):
417
420
        """Register a function to call after self.run returns or raises.
429
432
 
430
433
        This is useful for releasing expensive or contentious resources (such
431
434
        as write locks) before doing further work that does not require those
432
 
        resources (such as writing results to self.outf). Note though, that
433
 
        as it releases all resources, this may release locks that the command
434
 
        wants to hold, so use should be done with care.
 
435
        resources (such as writing results to self.outf).
435
436
        """
436
437
        self._operation.cleanup_now()
437
438
 
682
683
 
683
684
        self._setup_outf()
684
685
 
685
 
        return self.run(**all_cmd_args)
686
 
 
687
 
    def _setup_run(self):
688
 
        """Wrap the defined run method on self with a cleanup.
689
 
 
690
 
        This is called by __init__ to make the Command be able to be run
691
 
        by just calling run(), as it could be before cleanups were added.
692
 
 
693
 
        If a different form of cleanups are in use by your Command subclass,
694
 
        you can override this method.
695
 
        """
696
 
        class_run = self.run
697
 
        def run(*args, **kwargs):
698
 
            self._operation = cleanup.OperationWithCleanups(class_run)
699
 
            try:
700
 
                return self._operation.run_simple(*args, **kwargs)
701
 
            finally:
702
 
                del self._operation
703
 
        self.run = run
704
 
 
705
 
    @deprecated_method(deprecated_in((2, 2, 0)))
 
686
        return self.run_direct(**all_cmd_args)
 
687
 
706
688
    def run_direct(self, *args, **kwargs):
707
 
        """Deprecated thunk from bzrlib 2.1."""
708
 
        return self.run(*args, **kwargs)
 
689
        """Call run directly with objects (without parsing an argv list)."""
 
690
        return self._operation.run_simple(*args, **kwargs)
709
691
 
710
692
    def run(self):
711
693
        """Actually run the command.
716
698
        Return 0 or None if the command was successful, or a non-zero
717
699
        shell error code if not.  It's OK for this method to allow
718
700
        an exception to raise up.
719
 
 
720
 
        This method is automatically wrapped by Command.__init__ with a 
721
 
        cleanup operation, stored as self._operation. This can be used
722
 
        via self.add_cleanup to perform automatic cleanups at the end of
723
 
        run().
724
 
 
725
 
        The argument for run are assembled by introspection. So for instance,
726
 
        if your command takes an argument files, you would declare::
727
 
 
728
 
            def run(self, files=None):
729
 
                pass
730
701
        """
731
702
        raise NotImplementedError('no implementation of command %r'
732
703
                                  % self.name())