51
52
from bzrlib import plugins as _mod_plugins
55
from bzrlib.symbol_versioning import (
55
61
DEFAULT_PLUGIN_PATH = None
57
63
_plugins_disabled = False
61
# Map from plugin name, to list of string warnings about eg plugin
65
66
def are_plugins_disabled():
66
67
return _plugins_disabled
79
def describe_plugins(show_paths=False):
80
"""Generate text description of plugins.
82
Includes both those that have loaded, and those that failed to
85
:param show_paths: If true,
86
:returns: Iterator of text lines (including newlines.)
88
from inspect import getdoc
89
loaded_plugins = plugins()
90
all_names = sorted(list(set(
91
loaded_plugins.keys() + plugin_warnings.keys())))
92
for name in all_names:
93
if name in loaded_plugins:
94
plugin = loaded_plugins[name]
95
version = plugin.__version__
96
if version == 'unknown':
98
yield '%s %s\n' % (name, version)
99
d = getdoc(plugin.module)
101
doc = d.split('\n')[0]
103
doc = '(no description)'
104
yield (" %s\n" % doc)
106
yield (" %s\n" % plugin.path())
109
yield "%s (failed to load)\n" % name
110
if name in plugin_warnings:
111
for line in plugin_warnings[name]:
112
yield " ** " + line + '\n'
116
80
def _strip_trailing_sep(path):
117
81
return path.rstrip("\\/")
120
def _get_specific_plugin_paths(paths):
121
"""Returns the plugin paths from a string describing the associations.
123
:param paths: A string describing the paths associated with the plugins.
125
:returns: A list of (plugin name, path) tuples.
127
For example, if paths is my_plugin@/test/my-test:her_plugin@/production/her,
128
[('my_plugin', '/test/my-test'), ('her_plugin', '/production/her')]
131
Note that ':' in the example above depends on the os.
136
for spec in paths.split(os.pathsep):
138
name, path = spec.split('@')
140
raise errors.BzrCommandError(
141
'"%s" is not a valid <plugin_name>@<plugin_path> description '
143
specs.append((name, path))
147
84
def set_plugins_path(path=None):
148
85
"""Set the path for plugins to be loaded from.
161
98
for name in disabled_plugins.split(os.pathsep):
162
99
PluginImporter.blacklist.add('bzrlib.plugins.' + name)
163
100
# Set up a the specific paths for plugins
164
for plugin_name, plugin_path in _get_specific_plugin_paths(os.environ.get(
165
'BZR_PLUGINS_AT', None)):
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('@')
166
105
PluginImporter.specific_paths[
167
106
'bzrlib.plugins.%s' % plugin_name] = plugin_path
363
302
return None, None, (None, None, None)
366
def record_plugin_warning(plugin_name, warning_message):
367
trace.mutter(warning_message)
368
plugin_warnings.setdefault(plugin_name, []).append(warning_message)
371
305
def _load_plugin_module(name, dir):
372
306
"""Load plugin name from dir.
381
315
except KeyboardInterrupt:
383
317
except errors.IncompatibleAPI, e:
385
"Unable to load plugin %r. It requested API version "
318
trace.warning("Unable to load plugin %r. It requested API version "
386
319
"%s of module %s but the minimum exported version is %s, and "
387
320
"the maximum is %s" %
388
321
(name, e.wanted, e.api, e.minimum, e.current))
389
record_plugin_warning(name, warning_message)
390
322
except Exception, e:
391
323
trace.warning("%s" % e)
392
324
if re.search('\.|-| ', name):
397
329
"file path isn't a valid module name; try renaming "
398
330
"it to %r." % (name, dir, sanitised_name))
400
record_plugin_warning(
402
'Unable to load plugin %r from %r' % (name, dir))
332
trace.warning('Unable to load plugin %r from %r' % (name, dir))
403
333
trace.log_exception_quietly()
404
334
if 'error' in debug.debug_flags:
405
335
trace.print_exception(sys.exc_info(), sys.stderr)
448
def format_concise_plugin_list():
449
"""Return a string holding a concise list of plugins and their version.
452
for name, a_plugin in sorted(plugins().items()):
453
items.append("%s[%s]" %
454
(name, a_plugin.__version__))
455
return ', '.join(items)
459
378
class PluginsHelpIndex(object):
460
379
"""A help index that returns help topics for plugins."""
643
562
# We are called only for specific paths
644
563
plugin_path = self.specific_paths[fullname]
645
564
loading_path = None
646
566
if os.path.isdir(plugin_path):
647
567
for suffix, mode, kind in imp.get_suffixes():
648
568
if kind not in (imp.PY_SOURCE, imp.PY_COMPILED):
651
571
init_path = osutils.pathjoin(plugin_path, '__init__' + suffix)
652
572
if os.path.isfile(init_path):
653
# We've got a module here and load_module needs specific
655
loading_path = plugin_path
658
kind = imp.PKG_DIRECTORY
573
loading_path = init_path
661
577
for suffix, mode, kind in imp.get_suffixes():
665
581
if loading_path is None:
666
582
raise ImportError('%s cannot be loaded from %s'
667
583
% (fullname, plugin_path))
668
if kind is imp.PKG_DIRECTORY:
671
f = open(loading_path, mode)
584
f = open(loading_path, mode)
673
586
mod = imp.load_module(fullname, f, loading_path,
674
587
(suffix, mode, kind))
589
# The plugin can contain modules, so be ready
590
mod.__path__ = [plugin_path]
675
591
mod.__package__ = fullname
682
597
# Install a dedicated importer for plugins requiring special handling