~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-01-11 13:15:01 UTC
  • mfrom: (4900.1.6 command-cleanup)
  • Revision ID: pqm@pqm.ubuntu.com-20100111131501-btdm0vi95l7xjlp1
(andrew) Implement Command.add_cleanup,
        and replace try/finally blocks in bzrlib.builtins with it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
import bzrlib
42
42
from bzrlib import (
 
43
    cleanup,
43
44
    debug,
44
45
    errors,
45
46
    option,
384
385
            warn("No help message set for %r" % self)
385
386
        # List of standard options directly supported
386
387
        self.supported_std_options = []
387
 
 
 
388
        self._operation = cleanup.OperationWithCleanups(self.run)
 
389
    
 
390
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
391
        """Register a function to call after self.run returns or raises.
 
392
 
 
393
        Functions will be called in LIFO order.
 
394
        """
 
395
        self._operation.add_cleanup(cleanup_func, *args, **kwargs)
 
396
 
 
397
    def cleanup_now(self):
 
398
        """Execute and empty pending cleanup functions immediately.
 
399
 
 
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).
 
403
 
 
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).
 
407
        """
 
408
        self._operation.cleanup_now()
 
409
        
388
410
    @deprecated_method(deprecated_in((2, 1, 0)))
389
411
    def _maybe_expand_globs(self, file_list):
390
412
        """Glob expand file_list if the platform does not do that itself.
636
658
 
637
659
        self._setup_outf()
638
660
 
639
 
        return self.run(**all_cmd_args)
 
661
        return self.run_direct(**all_cmd_args)
 
662
 
 
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)
640
666
 
641
667
    def run(self):
642
668
        """Actually run the command.