81
81
return path.rstrip("\\/")
84
def _get_specific_plugin_paths(paths):
85
"""Returns the plugin paths from a string describing the associations.
87
:param paths: A string describing the paths associated with the plugins.
89
:returns: A list of (plugin name, path) tuples.
91
For example, if paths is my_plugin@/test/my-test:her_plugin@/production/her,
92
[('my_plugin', '/test/my-test'), ('her_plugin', '/production/her')]
95
Note that ':' in the example above depends on the os.
100
for spec in paths.split(os.pathsep):
102
name, path = spec.split('@')
104
raise errors.BzrCommandError(
105
'"%s" is not a valid <plugin_name>@<plugin_path> description '
107
specs.append((name, path))
84
111
def set_plugins_path(path=None):
85
112
"""Set the path for plugins to be loaded from.
98
125
for name in disabled_plugins.split(os.pathsep):
99
126
PluginImporter.blacklist.add('bzrlib.plugins.' + name)
100
127
# Set up a the specific paths for plugins
101
specific_plugins = os.environ.get('BZR_PLUGINS_AT', None)
102
if specific_plugins is not None:
103
for spec in specific_plugins.split(os.pathsep):
104
plugin_name, plugin_path = spec.split('@')
128
for plugin_name, plugin_path in _get_specific_plugin_paths(os.environ.get(
129
'BZR_PLUGINS_AT', None)):
105
130
PluginImporter.specific_paths[
106
131
'bzrlib.plugins.%s' % plugin_name] = plugin_path
560
585
def load_module(self, fullname):
561
586
"""Load a plugin from a specific directory."""
562
587
# We are called only for specific paths
563
plugin_dir = self.specific_paths[fullname]
565
maybe_package = False
566
for p in os.listdir(plugin_dir):
567
if os.path.isdir(osutils.pathjoin(plugin_dir, p)):
568
# We're searching for files only and don't want submodules to
569
# be recognized as plugins (they are submodules inside the
573
suffix, mode, kind) = _find_plugin_module(plugin_dir, p)
575
candidate = (name, path, suffix, mode, kind)
576
if kind == imp.PY_SOURCE:
577
# We favour imp.PY_SOURCE (which will use the compiled
578
# version if available) over imp.PY_COMPILED (which is used
579
# only if the source is not available)
581
if candidate is None:
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:
582
610
raise ImportError('%s cannot be loaded from %s'
583
% (fullname, plugin_dir))
611
% (fullname, plugin_path))
612
if kind is imp.PKG_DIRECTORY:
615
f = open(loading_path, mode)
586
mod = imp.load_module(fullname, f, path, (suffix, mode, kind))
587
# The plugin can contain modules, so be ready
588
mod.__path__ = [plugin_dir]
617
mod = imp.load_module(fullname, f, loading_path,
618
(suffix, mode, kind))
589
619
mod.__package__ = fullname
595
626
# Install a dedicated importer for plugins requiring special handling