/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/library_state.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""The core state needed to make use of bzr is managed here."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
__all__ = [
20
22
    'BzrLibraryState',
21
23
    ]
22
24
 
23
25
 
24
 
import contextlib
25
 
 
26
 
 
27
 
import breezy
28
 
from .lazy_import import lazy_import
 
26
import brzlib
 
27
from brzlib.lazy_import import lazy_import
29
28
lazy_import(globals(), """
30
 
from breezy import (
 
29
from brzlib import (
 
30
    cleanup,
31
31
    config,
32
32
    osutils,
33
33
    symbol_versioning,
38
38
 
39
39
 
40
40
class BzrLibraryState(object):
41
 
    """The state about how breezy has been configured.
 
41
    """The state about how brzlib has been configured.
42
42
 
43
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
 
44
    currently always exposed as brzlib.global_state, but we desired to move
45
45
    to a point where no global state is needed at all.
46
46
 
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
 
47
    :ivar saved_state: The brzlib.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 brzlib is completed. This is initialised
49
51
        in __enter__ and executed in __exit__.
50
52
    """
51
53
 
52
54
    def __init__(self, ui, trace):
53
 
        """Create library start for normal use of breezy.
 
55
        """Create library start for normal use of brzlib.
54
56
 
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
        Most applications that embed brzlib, including bzr itself, should just
 
58
        call brzlib.initialize(), but it is possible to use the state class
57
59
        directly. The initialize() function provides sensible defaults for a
58
60
        CLI program, such as a text UI factory.
59
61
 
65
67
        global variables in use by bzr are set, and they are cleared on
66
68
        __exit__.
67
69
 
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.
 
70
        :param ui: A brzlib.ui.ui_factory to use.
 
71
        :param trace: A brzlib.trace.Config context manager to use, perhaps
 
72
            brzlib.trace.DefaultConfig.
71
73
        """
72
74
        self._ui = ui
73
75
        self._trace = trace
75
77
        # arguments are parsed.
76
78
        self.cmdline_overrides = config.CommandLineStore()
77
79
        # No config stores are cached to start with
78
 
        self.config_stores = {}  # By url
 
80
        self.config_stores = {} # By url
79
81
        self.started = False
80
82
 
81
83
    def __enter__(self):
82
84
        if not self.started:
83
85
            self._start()
84
 
        return self  # This is bound to the 'as' clause in a with statement.
 
86
        return self # This is bound to the 'as' clause in a with statement.
85
87
 
86
88
    def _start(self):
87
89
        """Do all initialization."""
89
91
        # isolation within the same interpreter.  It's not reached on normal
90
92
        # in-process run_bzr calls.  If it's broken, we expect that
91
93
        # TestRunBzrSubprocess may fail.
92
 
        self.exit_stack = contextlib.ExitStack()
 
94
        self.cleanups = cleanup.ObjectWithCleanups()
93
95
 
94
 
        if breezy.version_info[3] == 'final':
95
 
            self.exit_stack.callback(
 
96
        if brzlib.version_info[3] == 'final':
 
97
            self.cleanups.add_cleanup(
96
98
                symbol_versioning.suppress_deprecation_warnings(override=True))
97
99
 
98
100
        self._trace.__enter__()
99
101
 
100
 
        self._orig_ui = breezy.ui.ui_factory
101
 
        breezy.ui.ui_factory = self._ui
 
102
        self._orig_ui = brzlib.ui.ui_factory
 
103
        brzlib.ui.ui_factory = self._ui
102
104
        self._ui.__enter__()
103
105
 
104
 
        if breezy._global_state is not None:
105
 
            raise RuntimeError("Breezy already initialized")
106
 
        breezy._global_state = self
 
106
        self.saved_state = brzlib.global_state
 
107
        brzlib.global_state = self
107
108
        self.started = True
108
109
 
109
110
    def __exit__(self, exc_type, exc_val, exc_tb):
110
111
        if exc_type is None:
111
112
            # Save config changes
112
 
            for k, store in self.config_stores.items():
 
113
            for k, store in self.config_stores.iteritems():
113
114
                store.save_changes()
114
 
        self.exit_stack.close()
 
115
        self.cleanups.cleanup_now()
115
116
        trace._flush_stdout_stderr()
116
117
        trace._flush_trace()
117
118
        osutils.report_extension_load_failures()
118
119
        self._ui.__exit__(None, None, None)
119
120
        self._trace.__exit__(None, None, None)
120
121
        ui.ui_factory = self._orig_ui
121
 
        breezy._global_state = None
122
 
        return False  # propogate exceptions.
 
122
        brzlib.global_state = self.saved_state
 
123
        return False # propogate exceptions.