~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/__init__.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2013, 2016, 2017 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
31
31
We hope you enjoy this library.
32
32
"""
33
33
 
 
34
from __future__ import absolute_import
 
35
 
34
36
import time
35
37
 
36
38
# Keep track of when bzrlib was first imported, so that we can give rough
37
39
# timestamps relative to program start in the log file kept by bzrlib.trace.
38
40
_start_time = time.time()
39
41
 
 
42
import codecs
40
43
import sys
41
44
 
42
45
 
43
46
IGNORE_FILENAME = ".bzrignore"
44
47
 
45
48
 
46
 
__copyright__ = "Copyright 2005-2010 Canonical Ltd."
 
49
__copyright__ = "Copyright 2005-2012 Canonical Ltd."
47
50
 
48
51
# same format as sys.version_info: "A tuple containing the five components of
49
52
# the version number: major, minor, micro, releaselevel, and serial. All
52
55
# Python version 2.0 is (2, 0, 0, 'final', 0)."  Additionally we use a
53
56
# releaselevel of 'dev' for unreleased under-development code.
54
57
 
55
 
version_info = (2, 3, 0, 'dev', 1)
 
58
version_info = (2, 8, 0, 'dev', 1)
56
59
 
57
60
# API compatibility version
58
 
api_minimum_version = (2, 2, 0)
 
61
api_minimum_version = (2, 4, 0)
59
62
 
60
63
 
61
64
def _format_version_tuple(version_info):
71
74
    1.0.0
72
75
    >>> print _format_version_tuple((1, 2, 0, 'dev', 0))
73
76
    1.2.0dev
74
 
    >>> print bzrlib._format_version_tuple((1, 2, 0, 'dev', 1))
 
77
    >>> print _format_version_tuple((1, 2, 0, 'dev', 1))
75
78
    1.2.0dev1
76
79
    >>> print _format_version_tuple((1, 1, 1, 'candidate', 2))
77
80
    1.1.1rc2
78
 
    >>> print bzrlib._format_version_tuple((2, 1, 0, 'beta', 1))
 
81
    >>> print _format_version_tuple((2, 1, 0, 'beta', 1))
79
82
    2.1b1
80
83
    >>> print _format_version_tuple((1, 4, 0))
81
84
    1.4.0
82
85
    >>> print _format_version_tuple((1, 4))
83
86
    1.4
84
 
    >>> print bzrlib._format_version_tuple((2, 1, 0, 'final', 1))
85
 
    Traceback (most recent call last):
86
 
    ...
87
 
    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
88
89
    >>> print _format_version_tuple((1, 4, 0, 'wibble', 0))
89
 
    Traceback (most recent call last):
90
 
    ...
91
 
    ValueError: version_info (1, 4, 0, 'wibble', 0) not valid
 
90
    1.4.0.wibble.0
92
91
    """
93
92
    if len(version_info) == 2:
94
93
        main_version = '%d.%d' % version_info[:2]
100
99
    release_type = version_info[3]
101
100
    sub = version_info[4]
102
101
 
103
 
    # check they're consistent
104
102
    if release_type == 'final' and sub == 0:
105
103
        sub_string = ''
 
104
    elif release_type == 'final':
 
105
        sub_string = '.' + str(sub)
106
106
    elif release_type == 'dev' and sub == 0:
107
107
        sub_string = 'dev'
108
108
    elif release_type == 'dev':
114
114
    elif release_type == 'candidate':
115
115
        sub_string = 'rc' + str(sub)
116
116
    else:
117
 
        raise ValueError("version_info %r not valid" % (version_info,))
 
117
        return '.'.join(map(str, version_info))
118
118
 
119
119
    return main_version + sub_string
120
120
 
134
134
__version__ = _format_version_tuple(version_info)
135
135
version_string = __version__
136
136
 
 
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
        old_ptr = ctypes.c_void_p.in_dll(ctypes.pythonapi,
 
152
            "Py_FileSystemDefaultEncoding")
 
153
    except (ImportError, ValueError):
 
154
        return # No ctypes or not CPython implementation, do nothing
 
155
    new_ptr = ctypes.cast(ctypes.c_char_p(intern(new_enc)), ctypes.c_void_p)
 
156
    old_ptr.value = new_ptr.value
 
157
    if sys.getfilesystemencoding() != new_enc:
 
158
        raise RuntimeError("Failed to change the filesystem default encoding")
 
159
    return new_enc
 
160
 
 
161
 
 
162
# When running under the bzr script, override bad filesystem default encoding.
 
163
# This is not safe to do for all users of bzrlib, other scripts should instead
 
164
# just ensure a usable locale is set via the $LANG variable on posix systems.
 
165
_fs_enc = sys.getfilesystemencoding()
 
166
if getattr(sys, "_bzr_default_fs_enc", None) is not None:
 
167
    if (_fs_enc is None or codecs.lookup(_fs_enc).name == "ascii"):
 
168
        _fs_enc = _patch_filesystem_default_encoding(sys._bzr_default_fs_enc)
 
169
if _fs_enc is None:
 
170
    _fs_enc = "ascii"
 
171
else:
 
172
    _fs_enc = codecs.lookup(_fs_enc).name
 
173
 
 
174
 
137
175
# bzr has various bits of global state that are slowly being eliminated.
138
176
# This variable is intended to permit any new state-like things to be attached
139
177
# to a library_state.BzrLibraryState object rather than getting new global
140
178
# variables that need to be hunted down. Accessing the current BzrLibraryState
141
179
# through this variable is not encouraged: it is better to pass it around as
142
180
# part of the context of an operation than to look it up directly, but when
143
 
# that is too hard, it is better to use this variable than to make a branch new
 
181
# that is too hard, it is better to use this variable than to make a brand new
144
182
# global variable.
145
183
# If using this variable by looking it up (because it can't be easily obtained)
146
184
# it is important to store the reference you get, rather than looking it up
157
195
 
158
196
    More options may be added in future so callers should use named arguments.
159
197
 
 
198
    The object returned by this function can be used as a contex manager
 
199
    through the 'with' statement to automatically shut down when the process
 
200
    is finished with bzrlib.  However (from bzr 2.4) it's not necessary to
 
201
    separately enter the context as well as starting bzr: bzrlib is ready to
 
202
    go when this function returns.
 
203
 
160
204
    :param setup_ui: If true (default) use a terminal UI; otherwise 
161
205
        some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
162
206
        the caller.
163
207
    :param stdin, stdout, stderr: If provided, use these for terminal IO;
164
208
        otherwise use the files in `sys`.
165
 
    :return: A context manager for the use of bzrlib. The __enter__ method of
166
 
        this context needs to be called before it takes effect, and the __exit__
 
209
    :return: A context manager for the use of bzrlib. The __exit__
167
210
        should be called by the caller before exiting their process or
168
211
        otherwise stopping use of bzrlib. Advanced callers can use
169
212
        BzrLibraryState directly.
178
221
    else:
179
222
        ui_factory = None
180
223
    tracer = trace.DefaultConfig()
181
 
    return library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
 
224
    state = library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
 
225
    # Start automatically in case people don't realize this returns a context.
 
226
    state._start()
 
227
    return state
182
228
 
183
229
 
184
230
def test_suite():