~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright (C) 2009 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Helpers for managing cleanup functions and the errors they might raise.

Generally, code that wants to perform some cleanup at the end of an action will
look like this::

    from bzrlib.cleanups import run_cleanup
    try:
        do_something()
    finally:
        run_cleanup(cleanup_something)

Any errors from `cleanup_something` will be logged, but not raised.
Importantly, any errors from do_something will be propagated.

There is also convenience function for running multiple, independent cleanups
in sequence: run_cleanups.  e.g.::

    try:
        do_something()
    finally:
        run_cleanups([cleanup_func_a, cleanup_func_b], ...)

Developers can use the `-Dcleanup` debug flag to cause cleanup errors to be
reported in the UI as well as logged.

Note the tradeoff that run_cleanup/run_cleanups makes: errors from
`do_something` will not be obscured by errors from `cleanup_something`, but
errors from `cleanup_something` will never reach the user, even if there is no
error from `do_something`.  So run_cleanup is good to use when a failure of
internal housekeeping (e.g. failure to finish a progress bar) is unimportant to
a user.

If you want to be certain that the first, and only the first, error is raised,
then use::

    do_with_cleanups(do_something, cleanups)

This is more inconvenient (because you need to make every try block a
function), but will ensure that the first error encountered is the one raised,
while also ensuring all cleanups are run.
"""


import sys
from bzrlib import (
    debug,
    trace,
    )

def _log_cleanup_error(exc):
    trace.mutter('Cleanup failed:')
    trace.log_exception_quietly()
    if 'cleanup' in debug.debug_flags:
        trace.warning('bzr: warning: Cleanup failed: %s', exc)


def run_cleanup(func, *args, **kwargs):
    """Run func(*args, **kwargs), logging but not propagating any error it
    raises.

    :returns: True if func raised no errors, else False.
    """
    try:
        func(*args, **kwargs)
    except KeyboardInterrupt:
        raise
    except Exception, exc:
        _log_cleanup_error(exc)
        return False
    return True


def run_cleanup_reporting_errors(func, *args, **kwargs):
    try:
        func(*args, **kwargs)
    except KeyboardInterrupt:
        raise
    except Exception, exc:
        trace.mutter('Cleanup failed:')
        trace.log_exception_quietly()
        trace.warning('Cleanup failed: %s', exc)
        return False
    return True


def run_cleanups(funcs, on_error='log'):
    """Run a series of cleanup functions.

    :param errors: One of 'log', 'warn first', 'warn all'
    """
    seen_error = False
    for func in funcs:
        if on_error == 'log' or (on_error == 'warn first' and seen_error):
            seen_error |= run_cleanup(func)
        else:
            seen_error |= run_cleanup_reporting_errors(func)


def do_with_cleanups(func, cleanup_funcs):
    """Run `func`, then call all the cleanup_funcs.

    All the cleanup_funcs are guaranteed to be run.  The first exception raised
    by func or any of the cleanup_funcs is the one that will be propagted by
    this function (subsequent errors are caught and logged).

    Conceptually similar to::

        try:
            return func()
        finally:
            for cleanup in cleanup_funcs:
                cleanup()

    It avoids several problems with using try/finally directly:
     * an exception from func will not be obscured by a subsequent exception
       from a cleanup.
     * an exception from a cleanup will not prevent other cleanups from
       running (but the first exception encountered is still the one
       propagated).

    Unike `run_cleanup`, `do_with_cleanups` can propagate an exception from a
    cleanup, but only if there is no exception from func.
    """
    # As correct as Python 2.4 allows.
    try:
        result = func()
    except:
        # We have an exception from func already, so suppress cleanup errors.
        run_cleanups(cleanup_funcs)
        raise
    else:
        # No exception from func, so allow the first exception from
        # cleanup_funcs to propagate if one occurs (but only after running all
        # of them).
        exc_info = None
        for cleanup in cleanup_funcs:
            # XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
            # won't run all cleanups... perhaps we should temporarily install a
            # SIGINT handler?
            if exc_info is None:
                try:
                    cleanup()
                except:
                    # This is the first cleanup to fail, so remember its
                    # details.
                    exc_info = sys.exc_info()
            else:
                # We already have an exception to propagate, so log any errors
                # but don't propagate them.
                run_cleanup(cleanup)
        if exc_info is not None:
            raise exc_info[0], exc_info[1], exc_info[2]
        # No error, so we can return the result
        return result