~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/library_state.py

  • Committer: Martin Packman
  • Date: 2012-01-05 09:50:04 UTC
  • mfrom: (6424 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6426.
  • Revision ID: martin.packman@canonical.com-20120105095004-mia9xb7y0efmto0v
Merge bzr.dev to resolve conflicts in bzrlib.builtins

Show diffs side-by-side

added added

removed removed

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