~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/library_state.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:56:05 UTC
  • mfrom: (6615.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201195605-o7rl92wf6uyum3fk
(vila) Open trunk again as 2.8b1 (Vincent Ladeuil)

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):
31
43
    This is the core state needed to make use of bzr. The current instance is
32
44
    currently always exposed as bzrlib.global_state, but we desired to move
33
45
    to a point where no global state is needed at all.
34
 
    
 
46
 
35
47
    :ivar saved_state: The bzrlib.global_state at the time __enter__ was
36
48
        called.
37
49
    :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
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()
 
79
        # No config stores are cached to start with
 
80
        self.config_stores = {} # By url
 
81
        self.started = False
64
82
 
65
83
    def __enter__(self):
 
84
        if not self.started:
 
85
            self._start()
 
86
        return self # This is bound to the 'as' clause in a with statement.
 
87
 
 
88
    def _start(self):
 
89
        """Do all initialization."""
66
90
        # NB: This function tweaks so much global state it's hard to test it in
67
91
        # isolation within the same interpreter.  It's not reached on normal
68
92
        # in-process run_bzr calls.  If it's broken, we expect that
69
93
        # TestRunBzrSubprocess may fail.
70
 
        import bzrlib
 
94
        self.cleanups = cleanup.ObjectWithCleanups()
 
95
 
71
96
        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
 
97
            self.cleanups.add_cleanup(
 
98
                symbol_versioning.suppress_deprecation_warnings(override=True))
76
99
 
77
 
        import bzrlib.cleanup
78
 
        self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
79
 
        if warning_cleanup:
80
 
            self.cleanups.add_cleanup(warning_cleanup)
81
100
        self._trace.__enter__()
82
101
 
83
102
        self._orig_ui = bzrlib.ui.ui_factory
86
105
 
87
106
        self.saved_state = bzrlib.global_state
88
107
        bzrlib.global_state = self
89
 
        return self # This is bound to the 'as' clause in a with statement.
 
108
        self.started = True
90
109
 
91
110
    def __exit__(self, exc_type, exc_val, exc_tb):
 
111
        if exc_type is None:
 
112
            # Save config changes
 
113
            for k, store in self.config_stores.iteritems():
 
114
                store.save_changes()
92
115
        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()
 
116
        trace._flush_stdout_stderr()
 
117
        trace._flush_trace()
 
118
        osutils.report_extension_load_failures()
98
119
        self._ui.__exit__(None, None, None)
99
120
        self._trace.__exit__(None, None, None)
100
 
        bzrlib.ui.ui_factory = self._orig_ui
101
 
        global global_state
102
 
        global_state = self.saved_state
 
121
        ui.ui_factory = self._orig_ui
 
122
        bzrlib.global_state = self.saved_state
103
123
        return False # propogate exceptions.