1
# Copyright (C) 2009 Canonical Ltd
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.
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.
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
17
"""Helpers for managing cleanup functions and the errors they might raise.
19
Generally, code that wants to perform some cleanup at the end of an action will
22
from bzrlib.cleanups import run_cleanup
26
run_cleanup(cleanup_something)
28
Any errors from `cleanup_something` will be logged, but not raised.
29
Importantly, any errors from do_something will be propagated.
31
There is also convenience function for running multiple, independent cleanups
32
in sequence: run_cleanups. e.g.::
37
run_cleanups([cleanup_func_a, cleanup_func_b], ...)
39
Developers can use the `-Dcleanup` debug flag to cause cleanup errors to be
40
reported in the UI as well as logged.
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
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
49
If you want to be certain that the first, and only the first, error is raised,
52
do_with_cleanups(do_something, cleanups)
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.
66
def _log_cleanup_error(exc):
67
trace.mutter('Cleanup failed:')
68
trace.log_exception_quietly()
69
if 'cleanup' in debug.debug_flags:
70
trace.warning('bzr: warning: Cleanup failed: %s', exc)
73
def run_cleanup(func, *args, **kwargs):
74
"""Run func(*args, **kwargs), logging but not propagating any error it
77
:returns: True if func raised no errors, else False.
81
except KeyboardInterrupt:
83
except Exception, exc:
84
_log_cleanup_error(exc)
89
def run_cleanup_reporting_errors(func, *args, **kwargs):
92
except KeyboardInterrupt:
94
except Exception, exc:
95
trace.mutter('Cleanup failed:')
96
trace.log_exception_quietly()
97
trace.warning('Cleanup failed: %s', exc)
102
def run_cleanups(funcs, on_error='log'):
103
"""Run a series of cleanup functions.
105
:param errors: One of 'log', 'warn first', 'warn all'
109
if on_error == 'log' or (on_error == 'warn first' and seen_error):
110
seen_error |= run_cleanup(func)
112
seen_error |= run_cleanup_reporting_errors(func)
115
def do_with_cleanups(func, cleanup_funcs):
116
"""Run `func`, then call all the cleanup_funcs.
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).
122
Conceptually similar to::
127
for cleanup in cleanup_funcs:
130
It avoids several problems with using try/finally directly:
131
* an exception from func will not be obscured by a subsequent exception
133
* an exception from a cleanup will not prevent other cleanups from
134
running (but the first exception encountered is still the one
137
Unike `run_cleanup`, `do_with_cleanups` can propagate an exception from a
138
cleanup, but only if there is no exception from func.
140
# As correct as Python 2.4 allows.
144
# We have an exception from func already, so suppress cleanup errors.
145
run_cleanups(cleanup_funcs)
148
# No exception from func, so allow the first exception from
149
# cleanup_funcs to propagate if one occurs (but only after running all
152
for cleanup in cleanup_funcs:
153
# XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
154
# won't run all cleanups... perhaps we should temporarily install a
160
# This is the first cleanup to fail, so remember its
162
exc_info = sys.exc_info()
164
# We already have an exception to propagate, so log any errors
165
# but don't propagate them.
167
if exc_info is not None:
168
raise exc_info[0], exc_info[1], exc_info[2]
169
# No error, so we can return the result