~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/cleanup.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-03-25 00:02:51 UTC
  • mfrom: (5106.1.1 version-bump)
  • Revision ID: pqm@pqm.ubuntu.com-20100325000251-bwsv5c5d3l9x3lnn
(Jelmer) Bump API version for 2.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
details.
42
42
"""
43
43
 
44
 
from __future__ import absolute_import
45
44
 
46
45
from collections import deque
47
46
import sys
79
78
        _run_cleanup(func, *args, **kwargs)
80
79
 
81
80
 
82
 
class ObjectWithCleanups(object):
83
 
    """A mixin for objects that hold a cleanup list.
84
 
 
85
 
    Subclass or client code can call add_cleanup and then later `cleanup_now`.
86
 
    """
87
 
    def __init__(self):
88
 
        self.cleanups = deque()
89
 
 
90
 
    def add_cleanup(self, cleanup_func, *args, **kwargs):
91
 
        """Add a cleanup to run.
92
 
 
93
 
        Cleanups may be added at any time.  
94
 
        Cleanups will be executed in LIFO order.
95
 
        """
96
 
        self.cleanups.appendleft((cleanup_func, args, kwargs))
97
 
 
98
 
    def cleanup_now(self):
99
 
        _run_cleanups(self.cleanups)
100
 
        self.cleanups.clear()
101
 
 
102
 
 
103
 
class OperationWithCleanups(ObjectWithCleanups):
 
81
class OperationWithCleanups(object):
104
82
    """A way to run some code with a dynamic cleanup list.
105
83
 
106
84
    This provides a way to add cleanups while the function-with-cleanups is
124
102
    """
125
103
 
126
104
    def __init__(self, func):
127
 
        super(OperationWithCleanups, self).__init__()
128
105
        self.func = func
 
106
        self.cleanups = deque()
 
107
 
 
108
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
109
        """Add a cleanup to run.
 
110
 
 
111
        Cleanups may be added at any time before or during the execution of
 
112
        self.func.  Cleanups will be executed in LIFO order.
 
113
        """
 
114
        self.cleanups.appendleft((cleanup_func, args, kwargs))
129
115
 
130
116
    def run(self, *args, **kwargs):
131
117
        return _do_with_cleanups(
135
121
        return _do_with_cleanups(
136
122
            self.cleanups, self.func, *args, **kwargs)
137
123
 
 
124
    def cleanup_now(self):
 
125
        _run_cleanups(self.cleanups)
 
126
        self.cleanups.clear()
 
127
 
138
128
 
139
129
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
140
130
    """Run `func`, then call all the cleanup_funcs.
189
179
                # but don't propagate them.
190
180
                _run_cleanup(cleanup, *c_args, **kwargs)
191
181
        if exc_info is not None:
192
 
            try:
193
 
                raise exc_info[0], exc_info[1], exc_info[2]
194
 
            finally:
195
 
                del exc_info
 
182
            raise exc_info[0], exc_info[1], exc_info[2]
196
183
        # No error, so we can return the result
197
184
        return result
198
185