/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: Andrew Bennetts
  • Date: 2009-09-23 06:31:48 UTC
  • mto: (4744.3.1 robust-cleanup-in-commit)
  • mto: This revision was merged to the branch mainline in revision 4775.
  • Revision ID: andrew.bennetts@canonical.com-20090923063148-hu5061v0yk0ebn11
More tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
Any errors from `cleanup_something` will be logged, but not raised.
29
29
Importantly, any errors from do_something will be propagated.
30
30
 
31
 
If a failure in a cleanup function should be reported to a user, then use::
32
 
 
33
 
    run_cleanup_reporting_errors(cleanup_something)
34
 
 
35
 
This will emit a trace.warning with the error in addition to logging it.
36
 
 
37
31
There is also convenience function for running multiple, independent cleanups
38
32
in sequence: run_cleanups.  e.g.::
39
33
 
45
39
Developers can use the `-Dcleanup` debug flag to cause cleanup errors to be
46
40
reported in the UI as well as logged.
47
41
 
48
 
XXX: what about the case where do_something succeeds, but cleanup fails, and
49
 
that matters?
 
42
Note the tradeoff that run_cleanup/run_cleanups makes: errors from
 
43
`do_something` will not be obscured by errors from `cleanup_something`, but
 
44
errors from `cleanup_something` will never reach the user, even if there is not
 
45
error from `do_something`.
50
46
 
51
 
XXX: perhaps this:
 
47
If you want to be certain that the first, and only the first, error is raised,
 
48
then use::
52
49
 
53
50
    do_with_cleanups(do_something, cleanups)
54
51
 
55
 
    this can be pedantically correct, at the cost of inconveniencing the
56
 
    callsite.
 
52
This is more inconvenient (because you need to make every try block a
 
53
function), but will ensure that the first error encountered is the one raised,
 
54
while also ensuring all cleanups are run.
57
55
"""
58
56
 
59
57
 
86
84
    return True
87
85
 
88
86
 
89
 
#def run_cleanup_reporting_errors(func, *args, **kwargs):
90
 
#    try:
91
 
#        func(*args, **kwargs)
92
 
#    except KeyboardInterrupt:
93
 
#        raise
94
 
#    except Exception, exc:
95
 
#        trace.mutter('Cleanup failed:')
96
 
#        trace.log_exception_quietly()
97
 
#        trace.warning('Cleanup failed: %s', exc)
98
 
#        return False
99
 
#    return True
 
87
def run_cleanup_reporting_errors(func, *args, **kwargs):
 
88
    try:
 
89
        func(*args, **kwargs)
 
90
    except KeyboardInterrupt:
 
91
        raise
 
92
    except Exception, exc:
 
93
        trace.mutter('Cleanup failed:')
 
94
        trace.log_exception_quietly()
 
95
        trace.warning('Cleanup failed: %s', exc)
 
96
        return False
 
97
    return True
100
98
 
101
99
 
102
100
def run_cleanups(funcs, on_error='log'):