/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-06-25 06:12:56 UTC
  • mto: This revision was merged to the branch mainline in revision 5324.
  • Revision ID: robertc@robertcollins.net-20100625061256-x2mbkcmcz5xb81wx
Move BzrLibraryState to its own module and prepare to start testing it.

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
 
 
27
 
 
28
class BzrLibraryState(object):
 
29
    """The state about how bzrlib has been configured.
 
30
 
 
31
    This is the core state needed to make use of bzr. The current instance is
 
32
    currently always exposed as bzrlib.global_state, but we desired to move
 
33
    to a point where no global state is needed at all.
 
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
 
39
        in __enter__ and executed in __exit__.
 
40
    """
 
41
 
 
42
    def __init__(self, setup_ui=True, stdin=None, stdout=None, stderr=None):
 
43
        """Create library start for normal use of bzrlib.
 
44
 
 
45
        Most applications that embed bzrlib, including bzr itself, should just
 
46
        call bzrlib.initialize(), but it is possible to use the state class
 
47
        directly.
 
48
 
 
49
        More options may be added in future so callers should use named
 
50
        arguments.
 
51
 
 
52
        BzrLibraryState implements the Python 2.5 Context Manager protocol
 
53
        PEP343, and can be used with the with statement. Upon __enter__ the
 
54
        global variables in use by bzr are set, and they are cleared on
 
55
        __exit__.
 
56
 
 
57
        :param setup_ui: If true (default) use a terminal UI; otherwise 
 
58
            some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
 
59
            the caller.
 
60
        :param stdin, stdout, stderr: If provided, use these for terminal IO;
 
61
            otherwise use the files in `sys`.
 
62
        """
 
63
        self.setup_ui = setup_ui
 
64
        self.stdin = stdin
 
65
        self.stdout = stdout
 
66
        self.stderr = stderr
 
67
 
 
68
    def __enter__(self):
 
69
        # NB: This function tweaks so much global state it's hard to test it in
 
70
        # isolation within the same interpreter.  It's not reached on normal
 
71
        # in-process run_bzr calls.  If it's broken, we expect that
 
72
        # TestRunBzrSubprocess may fail.
 
73
        import bzrlib
 
74
        if bzrlib.version_info[3] == 'final':
 
75
            from bzrlib.symbol_versioning import suppress_deprecation_warnings
 
76
            warning_cleanup = suppress_deprecation_warnings(override=True)
 
77
        else:
 
78
            warning_cleanup = None
 
79
 
 
80
        import bzrlib.cleanup
 
81
        import bzrlib.trace
 
82
        self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
 
83
        if warning_cleanup:
 
84
            self.cleanups.add_cleanup(warning_cleanup)
 
85
        bzrlib.trace.enable_default_logging()
 
86
 
 
87
        if self.setup_ui:
 
88
            import bzrlib.ui
 
89
            stdin = self.stdin or sys.stdin
 
90
            stdout = self.stdout or sys.stdout
 
91
            stderr = self.stderr or sys.stderr
 
92
            bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
 
93
                stdin, stdout, stderr)
 
94
        self.saved_state = bzrlib.global_state
 
95
        bzrlib.global_state = self
 
96
        return self # This is bound to the 'as' clause in a with statement.
 
97
 
 
98
    def __exit__(self, exc_type, exc_val, exc_tb):
 
99
        self.cleanups.cleanup_now()
 
100
        import bzrlib.ui
 
101
        bzrlib.trace._flush_stdout_stderr()
 
102
        bzrlib.trace._flush_trace()
 
103
        import bzrlib.osutils
 
104
        bzrlib.osutils.report_extension_load_failures()
 
105
        bzrlib.ui.ui_factory.__exit__(None, None, None)
 
106
        bzrlib.ui.ui_factory = None
 
107
        global global_state
 
108
        global_state = self.saved_state
 
109
        return False # propogate exceptions.