28
28
BZR_PLUGIN_PATH is also honoured for any plugins imported via
29
'import bzrlib.plugins.PLUGINNAME', as long as set_plugins_path has been
29
'import bzrlib.plugins.PLUGINNAME', as long as set_plugins_path has been
36
from bzrlib import osutils
38
36
from bzrlib.lazy_import import lazy_import
40
37
lazy_import(globals(), """
45
43
from bzrlib import (
46
_format_version_tuple,
52
49
from bzrlib import plugins as _mod_plugins
55
from bzrlib.symbol_versioning import deprecated_function
52
from bzrlib.symbol_versioning import deprecated_function, one_three
53
from bzrlib.trace import mutter, warning, log_exception_quietly
58
56
DEFAULT_PLUGIN_PATH = None
72
70
Future calls to load_plugins() will be ignored.
72
# TODO: jam 20060131 This should probably also disable
77
78
def _strip_trailing_sep(path):
78
79
return path.rstrip("\\/")
81
def set_plugins_path(path=None):
82
"""Set the path for plugins to be loaded from.
84
:param path: The list of paths to search for plugins. By default,
85
path will be determined using get_standard_plugins_path.
86
if path is [], no plugins can be loaded.
89
path = get_standard_plugins_path()
90
_mod_plugins.__path__ = path
94
def get_standard_plugins_path():
95
"""Determine a plugin path suitable for general use."""
82
def set_plugins_path():
83
"""Set the path for plugins to be loaded from."""
96
84
path = os.environ.get('BZR_PLUGIN_PATH',
97
85
get_default_plugin_path()).split(os.pathsep)
98
# Get rid of trailing slashes, since Python can't handle them when
99
# it tries to import modules.
100
path = map(_strip_trailing_sep, path)
101
86
bzr_exe = bool(getattr(sys, 'frozen', None))
102
87
if bzr_exe: # expand path for bzr.exe
103
88
# We need to use relative path to system-wide plugin
222
continue # We don't load __init__.py again in the plugin dir
223
elif getattr(_mod_plugins, f, None):
224
trace.mutter('Plugin name %s already loaded', f)
203
if getattr(_mod_plugins, f, None):
204
mutter('Plugin name %s already loaded', f)
226
# trace.mutter('add plugin name %s', f)
206
# mutter('add plugin name %s', f)
227
207
plugin_names.add(f)
229
209
for name in plugin_names:
231
211
exec "import bzrlib.plugins.%s" % name in {}
232
212
except KeyboardInterrupt:
234
except errors.IncompatibleAPI, e:
235
trace.warning("Unable to load plugin %r. It requested API version "
236
"%s of module %s but the minimum exported version is %s, and "
237
"the maximum is %s" %
238
(name, e.wanted, e.api, e.minimum, e.current))
239
214
except Exception, e:
240
trace.warning("%s" % e)
241
215
## import pdb; pdb.set_trace()
242
216
if re.search('\.|-| ', name):
243
217
sanitised_name = re.sub('[-. ]', '_', name)
244
218
if sanitised_name.startswith('bzr_'):
245
219
sanitised_name = sanitised_name[len('bzr_'):]
246
trace.warning("Unable to load %r in %r as a plugin because the "
220
warning("Unable to load %r in %r as a plugin because the "
247
221
"file path isn't a valid module name; try renaming "
248
222
"it to %r." % (name, d, sanitised_name))
250
trace.warning('Unable to load plugin %r from %r' % (name, d))
251
trace.log_exception_quietly()
224
warning('Unable to load plugin %r from %r' % (name, d))
225
log_exception_quietly()
226
if 'error' in debug.debug_flags:
227
trace.print_exception(sys.exc_info(), sys.stderr)
230
@deprecated_function(one_three)
231
def load_from_zip(zip_name):
232
"""Load all the plugins in a zip."""
233
valid_suffixes = ('.py', '.pyc', '.pyo') # only python modules/packages
236
index = zip_name.rindex('.zip')
239
archive = zip_name[:index+4]
240
prefix = zip_name[index+5:]
242
mutter('Looking for plugins in %r', zip_name)
244
# use zipfile to get list of files/dirs inside zip
246
z = zipfile.ZipFile(archive)
247
namelist = z.namelist()
249
except zipfile.error:
254
prefix = prefix.replace('\\','/')
255
if prefix[-1] != '/':
258
namelist = [name[ix:]
260
if name.startswith(prefix)]
262
mutter('Names in archive: %r', namelist)
264
for name in namelist:
265
if not name or name.endswith('/'):
268
# '/' is used to separate pathname components inside zip archives
271
head, tail = '', name
273
head, tail = name.rsplit('/',1)
275
# we don't need looking in subdirectories
278
base, suffix = osutils.splitext(tail)
279
if suffix not in valid_suffixes:
282
if base == '__init__':
293
if getattr(_mod_plugins, plugin_name, None):
294
mutter('Plugin name %s already loaded', plugin_name)
298
exec "import bzrlib.plugins.%s" % plugin_name in {}
299
mutter('Load plugin %s from zip %r', plugin_name, zip_name)
300
except KeyboardInterrupt:
303
## import pdb; pdb.set_trace()
304
warning('Unable to load plugin %r from %r'
306
log_exception_quietly()
252
307
if 'error' in debug.debug_flags:
253
308
trace.print_exception(sys.exc_info(), sys.stderr)
257
312
"""Return a dictionary of the plugins.
259
314
Each item in the dictionary is a PlugIn object.
387
442
def version_info(self):
388
443
"""Return the plugin's version_tuple or None if unknown."""
389
444
version_info = getattr(self.module, 'version_info', None)
390
if version_info is not None:
392
if isinstance(version_info, types.StringType):
393
version_info = version_info.split('.')
394
elif len(version_info) == 3:
395
version_info = tuple(version_info) + ('final', 0)
397
# The given version_info isn't even iteratible
398
trace.log_exception_quietly()
399
version_info = (version_info,)
445
if version_info is not None and len(version_info) == 3:
446
version_info = tuple(version_info) + ('final', 0)
400
447
return version_info
402
449
def _get__version__(self):
403
450
version_info = self.version_info()
404
if version_info is None or len(version_info) == 0:
451
if version_info is None:
407
version_string = _format_version_tuple(version_info)
408
except (ValueError, TypeError, IndexError), e:
409
trace.log_exception_quietly()
410
# try to return something usefull for bad plugins, in stead of
412
version_string = '.'.join(map(str, version_info))
453
if version_info[3] == 'final':
454
version_string = '%d.%d.%d' % version_info[:3]
456
version_string = '%d.%d.%d%s%d' % version_info
413
457
return version_string
415
459
__version__ = property(_get__version__)