~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/cleanup.py

Add a NEWS entry and prepare submission.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
31
31
If you want to be certain that the first, and only the first, error is raised,
32
32
then use::
33
33
 
34
 
    operation = OperationWithCleanups(do_something)
 
34
    operation = OperationWithCleanups(lambda operation: do_something())
35
35
    operation.add_cleanup(cleanup_something)
36
 
    operation.run_simple()
 
36
    operation.run()
37
37
 
38
38
This is more inconvenient (because you need to make every try block a
39
39
function), but will ensure that the first error encountered is the one raised,
91
91
 
92
92
    where `some_func` is::
93
93
 
94
 
        def some_func(operation, args, ...):
 
94
        def some_func(operation, args, ...)
95
95
            do_something()
96
96
            operation.add_cleanup(something)
97
97
            # etc
98
98
 
99
99
    Note that the first argument passed to `some_func` will be the
100
 
    OperationWithCleanups object.  To invoke `some_func` without that, use
101
 
    `run_simple` instead of `run`.
 
100
    OperationWithCleanups object.
102
101
    """
103
102
 
104
103
    def __init__(self, func):
117
116
        return _do_with_cleanups(
118
117
            self.cleanups, self.func, self, *args, **kwargs)
119
118
 
120
 
    def run_simple(self, *args, **kwargs):
121
 
        return _do_with_cleanups(
122
 
            self.cleanups, self.func, *args, **kwargs)
123
 
 
124
 
    def cleanup_now(self):
125
 
        _run_cleanups(self.cleanups)
126
 
        self.cleanups.clear()
127
 
 
128
119
 
129
120
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
130
121
    """Run `func`, then call all the cleanup_funcs.