/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""The core state needed to make use of bzr is managed here."""
18
 
 
19
 
__all__ = [
20
 
    'BzrLibraryState',
21
 
    ]
22
 
 
23
 
import sys
24
 
 
25
 
import bzrlib
26
 
from bzrlib.lazy_import import lazy_import
27
 
lazy_import(globals(), """
28
 
from bzrlib import (
29
 
    cleanup,
30
 
    config,
31
 
    osutils,
32
 
    symbol_versioning,
33
 
    trace,
34
 
    ui,
35
 
    )
36
 
""")
37
 
 
38
 
 
39
 
class BzrLibraryState(object):
40
 
    """The state about how bzrlib has been configured.
41
 
 
42
 
    This is the core state needed to make use of bzr. The current instance is
43
 
    currently always exposed as bzrlib.global_state, but we desired to move
44
 
    to a point where no global state is needed at all.
45
 
 
46
 
    :ivar saved_state: The bzrlib.global_state at the time __enter__ was
47
 
        called.
48
 
    :ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
49
 
        should occur when the use of bzrlib is completed. This is initialised
50
 
        in __enter__ and executed in __exit__.
51
 
    """
52
 
 
53
 
    def __init__(self, ui, trace):
54
 
        """Create library start for normal use of bzrlib.
55
 
 
56
 
        Most applications that embed bzrlib, including bzr itself, should just
57
 
        call bzrlib.initialize(), but it is possible to use the state class
58
 
        directly. The initialize() function provides sensible defaults for a
59
 
        CLI program, such as a text UI factory.
60
 
 
61
 
        More options may be added in future so callers should use named
62
 
        arguments.
63
 
 
64
 
        BzrLibraryState implements the Python 2.5 Context Manager protocol
65
 
        PEP343, and can be used with the with statement. Upon __enter__ the
66
 
        global variables in use by bzr are set, and they are cleared on
67
 
        __exit__.
68
 
 
69
 
        :param ui: A bzrlib.ui.ui_factory to use.
70
 
        :param trace: A bzrlib.trace.Config context manager to use, perhaps
71
 
            bzrlib.trace.DefaultConfig.
72
 
        """
73
 
        self._ui = ui
74
 
        self._trace = trace
75
 
        # There is no overrides by default, they are set later when the command
76
 
        # arguments are parsed.
77
 
        self.cmdline_overrides = config.CommandLineStore()
78
 
        self.started = False
79
 
 
80
 
    def __enter__(self):
81
 
        if not self.started:
82
 
            self._start()
83
 
        return self # This is bound to the 'as' clause in a with statement.
84
 
 
85
 
    def _start(self):
86
 
        """Do all initialization."""
87
 
        # NB: This function tweaks so much global state it's hard to test it in
88
 
        # isolation within the same interpreter.  It's not reached on normal
89
 
        # in-process run_bzr calls.  If it's broken, we expect that
90
 
        # TestRunBzrSubprocess may fail.
91
 
        self.cleanups = cleanup.ObjectWithCleanups()
92
 
 
93
 
        if bzrlib.version_info[3] == 'final':
94
 
            self.cleanups.add_cleanup(
95
 
                symbol_versioning.suppress_deprecation_warnings(override=True))
96
 
 
97
 
        self._trace.__enter__()
98
 
 
99
 
        self._orig_ui = bzrlib.ui.ui_factory
100
 
        bzrlib.ui.ui_factory = self._ui
101
 
        self._ui.__enter__()
102
 
 
103
 
        self.saved_state = bzrlib.global_state
104
 
        bzrlib.global_state = self
105
 
        self.started = True
106
 
 
107
 
    def __exit__(self, exc_type, exc_val, exc_tb):
108
 
        self.cleanups.cleanup_now()
109
 
        trace._flush_stdout_stderr()
110
 
        trace._flush_trace()
111
 
        osutils.report_extension_load_failures()
112
 
        self._ui.__exit__(None, None, None)
113
 
        self._trace.__exit__(None, None, None)
114
 
        ui.ui_factory = self._orig_ui
115
 
        bzrlib.global_state = self.saved_state
116
 
        return False # propogate exceptions.