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