132
132
# bzr has various bits of global state that are slowly being eliminated.
133
133
# This variable is intended to permit any new state-like things to be attached
134
# to a BzrLibraryState object rather than getting new global variables that
135
# need to be hunted down. Accessing the current BzrLibraryState through this
136
# variable is not encouraged: it is better to pass it around as part of the
137
# context of an operation than to look it up directly, but when that is too
138
# hard, it is better to use this variable than to make a branch new global
134
# to a library_state.BzrLibraryState object rather than getting new global
135
# variables that need to be hunted down. Accessing the current BzrLibraryState
136
# through this variable is not encouraged: it is better to pass it around as
137
# part of the context of an operation than to look it up directly, but when
138
# that is too hard, it is better to use this variable than to make a branch new
140
140
# If using this variable by looking it up (because it can't be easily obtained)
141
141
# it is important to store the reference you get, rather than looking it up
142
142
# repeatedly; that way your code will behave properly in the bzrlib test suite
144
144
global_state = None
147
class BzrLibraryState(object):
148
"""The state about how bzrlib has been configured.
150
:ivar saved_state: The bzrlib.global_state at the time __enter__ was
152
:ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
153
should occur when the use of bzrlib is completed. This is initialised
154
in __enter__ and executed in __exit__.
157
def __init__(self, setup_ui=True, stdin=None, stdout=None, stderr=None):
158
"""Create library start for normal use of bzrlib.
160
Most applications that embed bzrlib, including bzr itself, should just
161
call bzrlib.initialize(), but it is possible to use the state class
164
More options may be added in future so callers should use named
167
BzrLibraryState implements the Python 2.5 Context Manager protocol
168
PEP343, and can be used with the with statement. Upon __enter__ the
169
global variables in use by bzr are set, and they are cleared on
172
:param setup_ui: If true (default) use a terminal UI; otherwise
173
some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
175
:param stdin, stdout, stderr: If provided, use these for terminal IO;
176
otherwise use the files in `sys`.
178
self.setup_ui = setup_ui
184
# NB: This function tweaks so much global state it's hard to test it in
185
# isolation within the same interpreter. It's not reached on normal
186
# in-process run_bzr calls. If it's broken, we expect that
187
# TestRunBzrSubprocess may fail.
188
if version_info[3] == 'final':
189
from bzrlib.symbol_versioning import suppress_deprecation_warnings
190
suppress_deprecation_warnings(override=True)
192
import bzrlib.cleanup
194
self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
195
bzrlib.trace.enable_default_logging()
199
stdin = self.stdin or sys.stdin
200
stdout = self.stdout or sys.stdout
201
stderr = self.stderr or sys.stderr
202
bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
203
stdin, stdout, stderr)
205
self.saved_state = global_state
207
return self # This is bound to the 'as' clause in a with statement.
209
def __exit__(self, exc_type, exc_val, exc_tb):
210
self.cleanups.cleanup_now()
212
bzrlib.trace._flush_stdout_stderr()
213
bzrlib.trace._flush_trace()
214
import bzrlib.osutils
215
bzrlib.osutils.report_extension_load_failures()
216
bzrlib.ui.ui_factory.__exit__(None, None, None)
217
bzrlib.ui.ui_factory = None
219
global_state = self.saved_state
220
return False # propogate exceptions.
223
147
def initialize(setup_ui=True, stdin=None, stdout=None, stderr=None):
224
148
"""Set up everything needed for normal use of bzrlib.
239
163
otherwise stopping use of bzrlib. Advanced callers can use
240
164
BzrLibraryState directly.
242
return BzrLibraryState(setup_ui=setup_ui, stdin=stdin,
243
stdout=stdout, stderr=stderr)
166
import bzrlib.library_state
169
stdin = stdin or sys.stdin
170
stdout = stdout or sys.stdout
171
stderr = stderr or sys.stderr
172
ui_factory = bzrlib.ui.make_ui_for_terminal(stdin, stdout, stderr)
175
tracer = bzrlib.trace.DefaultConfig()
176
return bzrlib.library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
246
179
def test_suite():