/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 breezy/library_state.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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