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."""
28
from .lazy_import import lazy_import
29
lazy_import(globals(), """
40
class BzrLibraryState(object):
41
"""The state about how breezy has been configured.
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
45
to a point where no global state is needed at all.
47
:ivar exit_stack: An ExitStack which can be used for cleanups that
48
should occur when the use of breezy is completed. This is initialised
49
in __enter__ and executed in __exit__.
52
def __init__(self, ui, trace):
53
"""Create library start for normal use of breezy.
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
directly. The initialize() function provides sensible defaults for a
58
CLI program, such as a text UI factory.
60
More options may be added in future so callers should use named
63
BzrLibraryState implements the Python 2.5 Context Manager protocol
64
PEP343, and can be used with the with statement. Upon __enter__ the
65
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.
74
# There is no overrides by default, they are set later when the command
75
# arguments are parsed.
76
self.cmdline_overrides = config.CommandLineStore()
77
# No config stores are cached to start with
78
self.config_stores = {} # By url
84
return self # This is bound to the 'as' clause in a with statement.
87
"""Do all initialization."""
88
# NB: This function tweaks so much global state it's hard to test it in
89
# isolation within the same interpreter. It's not reached on normal
90
# in-process run_bzr calls. If it's broken, we expect that
91
# TestRunBzrSubprocess may fail.
92
self.exit_stack = contextlib.ExitStack()
94
if breezy.version_info[3] == 'final':
95
self.exit_stack.callback(
96
symbol_versioning.suppress_deprecation_warnings(override=True))
98
self._trace.__enter__()
100
self._orig_ui = breezy.ui.ui_factory
101
if self._ui is not None:
102
breezy.ui.ui_factory = self._ui
105
if breezy._global_state is not None:
106
raise RuntimeError("Breezy already initialized")
107
breezy._global_state = self
110
def __exit__(self, exc_type, exc_val, exc_tb):
112
# Save config changes
113
for k, store in self.config_stores.items():
115
self.exit_stack.close()
116
trace._flush_stdout_stderr()
118
osutils.report_extension_load_failures()
119
if self._ui is not None:
120
self._ui.__exit__(None, None, None)
121
self._trace.__exit__(None, None, None)
122
ui.ui_factory = self._orig_ui
123
breezy._global_state = None
124
return False # propogate exceptions.