1
# Copyright (C) 2005-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
Developer documentation is available at
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 bzrlib are:
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
31
We hope you enjoy this library.
36
# Keep track of when bzrlib was first imported, so that we can give rough
37
# timestamps relative to program start in the log file kept by bzrlib.trace.
38
_start_time = time.time()
41
if getattr(sys, '_bzr_lazy_regex', False):
42
# The 'bzr' executable sets _bzr_lazy_regex. We install the lazy regex
43
# hack as soon as possible so that as much of the standard library can
44
# benefit, including the 'string' module.
45
del sys._bzr_lazy_regex
46
import bzrlib.lazy_regex
47
bzrlib.lazy_regex.install_lazy_compile()
50
IGNORE_FILENAME = ".bzrignore"
53
__copyright__ = "Copyright 2005-2010 Canonical Ltd."
55
# same format as sys.version_info: "A tuple containing the five components of
56
# the version number: major, minor, micro, releaselevel, and serial. All
57
# values except releaselevel are integers; the release level is 'alpha',
58
# 'beta', 'candidate', or 'final'. The version_info value corresponding to the
59
# Python version 2.0 is (2, 0, 0, 'final', 0)." Additionally we use a
60
# releaselevel of 'dev' for unreleased under-development code.
62
version_info = (2, 2, 0, 'beta', 3)
64
# API compatibility version
65
api_minimum_version = (2, 2, 0)
68
def _format_version_tuple(version_info):
69
"""Turn a version number 2, 3 or 5-tuple into a short string.
71
This format matches <http://docs.python.org/dist/meta-data.html>
72
and the typical presentation used in Python output.
74
This also checks that the version is reasonable: the sub-release must be
75
zero for final releases.
77
>>> print _format_version_tuple((1, 0, 0, 'final', 0))
79
>>> print _format_version_tuple((1, 2, 0, 'dev', 0))
81
>>> print bzrlib._format_version_tuple((1, 2, 0, 'dev', 1))
83
>>> print _format_version_tuple((1, 1, 1, 'candidate', 2))
85
>>> print bzrlib._format_version_tuple((2, 1, 0, 'beta', 1))
87
>>> print _format_version_tuple((1, 4, 0))
89
>>> print _format_version_tuple((1, 4))
91
>>> print bzrlib._format_version_tuple((2, 1, 0, 'final', 1))
92
Traceback (most recent call last):
94
ValueError: version_info (2, 1, 0, 'final', 1) not valid
95
>>> print _format_version_tuple((1, 4, 0, 'wibble', 0))
96
Traceback (most recent call last):
98
ValueError: version_info (1, 4, 0, 'wibble', 0) not valid
100
if len(version_info) == 2:
101
main_version = '%d.%d' % version_info[:2]
103
main_version = '%d.%d.%d' % version_info[:3]
104
if len(version_info) <= 3:
107
release_type = version_info[3]
108
sub = version_info[4]
110
# check they're consistent
111
if release_type == 'final' and sub == 0:
113
elif release_type == 'dev' and sub == 0:
115
elif release_type == 'dev':
116
sub_string = 'dev' + str(sub)
117
elif release_type in ('alpha', 'beta'):
118
if version_info[2] == 0:
119
main_version = '%d.%d' % version_info[:2]
120
sub_string = release_type[0] + str(sub)
121
elif release_type == 'candidate':
122
sub_string = 'rc' + str(sub)
124
raise ValueError("version_info %r not valid" % (version_info,))
126
return main_version + sub_string
129
__version__ = _format_version_tuple(version_info)
130
version_string = __version__
132
# bzr has various bits of global state that are slowly being eliminated.
133
# This variable is intended to permit any new state-like things to be attached
134
# to a BzrLibraryState object rather than getting new global variables that
135
# need to be hunted down. Accessing the current BzrLibraryState through this
136
# variable is not encouraged: it is better to pass it around as part of the
137
# context of an operation than to look it up directly, but when that is too
138
# hard, it is better to use this variable than to make a branch new global
140
# If using this variable my looking it up (because it can't be easily obtained)
141
# it is important to store the reference you get, rather than looking it up
142
# repeatedly; that way your code will behave properly in the bzrlib test suite
143
# and from programs that do use multiple library contexts.
147
class BzrLibraryState(object):
148
"""The state about how bzrlib has been configured.
150
:ivar saved_state: The bzrlib.global_state at the time __enter__ was
152
:ivar cleanups: An ObjectWithCleanups which can be used for cleanups that
153
should occur when the use of bzrlib is completed. This is initialised
154
in __enter__ and executed in __exit__.
157
def __init__(self, setup_ui=True, stdin=None, stdout=None, stderr=None):
158
"""Create library start for normal use of bzrlib.
160
Most applications that embed bzrlib, including bzr itself, should just
161
call bzrlib.initialize(), but it is possible to use the state class
164
More options may be added in future so callers should use named
167
BzrLibraryState implements the Python 2.5 Context Manager protocol, and
168
can be used with the with statement. Upon __enter__ the global
169
variables in use by bzr are set, and they are cleared on __exit__.
171
:param setup_ui: If true (default) use a terminal UI; otherwise
172
some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
174
:param stdin, stdout, stderr: If provided, use these for terminal IO;
175
otherwise use the files in `sys`.
177
self.setup_ui = setup_ui
183
# NB: This function tweaks so much global state it's hard to test it in
184
# isolation within the same interpreter. It's not reached on normal
185
# in-process run_bzr calls. If it's broken, we expect that
186
# TestRunBzrSubprocess may fail.
187
if version_info[3] == 'final':
188
from bzrlib.symbol_versioning import suppress_deprecation_warnings
189
suppress_deprecation_warnings(override=True)
191
import bzrlib.cleanup
193
self.cleanups = bzrlib.cleanup.ObjectWithCleanups()
194
bzrlib.trace.enable_default_logging()
198
stdin = self.stdin or sys.stdin
199
stdout = self.stdout or sys.stdout
200
stderr = self.stderr or sys.stderr
201
bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
202
stdin, stdout, stderr)
204
self.saved_state = global_state
207
def __exit__(self, exc_type, exc_val, exc_tb):
208
self.cleanups.cleanup_now()
210
bzrlib.trace._flush_stdout_stderr()
211
bzrlib.trace._flush_trace()
212
import bzrlib.osutils
213
bzrlib.osutils.report_extension_load_failures()
214
bzrlib.ui.ui_factory.__exit__(None, None, None)
215
bzrlib.ui.ui_factory = None
217
global_state = self.saved_state
218
return False # propogate exceptions.
221
def initialize(setup_ui=True, stdin=None, stdout=None, stderr=None):
222
"""Set up everything needed for normal use of bzrlib.
224
Most applications that embed bzrlib, including bzr itself, should call
225
this function to initialize various subsystems.
227
More options may be added in future so callers should use named arguments.
229
:param setup_ui: If true (default) use a terminal UI; otherwise
230
some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
232
:param stdin, stdout, stderr: If provided, use these for terminal IO;
233
otherwise use the files in `sys`.
234
:return: A context manager for the use of bzrlib. The __enter__ method of
235
this context needs to be alled before it takes effect, and the __exit__
236
should be called by the caller before exiting their process or
237
otherwise stopping use of bzrlib. Advanced callers can use
238
BzrLibraryState directly.
240
return BzrLibraryState(setup_ui=setup_ui, stdin=stdin,
241
stdout=stdout, stderr=stderr)
246
return tests.test_suite()