19
19
Developer documentation is available at
20
https://www.breezy-vcs.org/developers/.
22
Some particularly interesting things in breezy are:
24
* breezy.initialize -- setup the library for use
25
* breezy.plugin.load_plugins -- load all installed plugins
26
* breezy.branch.Branch.open -- open a branch
27
* breezy.workingtree.WorkingTree.open -- open a working tree
20
http://doc.bazaar.canonical.com/bzr.dev/developers/
22
The project website is at http://bazaar.canonical.com/
24
Some particularly interesting things in brzlib are:
26
* brzlib.initialize -- setup the library for use
27
* brzlib.plugin.load_plugins -- load all installed plugins
28
* brzlib.branch.Branch.open -- open a branch
29
* brzlib.workingtree.WorkingTree.open -- open a working tree
29
31
We hope you enjoy this library.
34
from __future__ import absolute_import
34
# Keep track of when breezy was first imported, so that we can give rough
35
# timestamps relative to program start in the log file kept by breezy.trace.
38
# Keep track of when brzlib was first imported, so that we can give rough
39
# timestamps relative to program start in the log file kept by brzlib.trace.
36
40
_start_time = time.time()
43
"Copyright 2005-2012 Canonical Ltd.\n"
44
"Copyright 2017-2020 Breezy developers"
46
IGNORE_FILENAME = ".bzrignore"
49
__copyright__ = "Copyright 2005-2012 Canonical Ltd."
47
51
# same format as sys.version_info: "A tuple containing the five components of
48
52
# the version number: major, minor, micro, releaselevel, and serial. All
51
55
# Python version 2.0 is (2, 0, 0, 'final', 0)." Additionally we use a
52
56
# releaselevel of 'dev' for unreleased under-development code.
54
version_info = (3, 2, 0, 'dev', 0)
58
version_info = (2, 8, 0, 'dev', 1)
60
# API compatibility version
61
api_minimum_version = (2, 4, 0)
57
64
def _format_version_tuple(version_info):
63
70
This also checks that the version is reasonable: the sub-release must be
64
71
zero for final releases.
66
>>> print(_format_version_tuple((1, 0, 0, 'final', 0)))
73
>>> print _format_version_tuple((1, 0, 0, 'final', 0))
68
>>> print(_format_version_tuple((1, 2, 0, 'dev', 0)))
75
>>> print _format_version_tuple((1, 2, 0, 'dev', 0))
70
>>> print(_format_version_tuple((1, 2, 0, 'dev', 1)))
77
>>> print _format_version_tuple((1, 2, 0, 'dev', 1))
72
>>> print(_format_version_tuple((1, 1, 1, 'candidate', 2)))
79
>>> print _format_version_tuple((1, 1, 1, 'candidate', 2))
74
>>> print(_format_version_tuple((2, 1, 0, 'beta', 1)))
81
>>> print _format_version_tuple((2, 1, 0, 'beta', 1))
76
>>> print(_format_version_tuple((1, 4, 0)))
83
>>> print _format_version_tuple((1, 4, 0))
78
>>> print(_format_version_tuple((1, 4)))
85
>>> print _format_version_tuple((1, 4))
80
>>> print(_format_version_tuple((2, 1, 0, 'final', 42)))
87
>>> print _format_version_tuple((2, 1, 0, 'final', 42))
82
>>> print(_format_version_tuple((1, 4, 0, 'wibble', 0)))
89
>>> print _format_version_tuple((1, 4, 0, 'wibble', 0))
85
92
if len(version_info) == 2:
112
119
return main_version + sub_string
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, '_brz_lazy_regex', False):
126
# The 'bzr' executable sets _brz_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._brz_lazy_regex
130
import brzlib.lazy_regex
131
brzlib.lazy_regex.install_lazy_compile()
115
134
__version__ = _format_version_tuple(version_info)
116
135
version_string = __version__
119
138
def _patch_filesystem_default_encoding(new_enc):
120
139
"""Change the Python process global encoding for filesystem names
122
141
The effect is to change how open() and other builtin functions handle
123
142
unicode filenames on posix systems. This should only be done near startup.
132
151
old_ptr = ctypes.c_void_p.in_dll(ctypes.pythonapi,
133
"Py_FileSystemDefaultEncoding")
134
has_enc = ctypes.c_int.in_dll(ctypes.pythonapi,
135
"Py_HasFileSystemDefaultEncoding")
136
as_utf8 = ctypes.PYFUNCTYPE(
137
ctypes.POINTER(ctypes.c_char), ctypes.py_object)(
138
("PyUnicode_AsUTF8", ctypes.pythonapi))
152
"Py_FileSystemDefaultEncoding")
139
153
except (ImportError, ValueError):
140
return # No ctypes or not CPython implementation, do nothing
141
new_enc = sys.intern(new_enc)
142
enc_ptr = as_utf8(new_enc)
144
old_ptr.value = ctypes.cast(enc_ptr, ctypes.c_void_p).value
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
145
157
if sys.getfilesystemencoding() != new_enc:
146
158
raise RuntimeError("Failed to change the filesystem default encoding")
150
# When running under the brz script, override bad filesystem default encoding.
151
# This is not safe to do for all users of breezy, other scripts should instead
162
# When running under the bzr script, override bad filesystem default encoding.
163
# This is not safe to do for all users of brzlib, other scripts should instead
152
164
# just ensure a usable locale is set via the $LANG variable on posix systems.
153
165
_fs_enc = sys.getfilesystemencoding()
154
if getattr(sys, "_brz_default_fs_enc", None) is not None:
166
if getattr(sys, "_bzr_default_fs_enc", None) is not None:
155
167
if (_fs_enc is None or codecs.lookup(_fs_enc).name == "ascii"):
156
_fs_enc = _patch_filesystem_default_encoding(sys._brz_default_fs_enc)
168
_fs_enc = _patch_filesystem_default_encoding(sys._bzr_default_fs_enc)
157
169
if _fs_enc is None:
158
170
_fs_enc = "ascii"
160
172
_fs_enc = codecs.lookup(_fs_enc).name
163
# brz has various bits of global state that are slowly being eliminated.
175
# bzr has various bits of global state that are slowly being eliminated.
164
176
# This variable is intended to permit any new state-like things to be attached
165
177
# to a library_state.BzrLibraryState object rather than getting new global
166
178
# variables that need to be hunted down. Accessing the current BzrLibraryState
170
182
# global variable.
171
183
# If using this variable by looking it up (because it can't be easily obtained)
172
184
# it is important to store the reference you get, rather than looking it up
173
# repeatedly; that way your code will behave properly in the breezy test suite
185
# repeatedly; that way your code will behave properly in the brzlib test suite
174
186
# and from programs that do use multiple library contexts.
178
190
def initialize(setup_ui=True, stdin=None, stdout=None, stderr=None):
179
"""Set up everything needed for normal use of breezy.
191
"""Set up everything needed for normal use of brzlib.
181
Most applications that embed breezy, including brz itself, should call
182
this function to initialize various subsystems.
193
Most applications that embed brzlib, including bzr itself, should call
194
this function to initialize various subsystems.
184
196
More options may be added in future so callers should use named arguments.
186
198
The object returned by this function can be used as a contex manager
187
199
through the 'with' statement to automatically shut down when the process
188
is finished with breezy. However it's not necessary to
189
separately enter the context as well as starting brz: breezy is ready to
200
is finished with brzlib. However (from bzr 2.4) it's not necessary to
201
separately enter the context as well as starting bzr: brzlib is ready to
190
202
go when this function returns.
192
:param setup_ui: If true (default) use a terminal UI; otherwise
193
some other ui_factory must be assigned to `breezy.ui.ui_factory` by
204
:param setup_ui: If true (default) use a terminal UI; otherwise
205
some other ui_factory must be assigned to `brzlib.ui.ui_factory` by
195
207
:param stdin, stdout, stderr: If provided, use these for terminal IO;
196
208
otherwise use the files in `sys`.
197
:return: A context manager for the use of breezy. The __exit__
209
:return: A context manager for the use of brzlib. The __exit__
198
210
should be called by the caller before exiting their process or
199
otherwise stopping use of breezy. Advanced callers can use
211
otherwise stopping use of brzlib. Advanced callers can use
200
212
BzrLibraryState directly.
202
from breezy import library_state, trace
214
from brzlib import library_state, trace
205
217
stdin = stdin or sys.stdin
206
218
stdout = stdout or sys.stdout
207
219
stderr = stderr or sys.stderr
208
ui_factory = breezy.ui.make_ui_for_terminal(stdin, stdout, stderr)
220
ui_factory = brzlib.ui.make_ui_for_terminal(stdin, stdout, stderr)
210
222
ui_factory = None
211
223
tracer = trace.DefaultConfig()