~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/library_state.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

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()
 
79
        # No config stores are cached to start with
 
80
        self.config_stores = {} # By url
64
81
        self.started = False
65
82
 
66
83
    def __enter__(self):
69
86
        return self # This is bound to the 'as' clause in a with statement.
70
87
 
71
88
    def _start(self):
72
 
        """Do all initialization.
73
 
        """        
 
89
        """Do all initialization."""
74
90
        # NB: This function tweaks so much global state it's hard to test it in
75
91
        # isolation within the same interpreter.  It's not reached on normal
76
92
        # in-process run_bzr calls.  If it's broken, we expect that
77
93
        # TestRunBzrSubprocess may fail.
78
 
        import bzrlib
 
94
        self.cleanups = cleanup.ObjectWithCleanups()
 
95
 
79
96
        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
 
97
            self.cleanups.add_cleanup(
 
98
                symbol_versioning.suppress_deprecation_warnings(override=True))
84
99
 
85
 
        import bzrlib.cleanup
86
 
        self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
87
 
        if warning_cleanup:
88
 
            self.cleanups.add_cleanup(warning_cleanup)
89
100
        self._trace.__enter__()
90
101
 
91
102
        self._orig_ui = bzrlib.ui.ui_factory
97
108
        self.started = True
98
109
 
99
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()
100
115
        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()
 
116
        trace._flush_stdout_stderr()
 
117
        trace._flush_trace()
 
118
        osutils.report_extension_load_failures()
106
119
        self._ui.__exit__(None, None, None)
107
120
        self._trace.__exit__(None, None, None)
108
 
        bzrlib.ui.ui_factory = self._orig_ui
109
 
        global global_state
110
 
        global_state = self.saved_state
 
121
        ui.ui_factory = self._orig_ui
 
122
        bzrlib.global_state = self.saved_state
111
123
        return False # propogate exceptions.