293
251
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)
363
254
def load_from_dir(d):
364
255
"""Load the plugins in directory d.
366
257
d must be in the plugins module path already.
367
This function is called once for each directory in the module path.
259
# Get the list of valid python suffixes for __init__.py?
260
# this includes .py, .pyc, and .pyo (depending on if we are running -O)
261
# but it doesn't include compiled modules (.so, .dll, etc)
262
valid_suffixes = [suffix for suffix, mod_type, flags in imp.get_suffixes()
263
if flags in (imp.PY_SOURCE, imp.PY_COMPILED)]
264
package_entries = ['__init__'+suffix for suffix in valid_suffixes]
369
265
plugin_names = set()
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)
266
for f in os.listdir(d):
267
path = osutils.pathjoin(d, f)
268
if os.path.isdir(path):
269
for entry in package_entries:
270
# This directory should be a package, and thus added to
272
if os.path.isfile(osutils.pathjoin(path, entry)):
274
else: # This directory is not a package
277
for suffix_info in imp.get_suffixes():
278
if f.endswith(suffix_info[0]):
279
f = f[:-len(suffix_info[0])]
280
if suffix_info[2] == imp.C_EXTENSION and f.endswith('module'):
281
f = f[:-len('module')]
385
plugin_names.add(name)
286
continue # We don't load __init__.py again in the plugin dir
287
elif getattr(_mod_plugins, f, None):
288
trace.mutter('Plugin name %s already loaded', f)
290
# trace.mutter('add plugin name %s', f)
387
293
for name in plugin_names:
388
_load_plugin_module(name, d)
295
exec "import bzrlib.plugins.%s" % name in {}
296
except KeyboardInterrupt:
298
except errors.IncompatibleAPI, e:
299
trace.warning("Unable to load plugin %r. It requested API version "
300
"%s of module %s but the minimum exported version is %s, and "
301
"the maximum is %s" %
302
(name, e.wanted, e.api, e.minimum, e.current))
304
trace.warning("%s" % e)
305
## import pdb; pdb.set_trace()
306
if re.search('\.|-| ', name):
307
sanitised_name = re.sub('[-. ]', '_', name)
308
if sanitised_name.startswith('bzr_'):
309
sanitised_name = sanitised_name[len('bzr_'):]
310
trace.warning("Unable to load %r in %r as a plugin because the "
311
"file path isn't a valid module name; try renaming "
312
"it to %r." % (name, d, sanitised_name))
314
trace.warning('Unable to load plugin %r from %r' % (name, d))
315
trace.log_exception_quietly()
316
if 'error' in debug.debug_flags:
317
trace.print_exception(sys.exc_info(), sys.stderr)
548
477
return version_string
550
479
__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)