~bzr-pqm/bzr/bzr.dev

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