1
# Copyright (C) 2004, 2005, 2007 Canonical Ltd
1
# Copyright (C) 2004, 2005, 2007, 2008 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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; for example, to add new
24
commands, import bzrlib.commands and add your new command to the plugin_cmds
23
update any bzrlib registries it wants to extend.
25
See the plugin-api developer documentation for information about writing
27
28
BZR_PLUGIN_PATH is also honoured for any plugins imported via
28
29
'import bzrlib.plugins.PLUGINNAME', as long as set_plugins_path has been
42
46
from bzrlib import (
48
52
from bzrlib import plugins as _mod_plugins
51
55
from bzrlib.symbol_versioning import deprecated_function, one_three
52
from bzrlib.trace import mutter, warning, log_exception_quietly
55
58
DEFAULT_PLUGIN_PATH = None
174
177
def load_from_dir(d):
175
"""Load the plugins in directory d."""
178
"""Load the plugins in directory d.
180
d must be in the plugins module path already.
176
182
# Get the list of valid python suffixes for __init__.py?
177
183
# this includes .py, .pyc, and .pyo (depending on if we are running -O)
178
184
# but it doesn't include compiled modules (.so, .dll, etc)
202
if getattr(_mod_plugins, f, None):
203
mutter('Plugin name %s already loaded', f)
209
continue # We don't load __init__.py again in the plugin dir
210
elif getattr(_mod_plugins, f, None):
211
trace.mutter('Plugin name %s already loaded', f)
205
# mutter('add plugin name %s', f)
213
# trace.mutter('add plugin name %s', f)
206
214
plugin_names.add(f)
208
216
for name in plugin_names:
210
218
exec "import bzrlib.plugins.%s" % name in {}
211
219
except KeyboardInterrupt:
221
except errors.IncompatibleAPI, e:
222
trace.warning("Unable to load plugin %r. It requested API version "
223
"%s of module %s but the minimum exported version is %s, and "
224
"the maximum is %s" %
225
(name, e.wanted, e.api, e.minimum, e.current))
213
226
except Exception, e:
227
trace.warning("%s" % e)
214
228
## import pdb; pdb.set_trace()
215
229
if re.search('\.|-| ', name):
216
230
sanitised_name = re.sub('[-. ]', '_', name)
217
231
if sanitised_name.startswith('bzr_'):
218
232
sanitised_name = sanitised_name[len('bzr_'):]
219
warning("Unable to load %r in %r as a plugin because the "
233
trace.warning("Unable to load %r in %r as a plugin because the "
220
234
"file path isn't a valid module name; try renaming "
221
235
"it to %r." % (name, d, sanitised_name))
223
warning('Unable to load plugin %r from %r' % (name, d))
224
log_exception_quietly()
237
trace.warning('Unable to load plugin %r from %r' % (name, d))
238
trace.log_exception_quietly()
225
239
if 'error' in debug.debug_flags:
226
240
trace.print_exception(sys.exc_info(), sys.stderr)
238
252
archive = zip_name[:index+4]
239
253
prefix = zip_name[index+5:]
241
mutter('Looking for plugins in %r', zip_name)
255
trace.mutter('Looking for plugins in %r', zip_name)
243
257
# use zipfile to get list of files/dirs inside zip
258
272
for name in namelist
259
273
if name.startswith(prefix)]
261
mutter('Names in archive: %r', namelist)
275
trace.mutter('Names in archive: %r', namelist)
263
277
for name in namelist:
264
278
if not name or name.endswith('/'):
290
304
if not plugin_name:
292
306
if getattr(_mod_plugins, plugin_name, None):
293
mutter('Plugin name %s already loaded', plugin_name)
307
trace.mutter('Plugin name %s already loaded', plugin_name)
297
311
exec "import bzrlib.plugins.%s" % plugin_name in {}
298
mutter('Load plugin %s from zip %r', plugin_name, zip_name)
312
trace.mutter('Load plugin %s from zip %r', plugin_name, zip_name)
299
313
except KeyboardInterrupt:
301
315
except Exception, e:
302
316
## import pdb; pdb.set_trace()
303
warning('Unable to load plugin %r from %r'
317
trace.warning('Unable to load plugin %r from %r'
304
318
% (name, zip_name))
305
log_exception_quietly()
319
trace.log_exception_quietly()
306
320
if 'error' in debug.debug_flags:
307
321
trace.print_exception(sys.exc_info(), sys.stderr)