/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: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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
26
import breezy
28
27
from .lazy_import import lazy_import
29
28
lazy_import(globals(), """
30
29
from breezy import (
 
30
    cleanup,
31
31
    config,
32
32
    osutils,
33
33
    symbol_versioning,
41
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 breezy._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 exit_stack: An ExitStack which can be used for cleanups that
 
47
    :ivar saved_state: The breezy.global_state at the time __enter__ was
 
48
        called.
 
49
    :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
48
50
        should occur when the use of breezy is completed. This is initialised
49
51
        in __enter__ and executed in __exit__.
50
52
    """
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
96
        if breezy.version_info[3] == 'final':
95
 
            self.exit_stack.callback(
 
97
            self.cleanups.add_cleanup(
96
98
                symbol_versioning.suppress_deprecation_warnings(override=True))
97
99
 
98
100
        self._trace.__enter__()
99
101
 
100
102
        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__()
 
103
        breezy.ui.ui_factory = self._ui
 
104
        self._ui.__enter__()
104
105
 
105
 
        if breezy._global_state is not None:
106
 
            raise RuntimeError("Breezy already initialized")
107
 
        breezy._global_state = self
 
106
        self.saved_state = breezy.global_state
 
107
        breezy.global_state = self
108
108
        self.started = True
109
109
 
110
110
    def __exit__(self, exc_type, exc_val, exc_tb):
112
112
            # Save config changes
113
113
            for k, store in self.config_stores.items():
114
114
                store.save_changes()
115
 
        self.exit_stack.close()
 
115
        self.cleanups.cleanup_now()
116
116
        trace._flush_stdout_stderr()
117
117
        trace._flush_trace()
118
118
        osutils.report_extension_load_failures()
119
 
        if self._ui is not None:
120
 
            self._ui.__exit__(None, None, None)
 
119
        self._ui.__exit__(None, None, None)
121
120
        self._trace.__exit__(None, None, None)
122
121
        ui.ui_factory = self._orig_ui
123
 
        breezy._global_state = None
124
 
        return False  # propogate exceptions.
 
122
        breezy.global_state = self.saved_state
 
123
        return False # propogate exceptions.