/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: Jelmer Vernooij
  • Date: 2011-12-19 10:58:39 UTC
  • mfrom: (6383 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6386.
  • Revision ID: jelmer@canonical.com-20111219105839-uji05ck4rkm1mj4j
Merge bzr.dev.

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