1
# Copyright (C) 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""The core state needed to make use of bzr is managed here."""
26
class BzrLibraryState(object):
27
"""The state about how bzrlib has been configured.
29
This is the core state needed to make use of bzr. The current instance is
30
currently always exposed as bzrlib.global_state, but we desired to move
31
to a point where no global state is needed at all.
33
:ivar saved_state: The bzrlib.global_state at the time __enter__ was
35
:ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
36
should occur when the use of bzrlib is completed. This is initialised
37
in __enter__ and executed in __exit__.
40
def __init__(self, ui, trace):
41
"""Create library start for normal use of bzrlib.
43
Most applications that embed bzrlib, including bzr itself, should just
44
call bzrlib.initialize(), but it is possible to use the state class
45
directly. The initialize() function provides sensible defaults for a
46
CLI program, such as a text UI factory.
48
More options may be added in future so callers should use named
51
BzrLibraryState implements the Python 2.5 Context Manager protocol
52
PEP343, and can be used with the with statement. Upon __enter__ the
53
global variables in use by bzr are set, and they are cleared on
56
:param ui: A bzrlib.ui.ui_factory to use.
57
:param trace: A bzrlib.trace.Config context manager to use, perhaps
58
bzrlib.trace.DefaultConfig.
64
# NB: This function tweaks so much global state it's hard to test it in
65
# isolation within the same interpreter. It's not reached on normal
66
# in-process run_bzr calls. If it's broken, we expect that
67
# TestRunBzrSubprocess may fail.
69
if bzrlib.version_info[3] == 'final':
70
from bzrlib.symbol_versioning import suppress_deprecation_warnings
71
warning_cleanup = suppress_deprecation_warnings(override=True)
73
warning_cleanup = None
76
self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
78
self.cleanups.add_cleanup(warning_cleanup)
79
self._trace.__enter__()
82
self._orig_ui = ui.ui_factory
83
ui.ui_factory = self._ui
86
self.saved_state = bzrlib.global_state
87
bzrlib.global_state = self
88
return self # This is bound to the 'as' clause in a with statement.
90
def __exit__(self, exc_type, exc_val, exc_tb):
91
self.cleanups.cleanup_now()
92
from bzrlib import (osutils, trace, ui)
93
trace._flush_stdout_stderr()
95
osutils.report_extension_load_failures()
96
self._ui.__exit__(None, None, None)
97
self._trace.__exit__(None, None, None)
98
ui.ui_factory = self._orig_ui
100
global_state = self.saved_state
101
return False # propogate exceptions.