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

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

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