/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

Make sure we can load from a non-standard directory name.

* bzrlib/tests/test_plugins.py:
(TestPluginMixin.create_plugin): Add a doc string and a variable.
(TestPluginMixin.create_plugin_package): Leave callers decide the
directory name.
(TestLoadPluginAt.setUp): Use a non-standard directory name to
ensure we can work around python limitations.
(TestLoadPluginAt.assertTestFooLoadedFrom): Add more specific
checks.
(TestLoadPluginAt.test_regular_load,
TestLoadPluginAt.test_import): Use a non-standard directory name.
(TestLoadPluginAt.test_loading): Check that the specific plugins
are loaded automatically.

* bzrlib/plugin.py:
(load_from_path): Load explicitly specified plugins.
(_load_plugin_module): Factored out from load_from_dir().

Show diffs side-by-side

added added

removed removed

Lines of Context:
244
244
 
245
245
    The python module path for bzrlib.plugins will be modified to be 'dirs'.
246
246
    """
 
247
    for fullname, path in PluginImporter.specific_paths.iteritems():
 
248
        name = fullname[len('bzrlib.plugins.'):]
 
249
        _load_plugin_module(name, path) # XXX: path *contains* test_foo
 
250
 
247
251
    # We need to strip the trailing separators here as well as in the
248
252
    # set_plugins_path function because calling code can pass anything in to
249
253
    # this function, and since it sets plugins.__path__, it should set it to
296
300
    return None, None, (None, None, None)
297
301
 
298
302
 
 
303
def _load_plugin_module(name, dir):
 
304
    if ('bzrlib.plugins.%s' % name) in PluginImporter.blacklist:
 
305
        return
 
306
    try:
 
307
        exec "import bzrlib.plugins.%s" % name in {}
 
308
    except KeyboardInterrupt:
 
309
        raise
 
310
    except errors.IncompatibleAPI, e:
 
311
        trace.warning("Unable to load plugin %r. It requested API version "
 
312
            "%s of module %s but the minimum exported version is %s, and "
 
313
            "the maximum is %s" %
 
314
            (name, e.wanted, e.api, e.minimum, e.current))
 
315
    except Exception, e:
 
316
        trace.warning("%s" % e)
 
317
        if re.search('\.|-| ', name):
 
318
            sanitised_name = re.sub('[-. ]', '_', name)
 
319
            if sanitised_name.startswith('bzr_'):
 
320
                sanitised_name = sanitised_name[len('bzr_'):]
 
321
            trace.warning("Unable to load %r in %r as a plugin because the "
 
322
                    "file path isn't a valid module name; try renaming "
 
323
                    "it to %r." % (name, dir, sanitised_name))
 
324
        else:
 
325
            trace.warning('Unable to load plugin %r from %r' % (name, dir))
 
326
        trace.log_exception_quietly()
 
327
        if 'error' in debug.debug_flags:
 
328
            trace.print_exception(sys.exc_info(), sys.stderr)
 
329
 
 
330
 
299
331
def load_from_dir(d):
300
332
    """Load the plugins in directory d.
301
333
 
321
353
                plugin_names.add(name)
322
354
 
323
355
    for name in plugin_names:
324
 
        if ('bzrlib.plugins.%s' % name) in PluginImporter.blacklist:
325
 
            continue
326
 
        try:
327
 
            exec "import bzrlib.plugins.%s" % name in {}
328
 
        except KeyboardInterrupt:
329
 
            raise
330
 
        except errors.IncompatibleAPI, e:
331
 
            trace.warning("Unable to load plugin %r. It requested API version "
332
 
                "%s of module %s but the minimum exported version is %s, and "
333
 
                "the maximum is %s" %
334
 
                (name, e.wanted, e.api, e.minimum, e.current))
335
 
        except Exception, e:
336
 
            trace.warning("%s" % e)
337
 
            if re.search('\.|-| ', name):
338
 
                sanitised_name = re.sub('[-. ]', '_', name)
339
 
                if sanitised_name.startswith('bzr_'):
340
 
                    sanitised_name = sanitised_name[len('bzr_'):]
341
 
                trace.warning("Unable to load %r in %r as a plugin because the "
342
 
                        "file path isn't a valid module name; try renaming "
343
 
                        "it to %r." % (name, d, sanitised_name))
344
 
            else:
345
 
                trace.warning('Unable to load plugin %r from %r' % (name, d))
346
 
            trace.log_exception_quietly()
347
 
            if 'error' in debug.debug_flags:
348
 
                trace.print_exception(sys.exc_info(), sys.stderr)
 
356
        _load_plugin_module(name, d)
349
357
 
350
358
 
351
359
def plugins():