~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/library_state.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-10-05 21:15:13 UTC
  • mfrom: (5448.3.5 374700-Add-gnu-lsh-support)
  • Revision ID: pqm@pqm.ubuntu.com-20101005211513-whouyj5t7oo92gmq
(gz) Add support for GNU lsh as a secure shell client (Matthew Gordon)

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
25
import bzrlib
27
 
from bzrlib.lazy_import import lazy_import
28
 
lazy_import(globals(), """
29
 
from bzrlib import (
30
 
    cleanup,
31
 
    config,
32
 
    osutils,
33
 
    symbol_versioning,
34
 
    trace,
35
 
    ui,
36
 
    )
37
 
""")
38
26
 
39
27
 
40
28
class BzrLibraryState(object):
43
31
    This is the core state needed to make use of bzr. The current instance is
44
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
 
 
 
34
    
47
35
    :ivar saved_state: The bzrlib.global_state at the time __enter__ was
48
36
        called.
49
37
    :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
73
61
        """
74
62
        self._ui = ui
75
63
        self._trace = trace
76
 
        # There is no overrides by default, they are set later when the command
77
 
        # arguments are parsed.
78
 
        self.cmdline_overrides = config.CommandLineStore()
79
 
        # No config stores are cached to start with
80
 
        self.config_stores = {} # By url
81
 
        self.started = False
82
64
 
83
65
    def __enter__(self):
84
 
        if not self.started:
85
 
            self._start()
86
 
        return self # This is bound to the 'as' clause in a with statement.
87
 
 
88
 
    def _start(self):
89
 
        """Do all initialization."""
90
66
        # NB: This function tweaks so much global state it's hard to test it in
91
67
        # isolation within the same interpreter.  It's not reached on normal
92
68
        # in-process run_bzr calls.  If it's broken, we expect that
93
69
        # TestRunBzrSubprocess may fail.
94
 
        self.cleanups = cleanup.ObjectWithCleanups()
95
 
 
 
70
        import bzrlib
96
71
        if bzrlib.version_info[3] == 'final':
97
 
            self.cleanups.add_cleanup(
98
 
                symbol_versioning.suppress_deprecation_warnings(override=True))
 
72
            from bzrlib.symbol_versioning import suppress_deprecation_warnings
 
73
            warning_cleanup = suppress_deprecation_warnings(override=True)
 
74
        else:
 
75
            warning_cleanup = None
99
76
 
 
77
        import bzrlib.cleanup
 
78
        self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
 
79
        if warning_cleanup:
 
80
            self.cleanups.add_cleanup(warning_cleanup)
100
81
        self._trace.__enter__()
101
82
 
102
83
        self._orig_ui = bzrlib.ui.ui_factory
105
86
 
106
87
        self.saved_state = bzrlib.global_state
107
88
        bzrlib.global_state = self
108
 
        self.started = True
 
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.iteritems():
114
 
                store.save_changes()
115
92
        self.cleanups.cleanup_now()
116
 
        trace._flush_stdout_stderr()
117
 
        trace._flush_trace()
118
 
        osutils.report_extension_load_failures()
 
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()
119
98
        self._ui.__exit__(None, None, None)
120
99
        self._trace.__exit__(None, None, None)
121
 
        ui.ui_factory = self._orig_ui
122
 
        bzrlib.global_state = self.saved_state
 
100
        bzrlib.ui.ui_factory = self._orig_ui
 
101
        global global_state
 
102
        global_state = self.saved_state
123
103
        return False # propogate exceptions.