/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:26:22 UTC
  • mfrom: (7167.1.4 run-flake8)
  • Revision ID: breezy.the.bot@gmail.com-20181116182622-qw3gan3hz78a2imw
Add a flake8 test.

Merged from https://code.launchpad.net/~jelmer/brz/run-flake8/+merge/358902

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
details.
42
42
"""
43
43
 
 
44
from __future__ import absolute_import
44
45
 
45
46
from collections import deque
46
 
import sys
47
 
from bzrlib import (
 
47
from . import (
48
48
    debug,
49
49
    trace,
50
50
    )
53
53
    trace.mutter('Cleanup failed:')
54
54
    trace.log_exception_quietly()
55
55
    if 'cleanup' in debug.debug_flags:
56
 
        trace.warning('bzr: warning: Cleanup failed: %s', exc)
 
56
        trace.warning('brz: warning: Cleanup failed: %s', exc)
57
57
 
58
58
 
59
59
def _run_cleanup(func, *args, **kwargs):
66
66
        func(*args, **kwargs)
67
67
    except KeyboardInterrupt:
68
68
        raise
69
 
    except Exception, exc:
 
69
    except Exception as exc:
70
70
        _log_cleanup_error(exc)
71
71
        return False
72
72
    return True
78
78
        _run_cleanup(func, *args, **kwargs)
79
79
 
80
80
 
81
 
class OperationWithCleanups(object):
 
81
class ObjectWithCleanups(object):
 
82
    """A mixin for objects that hold a cleanup list.
 
83
 
 
84
    Subclass or client code can call add_cleanup and then later `cleanup_now`.
 
85
    """
 
86
    def __init__(self):
 
87
        self.cleanups = deque()
 
88
 
 
89
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
90
        """Add a cleanup to run.
 
91
 
 
92
        Cleanups may be added at any time.  
 
93
        Cleanups will be executed in LIFO order.
 
94
        """
 
95
        self.cleanups.appendleft((cleanup_func, args, kwargs))
 
96
 
 
97
    def cleanup_now(self):
 
98
        _run_cleanups(self.cleanups)
 
99
        self.cleanups.clear()
 
100
 
 
101
 
 
102
class OperationWithCleanups(ObjectWithCleanups):
82
103
    """A way to run some code with a dynamic cleanup list.
83
104
 
84
105
    This provides a way to add cleanups while the function-with-cleanups is
102
123
    """
103
124
 
104
125
    def __init__(self, func):
 
126
        super(OperationWithCleanups, self).__init__()
105
127
        self.func = func
106
 
        self.cleanups = deque()
107
 
 
108
 
    def add_cleanup(self, cleanup_func, *args, **kwargs):
109
 
        """Add a cleanup to run.
110
 
 
111
 
        Cleanups may be added at any time before or during the execution of
112
 
        self.func.  Cleanups will be executed in LIFO order.
113
 
        """
114
 
        self.cleanups.appendleft((cleanup_func, args, kwargs))
115
128
 
116
129
    def run(self, *args, **kwargs):
117
130
        return _do_with_cleanups(
121
134
        return _do_with_cleanups(
122
135
            self.cleanups, self.func, *args, **kwargs)
123
136
 
124
 
    def cleanup_now(self):
125
 
        _run_cleanups(self.cleanups)
126
 
        self.cleanups.clear()
127
 
 
128
137
 
129
138
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
130
139
    """Run `func`, then call all the cleanup_funcs.
151
160
    Unike `_run_cleanup`, `_do_with_cleanups` can propagate an exception from a
152
161
    cleanup, but only if there is no exception from func.
153
162
    """
154
 
    # As correct as Python 2.4 allows.
155
163
    try:
156
164
        result = func(*args, **kwargs)
157
165
    except:
158
166
        # We have an exception from func already, so suppress cleanup errors.
159
167
        _run_cleanups(cleanup_funcs)
160
168
        raise
161
 
    else:
162
 
        # No exception from func, so allow the first exception from
163
 
        # cleanup_funcs to propagate if one occurs (but only after running all
164
 
        # of them).
165
 
        exc_info = None
166
 
        for cleanup, c_args, c_kwargs in cleanup_funcs:
167
 
            # XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
168
 
            # won't run all cleanups... perhaps we should temporarily install a
169
 
            # SIGINT handler?
170
 
            if exc_info is None:
171
 
                try:
172
 
                    cleanup(*c_args, **c_kwargs)
173
 
                except:
174
 
                    # This is the first cleanup to fail, so remember its
175
 
                    # details.
176
 
                    exc_info = sys.exc_info()
177
 
            else:
178
 
                # We already have an exception to propagate, so log any errors
179
 
                # but don't propagate them.
180
 
                _run_cleanup(cleanup, *c_args, **kwargs)
181
 
        if exc_info is not None:
182
 
            raise exc_info[0], exc_info[1], exc_info[2]
183
 
        # No error, so we can return the result
184
 
        return result
185
 
 
186
 
 
 
169
    # No exception from func, so allow first cleanup error to propgate.
 
170
    pending_cleanups = iter(cleanup_funcs)
 
171
    try:
 
172
        for cleanup, c_args, c_kwargs in pending_cleanups:
 
173
            cleanup(*c_args, **c_kwargs)
 
174
    except:
 
175
        # Still run the remaining cleanups but suppress any further errors.
 
176
        _run_cleanups(pending_cleanups)
 
177
        raise
 
178
    # No error, so we can return the result
 
179
    return result