~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2010-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
411
411
            warn("No help message set for %r" % self)
412
412
        # List of standard options directly supported
413
413
        self.supported_std_options = []
414
 
        self._setup_run()
 
414
        self._operation = cleanup.OperationWithCleanups(self.run)
415
415
 
416
416
    def add_cleanup(self, cleanup_func, *args, **kwargs):
417
417
        """Register a function to call after self.run returns or raises.
429
429
 
430
430
        This is useful for releasing expensive or contentious resources (such
431
431
        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.
 
432
        resources (such as writing results to self.outf).
435
433
        """
436
434
        self._operation.cleanup_now()
437
435
 
682
680
 
683
681
        self._setup_outf()
684
682
 
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)))
 
683
        return self.run_direct(**all_cmd_args)
 
684
 
706
685
    def run_direct(self, *args, **kwargs):
707
 
        """Deprecated thunk from bzrlib 2.1."""
708
 
        return self.run(*args, **kwargs)
 
686
        """Call run directly with objects (without parsing an argv list)."""
 
687
        return self._operation.run_simple(*args, **kwargs)
709
688
 
710
689
    def run(self):
711
690
        """Actually run the command.
716
695
        Return 0 or None if the command was successful, or a non-zero
717
696
        shell error code if not.  It's OK for this method to allow
718
697
        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
698
        """
731
699
        raise NotImplementedError('no implementation of command %r'
732
700
                                  % self.name())