37
37
# timestamps relative to program start in the log file kept by bzrlib.trace.
38
38
_start_time = time.time()
43
44
IGNORE_FILENAME = ".bzrignore"
46
__copyright__ = "Copyright 2005-2010 Canonical Ltd."
47
__copyright__ = "Copyright 2005-2011 Canonical Ltd."
48
49
# same format as sys.version_info: "A tuple containing the five components of
49
50
# the version number: major, minor, micro, releaselevel, and serial. All
52
53
# Python version 2.0 is (2, 0, 0, 'final', 0)." Additionally we use a
53
54
# releaselevel of 'dev' for unreleased under-development code.
55
version_info = (2, 3, 0, 'dev', 2)
56
version_info = (2, 5, 0, 'dev', 5)
57
58
# API compatibility version
58
api_minimum_version = (2, 2, 0)
59
api_minimum_version = (2, 4, 0)
61
62
def _format_version_tuple(version_info):
72
73
>>> print _format_version_tuple((1, 2, 0, 'dev', 0))
74
>>> print bzrlib._format_version_tuple((1, 2, 0, 'dev', 1))
75
>>> print _format_version_tuple((1, 2, 0, 'dev', 1))
76
77
>>> print _format_version_tuple((1, 1, 1, 'candidate', 2))
78
>>> print bzrlib._format_version_tuple((2, 1, 0, 'beta', 1))
79
>>> print _format_version_tuple((2, 1, 0, 'beta', 1))
80
81
>>> print _format_version_tuple((1, 4, 0))
82
83
>>> print _format_version_tuple((1, 4))
84
>>> print bzrlib._format_version_tuple((2, 1, 0, 'final', 1))
85
Traceback (most recent call last):
87
ValueError: version_info (2, 1, 0, 'final', 1) not valid
85
>>> print _format_version_tuple((2, 1, 0, 'final', 42))
88
87
>>> print _format_version_tuple((1, 4, 0, 'wibble', 0))
89
Traceback (most recent call last):
91
ValueError: version_info (1, 4, 0, 'wibble', 0) not valid
93
90
if len(version_info) == 2:
94
91
main_version = '%d.%d' % version_info[:2]
100
97
release_type = version_info[3]
101
98
sub = version_info[4]
103
# check they're consistent
104
100
if release_type == 'final' and sub == 0:
102
elif release_type == 'final':
103
sub_string = '.' + str(sub)
106
104
elif release_type == 'dev' and sub == 0:
107
105
sub_string = 'dev'
108
106
elif release_type == 'dev':
114
112
elif release_type == 'candidate':
115
113
sub_string = 'rc' + str(sub)
117
raise ValueError("version_info %r not valid" % (version_info,))
115
return '.'.join(map(str, version_info))
119
117
return main_version + sub_string
134
132
__version__ = _format_version_tuple(version_info)
135
133
version_string = __version__
136
def _patch_filesystem_default_encoding(new_enc):
137
"""Change the Python process global encoding for filesystem names
139
The effect is to change how open() and other builtin functions handle
140
unicode filenames on posix systems. This should only be done near startup.
142
The new encoding string passed to this function must survive until process
143
termination, otherwise the interpreter may access uninitialized memory.
144
The use of intern() may defer breakage is but is not enough, the string
145
object should be secure against module reloading and during teardown.
151
pythonapi = getattr(ctypes, "pythonapi", None)
152
if pythonapi is None:
153
# Not CPython ctypes implementation
155
old_ptr = ctypes.c_void_p.in_dll(pythonapi, "Py_FileSystemDefaultEncoding")
156
new_ptr = ctypes.cast(ctypes.c_char_p(intern(new_enc)), ctypes.c_void_p)
157
old_ptr.value = new_ptr.value
158
if sys.getfilesystemencoding() != new_enc:
159
raise RuntimeError("Failed to change the filesystem default encoding")
163
# When running under the bzr script, override bad filesystem default encoding.
164
# This is not safe to do for all users of bzrlib, other scripts should instead
165
# just ensure a usable locale is set via the $LANG variable on posix systems.
166
_fs_enc = sys.getfilesystemencoding()
167
if getattr(sys, "_bzr_default_fs_enc", None) is not None:
168
if (_fs_enc is None or codecs.lookup(_fs_enc).name == "ascii"):
169
_fs_enc = _patch_filesystem_default_encoding(sys._bzr_default_fs_enc)
173
_fs_enc = codecs.lookup(_fs_enc).name
137
176
# bzr has various bits of global state that are slowly being eliminated.
138
177
# This variable is intended to permit any new state-like things to be attached
139
178
# to a library_state.BzrLibraryState object rather than getting new global
140
179
# variables that need to be hunted down. Accessing the current BzrLibraryState
141
180
# through this variable is not encouraged: it is better to pass it around as
142
181
# 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
182
# that is too hard, it is better to use this variable than to make a brand new
144
183
# global variable.
145
184
# If using this variable by looking it up (because it can't be easily obtained)
146
185
# it is important to store the reference you get, rather than looking it up
158
197
More options may be added in future so callers should use named arguments.
199
The object returned by this function can be used as a contex manager
200
through the 'with' statement to automatically shut down when the process
201
is finished with bzrlib. However (from bzr 2.4) it's not necessary to
202
separately enter the context as well as starting bzr: bzrlib is ready to
203
go when this function returns.
160
205
:param setup_ui: If true (default) use a terminal UI; otherwise
161
206
some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
163
208
:param stdin, stdout, stderr: If provided, use these for terminal IO;
164
209
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__
210
:return: A context manager for the use of bzrlib. The __exit__
167
211
should be called by the caller before exiting their process or
168
212
otherwise stopping use of bzrlib. Advanced callers can use
169
213
BzrLibraryState directly.