/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 bzrlib/cleanup.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 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(lambda operation: do_something())
 
34
    operation = OperationWithCleanups(do_something)
35
35
    operation.add_cleanup(cleanup_something)
36
 
    operation.run()
 
36
    operation.run_simple()
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.
 
100
    OperationWithCleanups object.  To invoke `some_func` without that, use
 
101
    `run_simple` instead of `run`.
101
102
    """
102
103
 
103
104
    def __init__(self, func):
116
117
        return _do_with_cleanups(
117
118
            self.cleanups, self.func, self, *args, **kwargs)
118
119
 
 
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
 
119
128
 
120
129
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
121
130
    """Run `func`, then call all the cleanup_funcs.