/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/cleanup.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 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
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.
 
30
 
 
31
If you want to be certain that the first, and only the first, error is raised,
 
32
then use::
 
33
 
 
34
    operation = OperationWithCleanups(do_something)
 
35
    operation.add_cleanup(cleanup_something)
 
36
    operation.run_simple()
 
37
 
 
38
This is more inconvenient (because you need to make every try block a
 
39
function), but will ensure that the first error encountered is the one raised,
 
40
while also ensuring all cleanups are run.  See OperationWithCleanups for more
 
41
details.
 
42
"""
 
43
 
 
44
from __future__ import absolute_import
 
45
 
 
46
from collections import deque
 
47
from . import (
 
48
    debug,
 
49
    trace,
 
50
    )
 
51
 
 
52
 
 
53
def _log_cleanup_error(exc):
 
54
    trace.mutter('Cleanup failed:')
 
55
    trace.log_exception_quietly()
 
56
    if 'cleanup' in debug.debug_flags:
 
57
        trace.warning('brz: warning: Cleanup failed: %s', exc)
 
58
 
 
59
 
 
60
def _run_cleanup(func, *args, **kwargs):
 
61
    """Run func(*args, **kwargs), logging but not propagating any error it
 
62
    raises.
 
63
 
 
64
    :returns: True if func raised no errors, else False.
 
65
    """
 
66
    try:
 
67
        func(*args, **kwargs)
 
68
    except KeyboardInterrupt:
 
69
        raise
 
70
    except Exception as exc:
 
71
        _log_cleanup_error(exc)
 
72
        return False
 
73
    return True
 
74
 
 
75
 
 
76
def _run_cleanups(funcs):
 
77
    """Run a series of cleanup functions."""
 
78
    for func, args, kwargs in funcs:
 
79
        _run_cleanup(func, *args, **kwargs)
 
80
 
 
81
 
 
82
class ObjectWithCleanups(object):
 
83
    """A mixin for objects that hold a cleanup list.
 
84
 
 
85
    Subclass or client code can call add_cleanup and then later `cleanup_now`.
 
86
    """
 
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):
 
105
    """A way to run some code with a dynamic cleanup list.
 
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
 
 
117
        def some_func(operation, args, ...):
 
118
            do_something()
 
119
            operation.add_cleanup(something)
 
120
            # etc
 
121
 
 
122
    Note that the first argument passed to `some_func` will be the
 
123
    OperationWithCleanups object.  To invoke `some_func` without that, use
 
124
    `run_simple` instead of `run`.
 
125
    """
 
126
 
 
127
    def __init__(self, func):
 
128
        super(OperationWithCleanups, self).__init__()
 
129
        self.func = func
 
130
 
 
131
    def run(self, *args, **kwargs):
 
132
        return _do_with_cleanups(
 
133
            self.cleanups, self.func, self, *args, **kwargs)
 
134
 
 
135
    def run_simple(self, *args, **kwargs):
 
136
        return _do_with_cleanups(
 
137
            self.cleanups, self.func, *args, **kwargs)
 
138
 
 
139
 
 
140
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
 
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:
 
150
            return func(*args, **kwargs)
 
151
        finally:
 
152
            for cleanup, cargs, ckwargs in cleanup_funcs:
 
153
                cleanup(*cargs, **ckwargs)
 
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
 
 
162
    Unike `_run_cleanup`, `_do_with_cleanups` can propagate an exception from a
 
163
    cleanup, but only if there is no exception from func.
 
164
    """
 
165
    try:
 
166
        result = func(*args, **kwargs)
 
167
    except BaseException:
 
168
        # We have an exception from func already, so suppress cleanup errors.
 
169
        _run_cleanups(cleanup_funcs)
 
170
        raise
 
171
    # No exception from func, so allow first cleanup error to propgate.
 
172
    pending_cleanups = iter(cleanup_funcs)
 
173
    try:
 
174
        for cleanup, c_args, c_kwargs in pending_cleanups:
 
175
            cleanup(*c_args, **c_kwargs)
 
176
    except BaseException:
 
177
        # Still run the remaining cleanups but suppress any further errors.
 
178
        _run_cleanups(pending_cleanups)
 
179
        raise
 
180
    # No error, so we can return the result
 
181
    return result