~bzr-pqm/bzr/bzr.dev

4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
1
# Copyright (C) 2009 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
Generally, code that wants to perform some cleanup at the end of an action will
20
look like this::
21
22
    from bzrlib.cleanups import run_cleanup
23
    try:
24
        do_something()
25
    finally:
26
        run_cleanup(cleanup_something)
27
28
Any errors from `cleanup_something` will be logged, but not raised.
29
Importantly, any errors from do_something will be propagated.
30
31
There is also convenience function for running multiple, independent cleanups
32
in sequence: run_cleanups.  e.g.::
33
34
    try:
35
        do_something()
36
    finally:
37
        run_cleanups([cleanup_func_a, cleanup_func_b], ...)
38
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
39
Developers can use the `-Dcleanup` debug flag to cause cleanup errors to be
40
reported in the UI as well as logged.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
41
4634.85.6 by Andrew Bennetts
More tests.
42
Note the tradeoff that run_cleanup/run_cleanups makes: errors from
43
`do_something` will not be obscured by errors from `cleanup_something`, but
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
44
errors from `cleanup_something` will never reach the user, even if there is no
45
error from `do_something`.  So run_cleanup is good to use when a failure of
46
internal housekeeping (e.g. failure to finish a progress bar) is unimportant to
47
a user.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
48
4634.85.6 by Andrew Bennetts
More tests.
49
If you want to be certain that the first, and only the first, error is raised,
50
then use::
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
51
52
    do_with_cleanups(do_something, cleanups)
53
4634.85.6 by Andrew Bennetts
More tests.
54
This is more inconvenient (because you need to make every try block a
55
function), but will ensure that the first error encountered is the one raised,
56
while also ensuring all cleanups are run.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
57
"""
58
59
60
import sys
4634.85.3 by Andrew Bennetts
Lots more tests.
61
from bzrlib import (
62
    debug,
63
    trace,
64
    )
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
65
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
66
def _log_cleanup_error(exc):
4634.85.3 by Andrew Bennetts
Lots more tests.
67
    trace.mutter('Cleanup failed:')
68
    trace.log_exception_quietly()
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
69
    if 'cleanup' in debug.debug_flags:
70
        trace.warning('bzr: warning: Cleanup failed: %s', exc)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
71
72
73
def run_cleanup(func, *args, **kwargs):
74
    """Run func(*args, **kwargs), logging but not propagating any error it
75
    raises.
76
77
    :returns: True if func raised no errors, else False.
78
    """
79
    try:
80
        func(*args, **kwargs)
81
    except KeyboardInterrupt:
82
        raise
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
83
    except Exception, exc:
84
        _log_cleanup_error(exc)
85
        return False
86
    return True
87
88
4634.85.6 by Andrew Bennetts
More tests.
89
def run_cleanup_reporting_errors(func, *args, **kwargs):
90
    try:
91
        func(*args, **kwargs)
92
    except KeyboardInterrupt:
93
        raise
94
    except Exception, exc:
95
        trace.mutter('Cleanup failed:')
96
        trace.log_exception_quietly()
97
        trace.warning('Cleanup failed: %s', exc)
98
        return False
99
    return True
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
100
101
102
def run_cleanups(funcs, on_error='log'):
4634.85.7 by Andrew Bennetts
Comment and docstring gardening.
103
    """Run a series of cleanup functions.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
104
105
    :param errors: One of 'log', 'warn first', 'warn all'
106
    """
107
    seen_error = False
108
    for func in funcs:
109
        if on_error == 'log' or (on_error == 'warn first' and seen_error):
110
            seen_error |= run_cleanup(func)
111
        else:
112
            seen_error |= run_cleanup_reporting_errors(func)
113
114
115
def do_with_cleanups(func, cleanup_funcs):
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
116
    """Run `func`, then call all the cleanup_funcs.
117
118
    All the cleanup_funcs are guaranteed to be run.  The first exception raised
119
    by func or any of the cleanup_funcs is the one that will be propagted by
120
    this function (subsequent errors are caught and logged).
121
122
    Conceptually similar to::
123
124
        try:
125
            return func()
126
        finally:
127
            for cleanup in cleanup_funcs:
128
                cleanup()
129
130
    It avoids several problems with using try/finally directly:
131
     * an exception from func will not be obscured by a subsequent exception
132
       from a cleanup.
133
     * an exception from a cleanup will not prevent other cleanups from
134
       running (but the first exception encountered is still the one
135
       propagated).
136
137
    Unike `run_cleanup`, `do_with_cleanups` can propagate an exception from a
138
    cleanup, but only if there is no exception from func.
139
    """
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
140
    # As correct as Python 2.4 allows.
141
    try:
4634.85.2 by Andrew Bennetts
Test and code tweak.
142
        result = func()
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
143
    except:
144
        # We have an exception from func already, so suppress cleanup errors.
145
        run_cleanups(cleanup_funcs)
146
        raise
147
    else:
148
        # No exception from func, so allow the first exception from
149
        # cleanup_funcs to propagate if one occurs (but only after running all
150
        # of them).
151
        exc_info = None
152
        for cleanup in cleanup_funcs:
4634.85.3 by Andrew Bennetts
Lots more tests.
153
            # XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
154
            # won't run all cleanups... perhaps we should temporarily install a
155
            # SIGINT handler?
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
156
            if exc_info is None:
157
                try:
158
                    cleanup()
159
                except:
160
                    # This is the first cleanup to fail, so remember its
161
                    # details.
162
                    exc_info = sys.exc_info()
163
            else:
164
                # We already have an exception to propagate, so log any errors
165
                # but don't propagate them.
166
                run_cleanup(cleanup)
167
        if exc_info is not None:
168
            raise exc_info[0], exc_info[1], exc_info[2]
4634.85.2 by Andrew Bennetts
Test and code tweak.
169
        # No error, so we can return the result
170
        return result
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
171
172