~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/cleanup.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-22 13:22:18 UTC
  • mfrom: (6155.2.2 migrate-config-options)
  • Revision ID: pqm@pqm.ubuntu.com-20110922132218-nitl31j5slbcmnm2
(vila) Migrate dpush_strict,
 push_strict and send_strict options to the stack based config design,
 introducing get_config_stack for branches (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Helpers for managing cleanup functions and the errors they might raise.
 
18
 
 
19
The usual way to run cleanup code in Python is::
 
20
 
 
21
    try:
 
22
        do_something()
 
23
    finally:
 
24
        cleanup_something()
 
25
 
 
26
However if both `do_something` and `cleanup_something` raise an exception
 
27
Python will forget the original exception and propagate the one from
 
28
cleanup_something.  Unfortunately, this is almost always much less useful than
 
29
the original exception.
 
30
 
 
31
If you want to be certain that the first, and only the first, error is raised,
 
32
then use::
 
33
 
 
34
    operation = OperationWithCleanups(do_something)
 
35
    operation.add_cleanup(cleanup_something)
 
36
    operation.run_simple()
 
37
 
 
38
This is more inconvenient (because you need to make every try block a
 
39
function), but will ensure that the first error encountered is the one raised,
 
40
while also ensuring all cleanups are run.  See OperationWithCleanups for more
 
41
details.
 
42
"""
 
43
 
 
44
 
 
45
from collections import deque
 
46
import sys
 
47
from bzrlib import (
 
48
    debug,
 
49
    trace,
 
50
    )
 
51
 
 
52
def _log_cleanup_error(exc):
 
53
    trace.mutter('Cleanup failed:')
 
54
    trace.log_exception_quietly()
 
55
    if 'cleanup' in debug.debug_flags:
 
56
        trace.warning('bzr: warning: Cleanup failed: %s', exc)
 
57
 
 
58
 
 
59
def _run_cleanup(func, *args, **kwargs):
 
60
    """Run func(*args, **kwargs), logging but not propagating any error it
 
61
    raises.
 
62
 
 
63
    :returns: True if func raised no errors, else False.
 
64
    """
 
65
    try:
 
66
        func(*args, **kwargs)
 
67
    except KeyboardInterrupt:
 
68
        raise
 
69
    except Exception, exc:
 
70
        _log_cleanup_error(exc)
 
71
        return False
 
72
    return True
 
73
 
 
74
 
 
75
def _run_cleanups(funcs):
 
76
    """Run a series of cleanup functions."""
 
77
    for func, args, kwargs in funcs:
 
78
        _run_cleanup(func, *args, **kwargs)
 
79
 
 
80
 
 
81
class ObjectWithCleanups(object):
 
82
    """A mixin for objects that hold a cleanup list.
 
83
 
 
84
    Subclass or client code can call add_cleanup and then later `cleanup_now`.
 
85
    """
 
86
    def __init__(self):
 
87
        self.cleanups = deque()
 
88
 
 
89
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
90
        """Add a cleanup to run.
 
91
 
 
92
        Cleanups may be added at any time.  
 
93
        Cleanups will be executed in LIFO order.
 
94
        """
 
95
        self.cleanups.appendleft((cleanup_func, args, kwargs))
 
96
 
 
97
    def cleanup_now(self):
 
98
        _run_cleanups(self.cleanups)
 
99
        self.cleanups.clear()
 
100
 
 
101
 
 
102
class OperationWithCleanups(ObjectWithCleanups):
 
103
    """A way to run some code with a dynamic cleanup list.
 
104
 
 
105
    This provides a way to add cleanups while the function-with-cleanups is
 
106
    running.
 
107
 
 
108
    Typical use::
 
109
 
 
110
        operation = OperationWithCleanups(some_func)
 
111
        operation.run(args...)
 
112
 
 
113
    where `some_func` is::
 
114
 
 
115
        def some_func(operation, args, ...):
 
116
            do_something()
 
117
            operation.add_cleanup(something)
 
118
            # etc
 
119
 
 
120
    Note that the first argument passed to `some_func` will be the
 
121
    OperationWithCleanups object.  To invoke `some_func` without that, use
 
122
    `run_simple` instead of `run`.
 
123
    """
 
124
 
 
125
    def __init__(self, func):
 
126
        super(OperationWithCleanups, self).__init__()
 
127
        self.func = func
 
128
 
 
129
    def run(self, *args, **kwargs):
 
130
        return _do_with_cleanups(
 
131
            self.cleanups, self.func, self, *args, **kwargs)
 
132
 
 
133
    def run_simple(self, *args, **kwargs):
 
134
        return _do_with_cleanups(
 
135
            self.cleanups, self.func, *args, **kwargs)
 
136
 
 
137
 
 
138
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
 
139
    """Run `func`, then call all the cleanup_funcs.
 
140
 
 
141
    All the cleanup_funcs are guaranteed to be run.  The first exception raised
 
142
    by func or any of the cleanup_funcs is the one that will be propagted by
 
143
    this function (subsequent errors are caught and logged).
 
144
 
 
145
    Conceptually similar to::
 
146
 
 
147
        try:
 
148
            return func(*args, **kwargs)
 
149
        finally:
 
150
            for cleanup, cargs, ckwargs in cleanup_funcs:
 
151
                cleanup(*cargs, **ckwargs)
 
152
 
 
153
    It avoids several problems with using try/finally directly:
 
154
     * an exception from func will not be obscured by a subsequent exception
 
155
       from a cleanup.
 
156
     * an exception from a cleanup will not prevent other cleanups from
 
157
       running (but the first exception encountered is still the one
 
158
       propagated).
 
159
 
 
160
    Unike `_run_cleanup`, `_do_with_cleanups` can propagate an exception from a
 
161
    cleanup, but only if there is no exception from func.
 
162
    """
 
163
    # As correct as Python 2.4 allows.
 
164
    try:
 
165
        result = func(*args, **kwargs)
 
166
    except:
 
167
        # We have an exception from func already, so suppress cleanup errors.
 
168
        _run_cleanups(cleanup_funcs)
 
169
        raise
 
170
    else:
 
171
        # No exception from func, so allow the first exception from
 
172
        # cleanup_funcs to propagate if one occurs (but only after running all
 
173
        # of them).
 
174
        exc_info = None
 
175
        for cleanup, c_args, c_kwargs in cleanup_funcs:
 
176
            # XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
 
177
            # won't run all cleanups... perhaps we should temporarily install a
 
178
            # SIGINT handler?
 
179
            if exc_info is None:
 
180
                try:
 
181
                    cleanup(*c_args, **c_kwargs)
 
182
                except:
 
183
                    # This is the first cleanup to fail, so remember its
 
184
                    # details.
 
185
                    exc_info = sys.exc_info()
 
186
            else:
 
187
                # We already have an exception to propagate, so log any errors
 
188
                # but don't propagate them.
 
189
                _run_cleanup(cleanup, *c_args, **kwargs)
 
190
        if exc_info is not None:
 
191
            try:
 
192
                raise exc_info[0], exc_info[1], exc_info[2]
 
193
            finally:
 
194
                del exc_info
 
195
        # No error, so we can return the result
 
196
        return result
 
197
 
 
198