/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 brzlib/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""All of bzr.
18
18
 
19
19
Developer documentation is available at
20
 
https://www.breezy-vcs.org/developers/.
21
 
 
22
 
Some particularly interesting things in breezy are:
23
 
 
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/
 
21
 
 
22
The project website is at http://bazaar.canonical.com/
 
23
 
 
24
Some particularly interesting things in brzlib are:
 
25
 
 
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
28
30
 
29
31
We hope you enjoy this library.
30
32
"""
31
33
 
 
34
from __future__ import absolute_import
 
35
 
32
36
import time
33
37
 
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()
37
41
 
38
42
import codecs
39
43
import sys
40
44
 
41
45
 
42
 
__copyright__ = (
43
 
    "Copyright 2005-2012 Canonical Ltd.\n"
44
 
    "Copyright 2017-2020 Breezy developers"
45
 
)
 
46
IGNORE_FILENAME = ".bzrignore"
 
47
 
 
48
 
 
49
__copyright__ = "Copyright 2005-2012 Canonical Ltd."
46
50
 
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.
53
57
 
54
 
version_info = (3, 2, 0, 'dev', 0)
 
58
version_info = (2, 8, 0, 'dev', 1)
 
59
 
 
60
# API compatibility version
 
61
api_minimum_version = (2, 4, 0)
55
62
 
56
63
 
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.
65
72
 
66
 
    >>> print(_format_version_tuple((1, 0, 0, 'final', 0)))
 
73
    >>> print _format_version_tuple((1, 0, 0, 'final', 0))
67
74
    1.0.0
68
 
    >>> print(_format_version_tuple((1, 2, 0, 'dev', 0)))
 
75
    >>> print _format_version_tuple((1, 2, 0, 'dev', 0))
69
76
    1.2.0dev
70
 
    >>> print(_format_version_tuple((1, 2, 0, 'dev', 1)))
 
77
    >>> print _format_version_tuple((1, 2, 0, 'dev', 1))
71
78
    1.2.0dev1
72
 
    >>> print(_format_version_tuple((1, 1, 1, 'candidate', 2)))
 
79
    >>> print _format_version_tuple((1, 1, 1, 'candidate', 2))
73
80
    1.1.1rc2
74
 
    >>> print(_format_version_tuple((2, 1, 0, 'beta', 1)))
 
81
    >>> print _format_version_tuple((2, 1, 0, 'beta', 1))
75
82
    2.1b1
76
 
    >>> print(_format_version_tuple((1, 4, 0)))
 
83
    >>> print _format_version_tuple((1, 4, 0))
77
84
    1.4.0
78
 
    >>> print(_format_version_tuple((1, 4)))
 
85
    >>> print _format_version_tuple((1, 4))
79
86
    1.4
80
 
    >>> print(_format_version_tuple((2, 1, 0, 'final', 42)))
 
87
    >>> print _format_version_tuple((2, 1, 0, 'final', 42))
81
88
    2.1.0.42
82
 
    >>> print(_format_version_tuple((1, 4, 0, 'wibble', 0)))
 
89
    >>> print _format_version_tuple((1, 4, 0, 'wibble', 0))
83
90
    1.4.0.wibble.0
84
91
    """
85
92
    if len(version_info) == 2:
112
119
    return main_version + sub_string
113
120
 
114
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, '_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()
 
132
 
 
133
 
115
134
__version__ = _format_version_tuple(version_info)
116
135
version_string = __version__
117
136
 
118
137
 
119
138
def _patch_filesystem_default_encoding(new_enc):
120
139
    """Change the Python process global encoding for filesystem names
121
 
 
 
140
    
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.
124
143
 
130
149
    try:
131
150
        import ctypes
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)
143
 
    has_enc.value = 1
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")
147
159
    return new_enc
148
160
 
149
161
 
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"
159
171
else:
160
172
    _fs_enc = codecs.lookup(_fs_enc).name
161
173
 
162
174
 
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.
175
 
_global_state = None
 
187
global_state = None
176
188
 
177
189
 
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.
180
192
 
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.  
183
195
 
184
196
    More options may be added in future so callers should use named arguments.
185
197
 
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.
191
203
 
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
194
206
        the caller.
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.
201
213
    """
202
 
    from breezy import library_state, trace
 
214
    from brzlib import library_state, trace
203
215
    if setup_ui:
204
 
        import breezy.ui
 
216
        import brzlib.ui
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)
209
221
    else:
210
222
        ui_factory = None
211
223
    tracer = trace.DefaultConfig()
215
227
    return state
216
228
 
217
229
 
218
 
def get_global_state():
219
 
    if _global_state is None:
220
 
        return initialize()
221
 
    return _global_state
222
 
 
223
 
 
224
230
def test_suite():
225
231
    import tests
226
232
    return tests.test_suite()