/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/cleanup.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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
 
from . import (
 
46
import sys
 
47
from bzrlib import (
48
48
    debug,
49
49
    trace,
50
50
    )
51
51
 
52
 
 
53
52
def _log_cleanup_error(exc):
54
53
    trace.mutter('Cleanup failed:')
55
54
    trace.log_exception_quietly()
56
55
    if 'cleanup' in debug.debug_flags:
57
 
        trace.warning('brz: warning: Cleanup failed: %s', exc)
 
56
        trace.warning('bzr: warning: Cleanup failed: %s', exc)
58
57
 
59
58
 
60
59
def _run_cleanup(func, *args, **kwargs):
67
66
        func(*args, **kwargs)
68
67
    except KeyboardInterrupt:
69
68
        raise
70
 
    except Exception as exc:
 
69
    except Exception, exc:
71
70
        _log_cleanup_error(exc)
72
71
        return False
73
72
    return True
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
 
 
88
 
    def __init__(self):
89
 
        self.cleanups = deque()
90
 
 
91
 
    def add_cleanup(self, cleanup_func, *args, **kwargs):
92
 
        """Add a cleanup to run.
93
 
 
94
 
        Cleanups may be added at any time.
95
 
        Cleanups will be executed in LIFO order.
96
 
        """
97
 
        self.cleanups.appendleft((cleanup_func, args, kwargs))
98
 
 
99
 
    def cleanup_now(self):
100
 
        _run_cleanups(self.cleanups)
101
 
        self.cleanups.clear()
102
 
 
103
 
 
104
 
class OperationWithCleanups(ObjectWithCleanups):
 
81
class OperationWithCleanups(object):
105
82
    """A way to run some code with a dynamic cleanup list.
106
83
 
107
84
    This provides a way to add cleanups while the function-with-cleanups is
125
102
    """
126
103
 
127
104
    def __init__(self, func):
128
 
        super(OperationWithCleanups, self).__init__()
129
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))
130
115
 
131
116
    def run(self, *args, **kwargs):
132
117
        return _do_with_cleanups(
136
121
        return _do_with_cleanups(
137
122
            self.cleanups, self.func, *args, **kwargs)
138
123
 
 
124
    def cleanup_now(self):
 
125
        _run_cleanups(self.cleanups)
 
126
        self.cleanups.clear()
 
127
 
139
128
 
140
129
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
141
130
    """Run `func`, then call all the cleanup_funcs.
162
151
    Unike `_run_cleanup`, `_do_with_cleanups` can propagate an exception from a
163
152
    cleanup, but only if there is no exception from func.
164
153
    """
 
154
    # As correct as Python 2.4 allows.
165
155
    try:
166
156
        result = func(*args, **kwargs)
167
 
    except BaseException:
 
157
    except:
168
158
        # We have an exception from func already, so suppress cleanup errors.
169
159
        _run_cleanups(cleanup_funcs)
170
160
        raise
171
 
    # No exception from func, so allow first cleanup error to propgate.
172
 
    pending_cleanups = iter(cleanup_funcs)
173
 
    try:
174
 
        for cleanup, c_args, c_kwargs in pending_cleanups:
175
 
            cleanup(*c_args, **c_kwargs)
176
 
    except BaseException:
177
 
        # Still run the remaining cleanups but suppress any further errors.
178
 
        _run_cleanups(pending_cleanups)
179
 
        raise
180
 
    # No error, so we can return the result
181
 
    return result
 
161
    else:
 
162
        # No exception from func, so allow the first exception from
 
163
        # cleanup_funcs to propagate if one occurs (but only after running all
 
164
        # of them).
 
165
        exc_info = None
 
166
        for cleanup, c_args, c_kwargs in cleanup_funcs:
 
167
            # XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
 
168
            # won't run all cleanups... perhaps we should temporarily install a
 
169
            # SIGINT handler?
 
170
            if exc_info is None:
 
171
                try:
 
172
                    cleanup(*c_args, **c_kwargs)
 
173
                except:
 
174
                    # This is the first cleanup to fail, so remember its
 
175
                    # details.
 
176
                    exc_info = sys.exc_info()
 
177
            else:
 
178
                # We already have an exception to propagate, so log any errors
 
179
                # but don't propagate them.
 
180
                _run_cleanup(cleanup, *c_args, **kwargs)
 
181
        if exc_info is not None:
 
182
            raise exc_info[0], exc_info[1], exc_info[2]
 
183
        # No error, so we can return the result
 
184
        return result
 
185
 
 
186