/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
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
34
    do_with_cleanups(do_something, cleanups)
35
4634.85.6 by Andrew Bennetts
More tests.
36
This is more inconvenient (because you need to make every try block a
37
function), but will ensure that the first error encountered is the one raised,
38
while also ensuring all cleanups are run.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
39
"""
40
41
42
import sys
4634.85.3 by Andrew Bennetts
Lots more tests.
43
from bzrlib import (
44
    debug,
45
    trace,
46
    )
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
47
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
48
def _log_cleanup_error(exc):
4634.85.3 by Andrew Bennetts
Lots more tests.
49
    trace.mutter('Cleanup failed:')
50
    trace.log_exception_quietly()
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
51
    if 'cleanup' in debug.debug_flags:
52
        trace.warning('bzr: warning: Cleanup failed: %s', exc)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
53
54
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.
55
def _run_cleanup(func, *args, **kwargs):
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
56
    """Run func(*args, **kwargs), logging but not propagating any error it
57
    raises.
58
59
    :returns: True if func raised no errors, else False.
60
    """
61
    try:
62
        func(*args, **kwargs)
63
    except KeyboardInterrupt:
64
        raise
4634.85.5 by Andrew Bennetts
Add unit test for -Dcleanup behaviour.
65
    except Exception, exc:
66
        _log_cleanup_error(exc)
67
        return False
68
    return True
69
70
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.
71
def _run_cleanup_reporting_errors(func, *args, **kwargs):
4634.85.6 by Andrew Bennetts
More tests.
72
    try:
73
        func(*args, **kwargs)
74
    except KeyboardInterrupt:
75
        raise
76
    except Exception, exc:
77
        trace.mutter('Cleanup failed:')
78
        trace.log_exception_quietly()
79
        trace.warning('Cleanup failed: %s', exc)
80
        return False
81
    return True
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
82
83
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.
84
def _run_cleanups(funcs, on_error='log'):
4634.85.7 by Andrew Bennetts
Comment and docstring gardening.
85
    """Run a series of cleanup functions.
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
86
87
    :param errors: One of 'log', 'warn first', 'warn all'
88
    """
89
    seen_error = False
90
    for func in funcs:
91
        if on_error == 'log' or (on_error == 'warn first' and seen_error):
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.
92
            seen_error |= _run_cleanup(func)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
93
        else:
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.
94
            seen_error |= _run_cleanup_reporting_errors(func)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
95
96
97
def do_with_cleanups(func, cleanup_funcs):
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
98
    """Run `func`, then call all the cleanup_funcs.
99
100
    All the cleanup_funcs are guaranteed to be run.  The first exception raised
101
    by func or any of the cleanup_funcs is the one that will be propagted by
102
    this function (subsequent errors are caught and logged).
103
104
    Conceptually similar to::
105
106
        try:
107
            return func()
108
        finally:
109
            for cleanup in cleanup_funcs:
110
                cleanup()
111
112
    It avoids several problems with using try/finally directly:
113
     * an exception from func will not be obscured by a subsequent exception
114
       from a cleanup.
115
     * an exception from a cleanup will not prevent other cleanups from
116
       running (but the first exception encountered is still the one
117
       propagated).
118
119
    Unike `run_cleanup`, `do_with_cleanups` can propagate an exception from a
120
    cleanup, but only if there is no exception from func.
121
    """
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
122
    # As correct as Python 2.4 allows.
123
    try:
4634.85.2 by Andrew Bennetts
Test and code tweak.
124
        result = func()
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
125
    except:
126
        # 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.
127
        _run_cleanups(cleanup_funcs)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
128
        raise
129
    else:
130
        # No exception from func, so allow the first exception from
131
        # cleanup_funcs to propagate if one occurs (but only after running all
132
        # of them).
133
        exc_info = None
134
        for cleanup in cleanup_funcs:
4634.85.3 by Andrew Bennetts
Lots more tests.
135
            # XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
4634.85.8 by Andrew Bennetts
Docstring and comment elaboration.
136
            # won't run all cleanups... perhaps we should temporarily install a
137
            # SIGINT handler?
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
138
            if exc_info is None:
139
                try:
140
                    cleanup()
141
                except:
142
                    # This is the first cleanup to fail, so remember its
143
                    # details.
144
                    exc_info = sys.exc_info()
145
            else:
146
                # We already have an exception to propagate, so log any errors
147
                # but don't propagate them.
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.
148
                _run_cleanup(cleanup)
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
149
        if exc_info is not None:
150
            raise exc_info[0], exc_info[1], exc_info[2]
4634.85.2 by Andrew Bennetts
Test and code tweak.
151
        # No error, so we can return the result
152
        return result
4634.85.1 by Andrew Bennetts
Begin defining cleanup helpers and their tests.
153
154