5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
1 |
# Copyright (C) 2010 Canonical Ltd
|
2 |
#
|
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
16 |
||
6379.6.7
by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear. |
17 |
"""The core state needed to make use of bzr is managed here."""
|
18 |
||
6379.6.3
by Jelmer Vernooij
Use absolute_import. |
19 |
from __future__ import absolute_import |
20 |
||
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
21 |
__all__ = [ |
22 |
'BzrLibraryState', |
|
23 |
]
|
|
24 |
||
5582.10.44
by Jelmer Vernooij
Clean up patch. |
25 |
|
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
26 |
import bzrlib |
6161.1.1
by Vincent Ladeuil
Allow config options to be overridden from the command line |
27 |
from bzrlib.lazy_import import lazy_import |
28 |
lazy_import(globals(), """ |
|
6311.2.2
by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state |
29 |
from bzrlib import (
|
30 |
cleanup,
|
|
31 |
config,
|
|
32 |
osutils,
|
|
33 |
symbol_versioning,
|
|
34 |
trace,
|
|
35 |
ui,
|
|
36 |
)
|
|
6161.1.1
by Vincent Ladeuil
Allow config options to be overridden from the command line |
37 |
""") |
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
38 |
|
39 |
||
40 |
class BzrLibraryState(object): |
|
41 |
"""The state about how bzrlib has been configured.
|
|
42 |
||
43 |
This is the core state needed to make use of bzr. The current instance is
|
|
44 |
currently always exposed as bzrlib.global_state, but we desired to move
|
|
45 |
to a point where no global state is needed at all.
|
|
5728.4.1
by Martin Pool
bzrlib.initialize now does what you'd expect |
46 |
|
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
47 |
:ivar saved_state: The bzrlib.global_state at the time __enter__ was
|
48 |
called.
|
|
49 |
:ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
|
|
50 |
should occur when the use of bzrlib is completed. This is initialised
|
|
51 |
in __enter__ and executed in __exit__.
|
|
52 |
"""
|
|
53 |
||
5320.2.5
by Robert Collins
Make bzrlib startup use a trace context manager. |
54 |
def __init__(self, ui, trace): |
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
55 |
"""Create library start for normal use of bzrlib.
|
56 |
||
57 |
Most applications that embed bzrlib, including bzr itself, should just
|
|
58 |
call bzrlib.initialize(), but it is possible to use the state class
|
|
5320.2.3
by Robert Collins
Restore the original ui_factory when existing BzrLibraryState. |
59 |
directly. The initialize() function provides sensible defaults for a
|
60 |
CLI program, such as a text UI factory.
|
|
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
61 |
|
62 |
More options may be added in future so callers should use named
|
|
63 |
arguments.
|
|
64 |
||
65 |
BzrLibraryState implements the Python 2.5 Context Manager protocol
|
|
66 |
PEP343, and can be used with the with statement. Upon __enter__ the
|
|
67 |
global variables in use by bzr are set, and they are cleared on
|
|
68 |
__exit__.
|
|
69 |
||
5320.2.3
by Robert Collins
Restore the original ui_factory when existing BzrLibraryState. |
70 |
:param ui: A bzrlib.ui.ui_factory to use.
|
5320.2.5
by Robert Collins
Make bzrlib startup use a trace context manager. |
71 |
:param trace: A bzrlib.trace.Config context manager to use, perhaps
|
72 |
bzrlib.trace.DefaultConfig.
|
|
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
73 |
"""
|
5320.2.3
by Robert Collins
Restore the original ui_factory when existing BzrLibraryState. |
74 |
self._ui = ui |
5320.2.5
by Robert Collins
Make bzrlib startup use a trace context manager. |
75 |
self._trace = trace |
6161.1.1
by Vincent Ladeuil
Allow config options to be overridden from the command line |
76 |
# There is no overrides by default, they are set later when the command
|
77 |
# arguments are parsed.
|
|
6260.3.1
by Vincent Ladeuil
Switch ``bzr config`` to the new config implementation |
78 |
self.cmdline_overrides = config.CommandLineStore() |
6499.3.3
by Vincent Ladeuil
Stop using _CompatibleStack now that local config files can be |
79 |
# No config stores are cached to start with
|
80 |
self.config_stores = {} # By url |
|
5728.4.1
by Martin Pool
bzrlib.initialize now does what you'd expect |
81 |
self.started = False |
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
82 |
|
83 |
def __enter__(self): |
|
5728.4.1
by Martin Pool
bzrlib.initialize now does what you'd expect |
84 |
if not self.started: |
85 |
self._start() |
|
86 |
return self # This is bound to the 'as' clause in a with statement. |
|
87 |
||
88 |
def _start(self): |
|
6161.1.1
by Vincent Ladeuil
Allow config options to be overridden from the command line |
89 |
"""Do all initialization."""
|
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
90 |
# NB: This function tweaks so much global state it's hard to test it in
|
91 |
# isolation within the same interpreter. It's not reached on normal
|
|
92 |
# in-process run_bzr calls. If it's broken, we expect that
|
|
93 |
# TestRunBzrSubprocess may fail.
|
|
6311.2.2
by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state |
94 |
self.cleanups = cleanup.ObjectWithCleanups() |
95 |
||
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
96 |
if bzrlib.version_info[3] == 'final': |
6311.2.2
by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state |
97 |
self.cleanups.add_cleanup( |
98 |
symbol_versioning.suppress_deprecation_warnings(override=True)) |
|
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
99 |
|
5320.2.5
by Robert Collins
Make bzrlib startup use a trace context manager. |
100 |
self._trace.__enter__() |
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
101 |
|
5582.10.44
by Jelmer Vernooij
Clean up patch. |
102 |
self._orig_ui = bzrlib.ui.ui_factory |
103 |
bzrlib.ui.ui_factory = self._ui |
|
5320.2.3
by Robert Collins
Restore the original ui_factory when existing BzrLibraryState. |
104 |
self._ui.__enter__() |
105 |
||
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
106 |
self.saved_state = bzrlib.global_state |
107 |
bzrlib.global_state = self |
|
5728.4.1
by Martin Pool
bzrlib.initialize now does what you'd expect |
108 |
self.started = True |
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
109 |
|
110 |
def __exit__(self, exc_type, exc_val, exc_tb): |
|
6499.3.3
by Vincent Ladeuil
Stop using _CompatibleStack now that local config files can be |
111 |
if exc_type is None: |
112 |
# Save config changes
|
|
113 |
for k, store in self.config_stores.iteritems(): |
|
114 |
store.save_changes() |
|
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
115 |
self.cleanups.cleanup_now() |
6311.2.2
by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state |
116 |
trace._flush_stdout_stderr() |
117 |
trace._flush_trace() |
|
118 |
osutils.report_extension_load_failures() |
|
5320.2.3
by Robert Collins
Restore the original ui_factory when existing BzrLibraryState. |
119 |
self._ui.__exit__(None, None, None) |
5320.2.5
by Robert Collins
Make bzrlib startup use a trace context manager. |
120 |
self._trace.__exit__(None, None, None) |
6311.2.2
by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state |
121 |
ui.ui_factory = self._orig_ui |
6311.2.1
by Martin Packman
Correctly restory global_state on library state exit |
122 |
bzrlib.global_state = self.saved_state |
5320.2.2
by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it. |
123 |
return False # propogate exceptions. |