/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
19
__all__ = [
20
    'BzrLibraryState',
21
    ]
22
5582.10.44 by Jelmer Vernooij
Clean up patch.
23
7479.2.1 by Jelmer Vernooij
Drop python2 support.
24
import contextlib
25
26
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
27
import breezy
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
28
from .lazy_import import lazy_import
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
29
lazy_import(globals(), """
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
30
from breezy import (
6311.2.2 by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state
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):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
41
    """The state about how breezy has been configured.
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
42
43
    This is the core state needed to make use of bzr. The current instance is
6759.4.1 by Jelmer Vernooij
Remove saved_state.
44
    currently always exposed as breezy._global_state, but we desired to move
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
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
7356.1.1 by Jelmer Vernooij
Use ExitStack context rather than brz-specific OperationWithCleanup.
47
    :ivar exit_stack: An ExitStack which can be used for cleanups that
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
48
        should occur when the use of breezy is completed. This is initialised
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
49
        in __enter__ and executed in __exit__.
50
    """
51
5320.2.5 by Robert Collins
Make bzrlib startup use a trace context manager.
52
    def __init__(self, ui, trace):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
53
        """Create library start for normal use of breezy.
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
54
6622.1.34 by Jelmer Vernooij
Rename brzlib => 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
5320.2.3 by Robert Collins
Restore the original ui_factory when existing BzrLibraryState.
57
        directly. The initialize() function provides sensible defaults for a
58
        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.
59
60
        More options may be added in future so callers should use named
61
        arguments.
62
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
66
        __exit__.
67
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
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.
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
71
        """
5320.2.3 by Robert Collins
Restore the original ui_factory when existing BzrLibraryState.
72
        self._ui = ui
5320.2.5 by Robert Collins
Make bzrlib startup use a trace context manager.
73
        self._trace = trace
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
74
        # There is no overrides by default, they are set later when the command
75
        # arguments are parsed.
6260.3.1 by Vincent Ladeuil
Switch ``bzr config`` to the new config implementation
76
        self.cmdline_overrides = config.CommandLineStore()
6499.3.3 by Vincent Ladeuil
Stop using _CompatibleStack now that local config files can be
77
        # No config stores are cached to start with
7143.15.2 by Jelmer Vernooij
Run autopep8.
78
        self.config_stores = {}  # By url
5728.4.1 by Martin Pool
bzrlib.initialize now does what you'd expect
79
        self.started = False
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
80
81
    def __enter__(self):
5728.4.1 by Martin Pool
bzrlib.initialize now does what you'd expect
82
        if not self.started:
83
            self._start()
7143.15.2 by Jelmer Vernooij
Run autopep8.
84
        return self  # This is bound to the 'as' clause in a with statement.
5728.4.1 by Martin Pool
bzrlib.initialize now does what you'd expect
85
86
    def _start(self):
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
87
        """Do all initialization."""
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
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.
7479.2.1 by Jelmer Vernooij
Drop python2 support.
92
        self.exit_stack = contextlib.ExitStack()
6311.2.2 by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state
93
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
94
        if breezy.version_info[3] == 'final':
7356.1.1 by Jelmer Vernooij
Use ExitStack context rather than brz-specific OperationWithCleanup.
95
            self.exit_stack.callback(
6311.2.2 by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state
96
                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.
97
5320.2.5 by Robert Collins
Make bzrlib startup use a trace context manager.
98
        self._trace.__enter__()
5320.2.2 by Robert Collins
Move BzrLibraryState to its own module and prepare to start testing it.
99
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
100
        self._orig_ui = breezy.ui.ui_factory
7490.8.1 by Jelmer Vernooij
Fix setup_ui=False.
101
        if self._ui is not None:
102
            breezy.ui.ui_factory = self._ui
103
            self._ui.__enter__()
5320.2.3 by Robert Collins
Restore the original ui_factory when existing BzrLibraryState.
104
6759.4.1 by Jelmer Vernooij
Remove saved_state.
105
        if breezy._global_state is not None:
106
            raise RuntimeError("Breezy already initialized")
107
        breezy._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
6621.2.26 by Martin
Misc set of changes to get started with selftest on Python 3
113
            for k, store in self.config_stores.items():
6499.3.3 by Vincent Ladeuil
Stop using _CompatibleStack now that local config files can be
114
                store.save_changes()
7356.1.1 by Jelmer Vernooij
Use ExitStack context rather than brz-specific OperationWithCleanup.
115
        self.exit_stack.close()
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()
7490.8.1 by Jelmer Vernooij
Fix setup_ui=False.
119
        if self._ui is not None:
120
            self._ui.__exit__(None, None, None)
5320.2.5 by Robert Collins
Make bzrlib startup use a trace context manager.
121
        self._trace.__exit__(None, None, None)
6311.2.2 by Martin Packman
Use lazy_import for all deferred imports in bzrlib.library_state
122
        ui.ui_factory = self._orig_ui
6759.4.1 by Jelmer Vernooij
Remove saved_state.
123
        breezy._global_state = None
7143.15.2 by Jelmer Vernooij
Run autopep8.
124
        return False  # propogate exceptions.