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
class BzrLibraryState(object):
29
"""The state about how bzrlib has been configured.
31
This is the core state needed to make use of bzr. The current instance is
32
currently always exposed as bzrlib.global_state, but we desired to move
33
to a point where no global state is needed at all.
35
:ivar saved_state: The bzrlib.global_state at the time __enter__ was
37
:ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
38
should occur when the use of bzrlib is completed. This is initialised
39
in __enter__ and executed in __exit__.
42
def __init__(self, setup_ui=True, stdin=None, stdout=None, stderr=None):
43
"""Create library start for normal use of bzrlib.
45
Most applications that embed bzrlib, including bzr itself, should just
46
call bzrlib.initialize(), but it is possible to use the state class
49
More options may be added in future so callers should use named
52
BzrLibraryState implements the Python 2.5 Context Manager protocol
53
PEP343, and can be used with the with statement. Upon __enter__ the
54
global variables in use by bzr are set, and they are cleared on
57
:param setup_ui: If true (default) use a terminal UI; otherwise
58
some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
60
:param stdin, stdout, stderr: If provided, use these for terminal IO;
61
otherwise use the files in `sys`.
63
self.setup_ui = setup_ui
69
# NB: This function tweaks so much global state it's hard to test it in
70
# isolation within the same interpreter. It's not reached on normal
71
# in-process run_bzr calls. If it's broken, we expect that
72
# TestRunBzrSubprocess may fail.
74
if bzrlib.version_info[3] == 'final':
75
from bzrlib.symbol_versioning import suppress_deprecation_warnings
76
warning_cleanup = suppress_deprecation_warnings(override=True)
78
warning_cleanup = None
82
self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
84
self.cleanups.add_cleanup(warning_cleanup)
85
bzrlib.trace.enable_default_logging()
89
stdin = self.stdin or sys.stdin
90
stdout = self.stdout or sys.stdout
91
stderr = self.stderr or sys.stderr
92
bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
93
stdin, stdout, stderr)
94
self.saved_state = bzrlib.global_state
95
bzrlib.global_state = self
96
return self # This is bound to the 'as' clause in a with statement.
98
def __exit__(self, exc_type, exc_val, exc_tb):
99
self.cleanups.cleanup_now()
101
bzrlib.trace._flush_stdout_stderr()
102
bzrlib.trace._flush_trace()
103
import bzrlib.osutils
104
bzrlib.osutils.report_extension_load_failures()
105
bzrlib.ui.ui_factory.__exit__(None, None, None)
106
bzrlib.ui.ui_factory = None
108
global_state = self.saved_state
109
return False # propogate exceptions.