~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/__init__.py

(jelmer) Use the absolute_import feature everywhere in bzrlib,
 and add a source test to make sure it's used everywhere. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""bzr library"""
 
17
"""All of bzr.
 
18
 
 
19
Developer documentation is available at
 
20
http://doc.bazaar.canonical.com/bzr.dev/developers/
 
21
 
 
22
The project website is at http://bazaar.canonical.com/
 
23
 
 
24
Some particularly interesting things in bzrlib are:
 
25
 
 
26
 * bzrlib.initialize -- setup the library for use
 
27
 * bzrlib.plugin.load_plugins -- load all installed plugins
 
28
 * bzrlib.branch.Branch.open -- open a branch
 
29
 * bzrlib.workingtree.WorkingTree.open -- open a working tree
 
30
 
 
31
We hope you enjoy this library.
 
32
"""
 
33
 
 
34
from __future__ import absolute_import
18
35
 
19
36
import time
20
37
 
22
39
# timestamps relative to program start in the log file kept by bzrlib.trace.
23
40
_start_time = time.time()
24
41
 
 
42
import codecs
25
43
import sys
26
 
if getattr(sys, '_bzr_lazy_regex', False):
27
 
    # The 'bzr' executable sets _bzr_lazy_regex.  We install the lazy regex
28
 
    # hack as soon as possible so that as much of the standard library can
29
 
    # benefit, including the 'string' module.
30
 
    del sys._bzr_lazy_regex
31
 
    import bzrlib.lazy_regex
32
 
    bzrlib.lazy_regex.install_lazy_compile()
33
44
 
34
45
 
35
46
IGNORE_FILENAME = ".bzrignore"
36
47
 
37
48
 
38
 
__copyright__ = "Copyright 2005, 2006, 2007, 2008, 2009 Canonical Ltd."
 
49
__copyright__ = "Copyright 2005-2011 Canonical Ltd."
39
50
 
40
51
# same format as sys.version_info: "A tuple containing the five components of
41
52
# the version number: major, minor, micro, releaselevel, and serial. All
44
55
# Python version 2.0 is (2, 0, 0, 'final', 0)."  Additionally we use a
45
56
# releaselevel of 'dev' for unreleased under-development code.
46
57
 
47
 
version_info = (2, 1, 0, 'dev', 4)
 
58
version_info = (2, 5, 0, 'dev', 5)
48
59
 
49
 
# API compatibility version: bzrlib is currently API compatible with 1.15.
50
 
api_minimum_version = (2, 1, 0)
 
60
# API compatibility version
 
61
api_minimum_version = (2, 4, 0)
51
62
 
52
63
 
53
64
def _format_version_tuple(version_info):
63
74
    1.0.0
64
75
    >>> print _format_version_tuple((1, 2, 0, 'dev', 0))
65
76
    1.2.0dev
66
 
    >>> print bzrlib._format_version_tuple((1, 2, 0, 'dev', 1))
 
77
    >>> print _format_version_tuple((1, 2, 0, 'dev', 1))
67
78
    1.2.0dev1
68
79
    >>> print _format_version_tuple((1, 1, 1, 'candidate', 2))
69
80
    1.1.1rc2
70
 
    >>> print bzrlib._format_version_tuple((2, 1, 0, 'beta', 1))
71
 
    2.1.0b1
 
81
    >>> print _format_version_tuple((2, 1, 0, 'beta', 1))
 
82
    2.1b1
72
83
    >>> print _format_version_tuple((1, 4, 0))
73
84
    1.4.0
74
85
    >>> print _format_version_tuple((1, 4))
75
86
    1.4
76
 
    >>> print bzrlib._format_version_tuple((2, 1, 0, 'final', 1))
77
 
    Traceback (most recent call last):
78
 
    ...
79
 
    ValueError: version_info (2, 1, 0, 'final', 1) not valid
 
87
    >>> print _format_version_tuple((2, 1, 0, 'final', 42))
 
88
    2.1.0.42
80
89
    >>> print _format_version_tuple((1, 4, 0, 'wibble', 0))
81
 
    Traceback (most recent call last):
82
 
    ...
83
 
    ValueError: version_info (1, 4, 0, 'wibble', 0) not valid
 
90
    1.4.0.wibble.0
84
91
    """
85
92
    if len(version_info) == 2:
86
93
        main_version = '%d.%d' % version_info[:2]
92
99
    release_type = version_info[3]
93
100
    sub = version_info[4]
94
101
 
95
 
    # check they're consistent
96
102
    if release_type == 'final' and sub == 0:
97
103
        sub_string = ''
 
104
    elif release_type == 'final':
 
105
        sub_string = '.' + str(sub)
98
106
    elif release_type == 'dev' and sub == 0:
99
107
        sub_string = 'dev'
100
108
    elif release_type == 'dev':
101
109
        sub_string = 'dev' + str(sub)
102
110
    elif release_type in ('alpha', 'beta'):
 
111
        if version_info[2] == 0:
 
112
            main_version = '%d.%d' % version_info[:2]
103
113
        sub_string = release_type[0] + str(sub)
104
114
    elif release_type == 'candidate':
105
115
        sub_string = 'rc' + str(sub)
106
116
    else:
107
 
        raise ValueError("version_info %r not valid" % (version_info,))
 
117
        return '.'.join(map(str, version_info))
108
118
 
109
119
    return main_version + sub_string
110
120
 
111
121
 
 
122
# lazy_regex import must be done after _format_version_tuple definition
 
123
# to avoid "no attribute '_format_version_tuple'" error when using
 
124
# deprecated_function in the lazy_regex module.
 
125
if getattr(sys, '_bzr_lazy_regex', False):
 
126
    # The 'bzr' executable sets _bzr_lazy_regex.  We install the lazy regex
 
127
    # hack as soon as possible so that as much of the standard library can
 
128
    # benefit, including the 'string' module.
 
129
    del sys._bzr_lazy_regex
 
130
    import bzrlib.lazy_regex
 
131
    bzrlib.lazy_regex.install_lazy_compile()
 
132
 
 
133
 
112
134
__version__ = _format_version_tuple(version_info)
113
135
version_string = __version__
114
136
 
115
137
 
 
138
def _patch_filesystem_default_encoding(new_enc):
 
139
    """Change the Python process global encoding for filesystem names
 
140
    
 
141
    The effect is to change how open() and other builtin functions handle
 
142
    unicode filenames on posix systems. This should only be done near startup.
 
143
 
 
144
    The new encoding string passed to this function must survive until process
 
145
    termination, otherwise the interpreter may access uninitialized memory.
 
146
    The use of intern() may defer breakage is but is not enough, the string
 
147
    object should be secure against module reloading and during teardown.
 
148
    """
 
149
    try:
 
150
        import ctypes
 
151
    except ImportError:
 
152
        return
 
153
    pythonapi = getattr(ctypes, "pythonapi", None)
 
154
    if pythonapi is None:
 
155
        # Not CPython ctypes implementation
 
156
        return
 
157
    old_ptr = ctypes.c_void_p.in_dll(pythonapi, "Py_FileSystemDefaultEncoding")
 
158
    new_ptr = ctypes.cast(ctypes.c_char_p(intern(new_enc)), ctypes.c_void_p)
 
159
    old_ptr.value = new_ptr.value
 
160
    if sys.getfilesystemencoding() != new_enc:
 
161
        raise RuntimeError("Failed to change the filesystem default encoding")
 
162
    return new_enc
 
163
 
 
164
 
 
165
# When running under the bzr script, override bad filesystem default encoding.
 
166
# This is not safe to do for all users of bzrlib, other scripts should instead
 
167
# just ensure a usable locale is set via the $LANG variable on posix systems.
 
168
_fs_enc = sys.getfilesystemencoding()
 
169
if getattr(sys, "_bzr_default_fs_enc", None) is not None:
 
170
    if (_fs_enc is None or codecs.lookup(_fs_enc).name == "ascii"):
 
171
        _fs_enc = _patch_filesystem_default_encoding(sys._bzr_default_fs_enc)
 
172
if _fs_enc is None:
 
173
    _fs_enc = "ascii"
 
174
else:
 
175
    _fs_enc = codecs.lookup(_fs_enc).name
 
176
 
 
177
 
 
178
# bzr has various bits of global state that are slowly being eliminated.
 
179
# This variable is intended to permit any new state-like things to be attached
 
180
# to a library_state.BzrLibraryState object rather than getting new global
 
181
# variables that need to be hunted down. Accessing the current BzrLibraryState
 
182
# through this variable is not encouraged: it is better to pass it around as
 
183
# part of the context of an operation than to look it up directly, but when
 
184
# that is too hard, it is better to use this variable than to make a brand new
 
185
# global variable.
 
186
# If using this variable by looking it up (because it can't be easily obtained)
 
187
# it is important to store the reference you get, rather than looking it up
 
188
# repeatedly; that way your code will behave properly in the bzrlib test suite
 
189
# and from programs that do use multiple library contexts.
 
190
global_state = None
 
191
 
 
192
 
 
193
def initialize(setup_ui=True, stdin=None, stdout=None, stderr=None):
 
194
    """Set up everything needed for normal use of bzrlib.
 
195
 
 
196
    Most applications that embed bzrlib, including bzr itself, should call
 
197
    this function to initialize various subsystems.  
 
198
 
 
199
    More options may be added in future so callers should use named arguments.
 
200
 
 
201
    The object returned by this function can be used as a contex manager
 
202
    through the 'with' statement to automatically shut down when the process
 
203
    is finished with bzrlib.  However (from bzr 2.4) it's not necessary to
 
204
    separately enter the context as well as starting bzr: bzrlib is ready to
 
205
    go when this function returns.
 
206
 
 
207
    :param setup_ui: If true (default) use a terminal UI; otherwise 
 
208
        some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
 
209
        the caller.
 
210
    :param stdin, stdout, stderr: If provided, use these for terminal IO;
 
211
        otherwise use the files in `sys`.
 
212
    :return: A context manager for the use of bzrlib. The __exit__
 
213
        should be called by the caller before exiting their process or
 
214
        otherwise stopping use of bzrlib. Advanced callers can use
 
215
        BzrLibraryState directly.
 
216
    """
 
217
    from bzrlib import library_state, trace
 
218
    if setup_ui:
 
219
        import bzrlib.ui
 
220
        stdin = stdin or sys.stdin
 
221
        stdout = stdout or sys.stdout
 
222
        stderr = stderr or sys.stderr
 
223
        ui_factory = bzrlib.ui.make_ui_for_terminal(stdin, stdout, stderr)
 
224
    else:
 
225
        ui_factory = None
 
226
    tracer = trace.DefaultConfig()
 
227
    state = library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
 
228
    # Start automatically in case people don't realize this returns a context.
 
229
    state._start()
 
230
    return state
 
231
 
 
232
 
116
233
def test_suite():
117
234
    import tests
118
235
    return tests.test_suite()