259
293
load_from_dirs = load_from_path
296
def _find_plugin_module(dir, name):
297
"""Check if there is a valid python module that can be loaded as a plugin.
299
:param dir: The directory where the search is performed.
300
:param path: An existing file path, either a python file or a package
303
:return: (name, path, description) name is the module name, path is the
304
file to load and description is the tuple returned by
307
path = osutils.pathjoin(dir, name)
308
if os.path.isdir(path):
309
# Check for a valid __init__.py file, valid suffixes depends on -O and
310
# can be .py, .pyc and .pyo
311
for suffix, mode, kind in imp.get_suffixes():
312
if kind not in (imp.PY_SOURCE, imp.PY_COMPILED):
313
# We don't recognize compiled modules (.so, .dll, etc)
315
init_path = osutils.pathjoin(path, '__init__' + suffix)
316
if os.path.isfile(init_path):
317
return name, init_path, (suffix, mode, kind)
319
for suffix, mode, kind in imp.get_suffixes():
320
if name.endswith(suffix):
321
# Clean up the module name
322
name = name[:-len(suffix)]
323
if kind == imp.C_EXTENSION and name.endswith('module'):
324
name = name[:-len('module')]
325
return name, path, (suffix, mode, kind)
326
# There is no python module here
327
return None, None, (None, None, None)
330
def _load_plugin_module(name, dir):
331
"""Load plugin name from dir.
333
:param name: The plugin name in the bzrlib.plugins namespace.
334
:param dir: The directory the plugin is loaded from for error messages.
336
if ('bzrlib.plugins.%s' % name) in PluginImporter.blacklist:
339
exec "import bzrlib.plugins.%s" % name in {}
340
except KeyboardInterrupt:
342
except errors.IncompatibleAPI, e:
343
trace.warning("Unable to load plugin %r. It requested API version "
344
"%s of module %s but the minimum exported version is %s, and "
345
"the maximum is %s" %
346
(name, e.wanted, e.api, e.minimum, e.current))
348
trace.warning("%s" % e)
349
if re.search('\.|-| ', name):
350
sanitised_name = re.sub('[-. ]', '_', name)
351
if sanitised_name.startswith('bzr_'):
352
sanitised_name = sanitised_name[len('bzr_'):]
353
trace.warning("Unable to load %r in %r as a plugin because the "
354
"file path isn't a valid module name; try renaming "
355
"it to %r." % (name, dir, sanitised_name))
357
trace.warning('Unable to load plugin %r from %r' % (name, dir))
358
trace.log_exception_quietly()
359
if 'error' in debug.debug_flags:
360
trace.print_exception(sys.exc_info(), sys.stderr)
262
363
def load_from_dir(d):
263
364
"""Load the plugins in directory d.
265
366
d must be in the plugins module path already.
367
This function is called once for each directory in the module path.
267
# Get the list of valid python suffixes for __init__.py?
268
# this includes .py, .pyc, and .pyo (depending on if we are running -O)
269
# but it doesn't include compiled modules (.so, .dll, etc)
270
valid_suffixes = [suffix for suffix, mod_type, flags in imp.get_suffixes()
271
if flags in (imp.PY_SOURCE, imp.PY_COMPILED)]
272
package_entries = ['__init__'+suffix for suffix in valid_suffixes]
273
369
plugin_names = set()
274
for f in os.listdir(d):
275
path = osutils.pathjoin(d, f)
276
if os.path.isdir(path):
277
for entry in package_entries:
278
# This directory should be a package, and thus added to
280
if os.path.isfile(osutils.pathjoin(path, entry)):
282
else: # This directory is not a package
285
for suffix_info in imp.get_suffixes():
286
if f.endswith(suffix_info[0]):
287
f = f[:-len(suffix_info[0])]
288
if suffix_info[2] == imp.C_EXTENSION and f.endswith('module'):
289
f = f[:-len('module')]
370
for p in os.listdir(d):
371
name, path, desc = _find_plugin_module(d, p)
373
if name == '__init__':
374
# We do nothing with the __init__.py file in directories from
375
# the bzrlib.plugins module path, we may want to, one day
377
continue # We don't load __init__.py in the plugins dirs
378
elif getattr(_mod_plugins, name, None) is not None:
379
# The module has already been loaded from another directory
380
# during a previous call.
381
# FIXME: There should be a better way to report masked plugins
383
trace.mutter('Plugin name %s already loaded', name)
294
continue # We don't load __init__.py again in the plugin dir
295
elif getattr(_mod_plugins, f, None):
296
trace.mutter('Plugin name %s already loaded', f)
298
# trace.mutter('add plugin name %s', f)
385
plugin_names.add(name)
301
387
for name in plugin_names:
303
exec "import bzrlib.plugins.%s" % name in {}
304
except KeyboardInterrupt:
306
except errors.IncompatibleAPI, e:
307
trace.warning("Unable to load plugin %r. It requested API version "
308
"%s of module %s but the minimum exported version is %s, and "
309
"the maximum is %s" %
310
(name, e.wanted, e.api, e.minimum, e.current))
312
trace.warning("%s" % e)
313
## import pdb; pdb.set_trace()
314
if re.search('\.|-| ', name):
315
sanitised_name = re.sub('[-. ]', '_', name)
316
if sanitised_name.startswith('bzr_'):
317
sanitised_name = sanitised_name[len('bzr_'):]
318
trace.warning("Unable to load %r in %r as a plugin because the "
319
"file path isn't a valid module name; try renaming "
320
"it to %r." % (name, d, sanitised_name))
322
trace.warning('Unable to load plugin %r from %r' % (name, d))
323
trace.log_exception_quietly()
324
if 'error' in debug.debug_flags:
325
trace.print_exception(sys.exc_info(), sys.stderr)
388
_load_plugin_module(name, d)
485
548
return version_string
487
550
__version__ = property(_get__version__)
553
class _PluginImporter(object):
554
"""An importer tailored to bzr specific needs.
556
This is a singleton that takes care of:
557
- disabled plugins specified in 'blacklist',
558
- plugins that needs to be loaded from specific directories.
565
self.blacklist = set()
566
self.specific_paths = {}
568
def find_module(self, fullname, parent_path=None):
569
"""Search a plugin module.
571
Disabled plugins raise an import error, plugins with specific paths
572
returns a specific loader.
574
:return: None if the plugin doesn't need special handling, self
577
if not fullname.startswith('bzrlib.plugins.'):
579
if fullname in self.blacklist:
580
raise ImportError('%s is disabled' % fullname)
581
if fullname in self.specific_paths:
585
def load_module(self, fullname):
586
"""Load a plugin from a specific directory."""
587
# We are called only for specific paths
588
plugin_path = self.specific_paths[fullname]
590
if os.path.isdir(plugin_path):
591
for suffix, mode, kind in imp.get_suffixes():
592
if kind not in (imp.PY_SOURCE, imp.PY_COMPILED):
593
# We don't recognize compiled modules (.so, .dll, etc)
595
init_path = osutils.pathjoin(plugin_path, '__init__' + suffix)
596
if os.path.isfile(init_path):
597
# We've got a module here and load_module needs specific
599
loading_path = plugin_path
602
kind = imp.PKG_DIRECTORY
605
for suffix, mode, kind in imp.get_suffixes():
606
if plugin_path.endswith(suffix):
607
loading_path = plugin_path
609
if loading_path is None:
610
raise ImportError('%s cannot be loaded from %s'
611
% (fullname, plugin_path))
612
if kind is imp.PKG_DIRECTORY:
615
f = open(loading_path, mode)
617
mod = imp.load_module(fullname, f, loading_path,
618
(suffix, mode, kind))
619
mod.__package__ = fullname
626
# Install a dedicated importer for plugins requiring special handling
627
PluginImporter = _PluginImporter()
628
sys.meta_path.append(PluginImporter)