40
40
class BzrLibraryState(object):
41
"""The state about how breezy has been configured.
41
"""The state about how bzrlib has been configured.
43
43
This is the core state needed to make use of bzr. The current instance is
44
currently always exposed as breezy._global_state, but we desired to move
44
currently always exposed as bzrlib.global_state, but we desired to move
45
45
to a point where no global state is needed at all.
47
:ivar saved_state: The bzrlib.global_state at the time __enter__ was
47
49
:ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
48
should occur when the use of breezy is completed. This is initialised
50
should occur when the use of bzrlib is completed. This is initialised
49
51
in __enter__ and executed in __exit__.
52
54
def __init__(self, ui, trace):
53
"""Create library start for normal use of breezy.
55
"""Create library start for normal use of bzrlib.
55
Most applications that embed breezy, including bzr itself, should just
56
call breezy.initialize(), but it is possible to use the state class
57
Most applications that embed bzrlib, including bzr itself, should just
58
call bzrlib.initialize(), but it is possible to use the state class
57
59
directly. The initialize() function provides sensible defaults for a
58
60
CLI program, such as a text UI factory.
65
67
global variables in use by bzr are set, and they are cleared on
68
:param ui: A breezy.ui.ui_factory to use.
69
:param trace: A breezy.trace.Config context manager to use, perhaps
70
breezy.trace.DefaultConfig.
70
:param ui: A bzrlib.ui.ui_factory to use.
71
:param trace: A bzrlib.trace.Config context manager to use, perhaps
72
bzrlib.trace.DefaultConfig.
73
75
self._trace = trace
91
93
# TestRunBzrSubprocess may fail.
92
94
self.cleanups = cleanup.ObjectWithCleanups()
94
if breezy.version_info[3] == 'final':
96
if bzrlib.version_info[3] == 'final':
95
97
self.cleanups.add_cleanup(
96
98
symbol_versioning.suppress_deprecation_warnings(override=True))
98
100
self._trace.__enter__()
100
self._orig_ui = breezy.ui.ui_factory
101
breezy.ui.ui_factory = self._ui
102
self._orig_ui = bzrlib.ui.ui_factory
103
bzrlib.ui.ui_factory = self._ui
102
104
self._ui.__enter__()
104
if breezy._global_state is not None:
105
raise RuntimeError("Breezy already initialized")
106
breezy._global_state = self
106
self.saved_state = bzrlib.global_state
107
bzrlib.global_state = self
107
108
self.started = True
109
110
def __exit__(self, exc_type, exc_val, exc_tb):
110
111
if exc_type is None:
111
112
# Save config changes
112
for k, store in self.config_stores.items():
113
for k, store in self.config_stores.iteritems():
113
114
store.save_changes()
114
115
self.cleanups.cleanup_now()
115
116
trace._flush_stdout_stderr()
118
119
self._ui.__exit__(None, None, None)
119
120
self._trace.__exit__(None, None, None)
120
121
ui.ui_factory = self._orig_ui
121
breezy._global_state = None
122
bzrlib.global_state = self.saved_state
122
123
return False # propogate exceptions.