~bzr-pqm/bzr/bzr.dev

4797.32.2 by John Arbash Meinel
merge 2.1, resolving NEWS conflict.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
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
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
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.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
30
4634.85.6 by Andrew Bennetts
More tests.
31
If you want to be certain that the first, and only the first, error is raised,
32
then use::
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
33
4797.16.1 by Andrew Bennetts
Replace many try/finally blocks in merge.py with more robust OperationWithCleanups.
34
    operation = OperationWithCleanups(do_something)
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
35
    operation.add_cleanup(cleanup_something)
4797.16.1 by Andrew Bennetts
Replace many try/finally blocks in merge.py with more robust OperationWithCleanups.
36
    operation.run_simple()
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
37
4634.85.6 by Andrew Bennetts
More tests.
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,
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
40
while also ensuring all cleanups are run.  See OperationWithCleanups for more
41
details.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
42
"""
43
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
44
from __future__ import absolute_import
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
45
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
46
from collections import deque
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
47
import sys
4634.85.3 by Andrew Bennetts
Lots more tests.
48
from bzrlib import (
49
    debug,
50
    trace,
51
    )
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
52
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
53
def _log_cleanup_error(exc):
4634.85.3 by Andrew Bennetts
Lots more tests.
54
    trace.mutter('Cleanup failed:')
55
    trace.log_exception_quietly()
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
56
    if 'cleanup' in debug.debug_flags:
57
        trace.warning('bzr: warning: Cleanup failed: %s', exc)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
58
59
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
60
def _run_cleanup(func, *args, **kwargs):
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
61
    """Run func(*args, **kwargs), logging but not propagating any error it
62
    raises.
63
64
    :returns: True if func raised no errors, else False.
65
    """
66
    try:
67
        func(*args, **kwargs)
68
    except KeyboardInterrupt:
69
        raise
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
70
    except Exception, exc:
71
        _log_cleanup_error(exc)
72
        return False
73
    return True
74
75
4744.3.3 by Andrew Bennetts
Remove _run_cleanup_reporting_errors.
76
def _run_cleanups(funcs):
77
    """Run a series of cleanup functions."""
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
78
    for func, args, kwargs in funcs:
79
        _run_cleanup(func, *args, **kwargs)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
80
81
5158.8.1 by Martin Pool
Split out ObjectWithCleanups
82
class ObjectWithCleanups(object):
83
    """A mixin for objects that hold a cleanup list.
84
5158.8.2 by Martin Pool
Add simple test case for ObjectWithCleanups
85
    Subclass or client code can call add_cleanup and then later `cleanup_now`.
5158.8.1 by Martin Pool
Split out ObjectWithCleanups
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):
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
104
    """A way to run some code with a dynamic cleanup list.
4744.3.2 by Andrew Bennetts
Add OperationWithCleanups helper, use it to make commit.py simpler and more robust.
105
106
    This provides a way to add cleanups while the function-with-cleanups is
107
    running.
108
109
    Typical use::
110
111
        operation = OperationWithCleanups(some_func)
112
        operation.run(args...)
113
114
    where `some_func` is::
115
4900.1.1 by Andrew Bennetts
Add add_cleanup to Command.
116
        def some_func(operation, args, ...):
4744.3.2 by Andrew Bennetts
Add OperationWithCleanups helper, use it to make commit.py simpler and more robust.
117
            do_something()
118
            operation.add_cleanup(something)
119
            # etc
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
120
121
    Note that the first argument passed to `some_func` will be the
4900.1.1 by Andrew Bennetts
Add add_cleanup to Command.
122
    OperationWithCleanups object.  To invoke `some_func` without that, use
123
    `run_simple` instead of `run`.
4744.3.2 by Andrew Bennetts
Add OperationWithCleanups helper, use it to make commit.py simpler and more robust.
124
    """
125
126
    def __init__(self, func):
5158.8.1 by Martin Pool
Split out ObjectWithCleanups
127
        super(OperationWithCleanups, self).__init__()
4744.3.2 by Andrew Bennetts
Add OperationWithCleanups helper, use it to make commit.py simpler and more robust.
128
        self.func = func
129
130
    def run(self, *args, **kwargs):
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
131
        return _do_with_cleanups(
132
            self.cleanups, self.func, self, *args, **kwargs)
133
4900.1.1 by Andrew Bennetts
Add add_cleanup to Command.
134
    def run_simple(self, *args, **kwargs):
135
        return _do_with_cleanups(
136
            self.cleanups, self.func, *args, **kwargs)
137
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
138
139
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
140
    """Run `func`, then call all the cleanup_funcs.
141
142
    All the cleanup_funcs are guaranteed to be run.  The first exception raised
143
    by func or any of the cleanup_funcs is the one that will be propagted by
144
    this function (subsequent errors are caught and logged).
145
146
    Conceptually similar to::
147
148
        try:
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
149
            return func(*args, **kwargs)
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
150
        finally:
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
151
            for cleanup, cargs, ckwargs in cleanup_funcs:
152
                cleanup(*cargs, **ckwargs)
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
153
154
    It avoids several problems with using try/finally directly:
155
     * an exception from func will not be obscured by a subsequent exception
156
       from a cleanup.
157
     * an exception from a cleanup will not prevent other cleanups from
158
       running (but the first exception encountered is still the one
159
       propagated).
160
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
161
    Unike `_run_cleanup`, `_do_with_cleanups` can propagate an exception from a
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
162
    cleanup, but only if there is no exception from func.
163
    """
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
164
    # As correct as Python 2.4 allows.
165
    try:
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
166
        result = func(*args, **kwargs)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
167
    except:
168
        # We have an exception from func already, so suppress cleanup errors.
4744.3.1 by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.
169
        _run_cleanups(cleanup_funcs)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
170
        raise
171
    else:
172
        # No exception from func, so allow the first exception from
173
        # cleanup_funcs to propagate if one occurs (but only after running all
174
        # of them).
175
        exc_info = None
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
176
        for cleanup, c_args, c_kwargs in cleanup_funcs:
4634.85.3 by Andrew Bennetts
Lots more tests.
177
            # XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
178
            # won't run all cleanups... perhaps we should temporarily install a
179
            # SIGINT handler?
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
180
            if exc_info is None:
181
                try:
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
182
                    cleanup(*c_args, **c_kwargs)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
183
                except:
184
                    # This is the first cleanup to fail, so remember its
185
                    # details.
186
                    exc_info = sys.exc_info()
187
            else:
188
                # We already have an exception to propagate, so log any errors
189
                # but don't propagate them.
4744.3.4 by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...).
190
                _run_cleanup(cleanup, *c_args, **kwargs)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
191
        if exc_info is not None:
5340.15.2 by John Arbash Meinel
supercede 2.4-613247-cleanup-tests
192
            try:
193
                raise exc_info[0], exc_info[1], exc_info[2]
194
            finally:
195
                del exc_info
4634.85.2 by Andrew Bennetts
Test and code tweak.
196
        # No error, so we can return the result
197
        return result
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
198
199