20
20
When load_plugins() is invoked, any python module in any directory in
21
21
$BZR_PLUGIN_PATH will be imported. The module will be imported as
22
22
'bzrlib.plugins.$BASENAME(PLUGIN)'. In the plugin's main body, it should
23
update any bzrlib registries it wants to extend.
25
See the plugin-api developer documentation for information about writing
23
update any bzrlib registries it wants to extend; for example, to add new
24
commands, import bzrlib.commands and add your new command to the plugin_cmds
28
27
BZR_PLUGIN_PATH is also honoured for any plugins imported via
29
28
'import bzrlib.plugins.PLUGINNAME', as long as set_plugins_path has been
43
42
from bzrlib import (
49
46
from bzrlib import plugins as _mod_plugins
52
from bzrlib.symbol_versioning import deprecated_function, one_three
49
from bzrlib.symbol_versioning import deprecated_function, zero_ninetyone
53
50
from bzrlib.trace import mutter, warning, log_exception_quietly
64
61
return DEFAULT_PLUGIN_PATH
64
@deprecated_function(zero_ninetyone)
66
"""Return a dictionary of the plugins."""
67
return dict((name, plugin.module) for name, plugin in plugins().items())
67
70
def disable_plugins():
68
71
"""Disable loading plugins.
78
80
def _strip_trailing_sep(path):
79
81
return path.rstrip("\\/")
82
83
def set_plugins_path():
83
84
"""Set the path for plugins to be loaded from."""
84
85
path = os.environ.get('BZR_PLUGIN_PATH',
85
86
get_default_plugin_path()).split(os.pathsep)
86
bzr_exe = bool(getattr(sys, 'frozen', None))
87
if bzr_exe: # expand path for bzr.exe
88
# We need to use relative path to system-wide plugin
89
# directory because bzrlib from standalone bzr.exe
90
# could be imported by another standalone program
91
# (e.g. bzr-config; or TortoiseBzr/Olive if/when they
92
# will become standalone exe). [bialix 20071123]
93
# __file__ typically is
94
# C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc
95
# then plugins directory is
96
# C:\Program Files\Bazaar\plugins
97
# so relative path is ../../../plugins
98
path.append(osutils.abspath(osutils.pathjoin(
99
osutils.dirname(__file__), '../../../plugins')))
100
87
# Get rid of trailing slashes, since Python can't handle them when
101
88
# it tries to import modules.
102
89
path = map(_strip_trailing_sep, path)
103
if not bzr_exe: # don't look inside library.zip
104
# search the plugin path before the bzrlib installed dir
105
path.append(os.path.dirname(_mod_plugins.__file__))
106
# search the arch independent path if we can determine that and
107
# the plugin is found nowhere else
108
if sys.platform != 'win32':
110
from distutils.sysconfig import get_python_lib
112
# If distutuils is not available, we just won't add that path
115
archless_path = osutils.pathjoin(get_python_lib(), 'bzrlib',
117
if archless_path not in path:
118
path.append(archless_path)
90
# search the plugin path before the bzrlib installed dir
91
path.append(os.path.dirname(_mod_plugins.__file__))
119
92
_mod_plugins.__path__ = path
214
191
except Exception, e:
215
192
## import pdb; pdb.set_trace()
216
193
if re.search('\.|-| ', name):
217
sanitised_name = re.sub('[-. ]', '_', name)
218
if sanitised_name.startswith('bzr_'):
219
sanitised_name = sanitised_name[len('bzr_'):]
220
warning("Unable to load %r in %r as a plugin because the "
221
"file path isn't a valid module name; try renaming "
222
"it to %r." % (name, d, sanitised_name))
194
warning('Unable to load plugin %r from %r: '
195
'It is not a valid python module name.' % (name, d))
224
197
warning('Unable to load plugin %r from %r' % (name, d))
225
198
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
201
def load_from_zip(zip_name):
232
202
"""Load all the plugins in a zip."""
233
203
valid_suffixes = ('.py', '.pyc', '.pyo') # only python modules/packages
236
207
index = zip_name.rindex('.zip')
237
208
except ValueError:
404
373
if getattr(self.module, '__path__', None) is not None:
405
374
return os.path.abspath(self.module.__path__[0])
406
375
elif getattr(self.module, '__file__', None) is not None:
407
path = os.path.abspath(self.module.__file__)
408
if path[-4:] in ('.pyc', '.pyo'):
409
pypath = path[:-4] + '.py'
410
if os.path.isfile(pypath):
376
return os.path.abspath(self.module.__file__)
414
378
return repr(self.module)
430
def load_plugin_tests(self, loader):
431
"""Return the adapted plugin's test suite.
433
:param loader: The custom loader that should be used to load additional
437
if getattr(self.module, 'load_tests', None) is not None:
438
return loader.loadTestsFromModule(self.module)
442
394
def version_info(self):
443
395
"""Return the plugin's version_tuple or None if unknown."""
444
396
version_info = getattr(self.module, 'version_info', None)
445
397
if version_info is not None and len(version_info) == 3:
446
398
version_info = tuple(version_info) + ('final', 0)
447
399
return version_info
449
401
def _get__version__(self):
450
402
version_info = self.version_info()
451
403
if version_info is None: