~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/library_state.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 19:18:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216191839-eg681lxqibi1qxu1
Fix remaining tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import sys
24
24
 
25
25
import bzrlib
 
26
from bzrlib.lazy_import import lazy_import
 
27
lazy_import(globals(), """
 
28
from bzrlib import (
 
29
    cleanup,
 
30
    config,
 
31
    osutils,
 
32
    symbol_versioning,
 
33
    trace,
 
34
    ui,
 
35
    )
 
36
""")
26
37
 
27
38
 
28
39
class BzrLibraryState(object):
31
42
    This is the core state needed to make use of bzr. The current instance is
32
43
    currently always exposed as bzrlib.global_state, but we desired to move
33
44
    to a point where no global state is needed at all.
34
 
    
 
45
 
35
46
    :ivar saved_state: The bzrlib.global_state at the time __enter__ was
36
47
        called.
37
48
    :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
61
72
        """
62
73
        self._ui = ui
63
74
        self._trace = trace
 
75
        # There is no overrides by default, they are set later when the command
 
76
        # arguments are parsed.
 
77
        self.cmdline_overrides = config.CommandLineStore()
 
78
        self.started = False
64
79
 
65
80
    def __enter__(self):
 
81
        if not self.started:
 
82
            self._start()
 
83
        return self # This is bound to the 'as' clause in a with statement.
 
84
 
 
85
    def _start(self):
 
86
        """Do all initialization."""
66
87
        # NB: This function tweaks so much global state it's hard to test it in
67
88
        # isolation within the same interpreter.  It's not reached on normal
68
89
        # in-process run_bzr calls.  If it's broken, we expect that
69
90
        # TestRunBzrSubprocess may fail.
70
 
        import bzrlib
 
91
        self.cleanups = cleanup.ObjectWithCleanups()
 
92
 
71
93
        if bzrlib.version_info[3] == 'final':
72
 
            from bzrlib.symbol_versioning import suppress_deprecation_warnings
73
 
            warning_cleanup = suppress_deprecation_warnings(override=True)
74
 
        else:
75
 
            warning_cleanup = None
 
94
            self.cleanups.add_cleanup(
 
95
                symbol_versioning.suppress_deprecation_warnings(override=True))
76
96
 
77
 
        import bzrlib.cleanup
78
 
        self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
79
 
        if warning_cleanup:
80
 
            self.cleanups.add_cleanup(warning_cleanup)
81
97
        self._trace.__enter__()
82
98
 
83
99
        self._orig_ui = bzrlib.ui.ui_factory
86
102
 
87
103
        self.saved_state = bzrlib.global_state
88
104
        bzrlib.global_state = self
89
 
        return self # This is bound to the 'as' clause in a with statement.
 
105
        self.started = True
90
106
 
91
107
    def __exit__(self, exc_type, exc_val, exc_tb):
92
108
        self.cleanups.cleanup_now()
93
 
        import bzrlib.ui
94
 
        bzrlib.trace._flush_stdout_stderr()
95
 
        bzrlib.trace._flush_trace()
96
 
        import bzrlib.osutils
97
 
        bzrlib.osutils.report_extension_load_failures()
 
109
        trace._flush_stdout_stderr()
 
110
        trace._flush_trace()
 
111
        osutils.report_extension_load_failures()
98
112
        self._ui.__exit__(None, None, None)
99
113
        self._trace.__exit__(None, None, None)
100
 
        bzrlib.ui.ui_factory = self._orig_ui
101
 
        global global_state
102
 
        global_state = self.saved_state
 
114
        ui.ui_factory = self._orig_ui
 
115
        bzrlib.global_state = self.saved_state
103
116
        return False # propogate exceptions.