/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 breezy/plugin.py

Merge test-run support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
from .lazy_import import lazy_import
47
47
lazy_import(globals(), """
48
48
import imp
 
49
import importlib
49
50
from importlib import util as importlib_util
50
51
 
51
52
from breezy import (
52
 
    bedding,
 
53
    config,
53
54
    debug,
 
55
    errors,
54
56
    help_topics,
55
57
    trace,
56
58
    )
57
59
""")
58
60
 
59
 
from . import (
60
 
    errors,
61
 
    )
62
 
 
63
61
 
64
62
_MODULE_PREFIX = "breezy.plugins."
65
63
 
104
102
        from breezy.plugins import __path__ as path
105
103
 
106
104
    state.plugin_warnings = {}
107
 
    _load_plugins_from_path(state, path)
108
 
    if (None, 'entrypoints') in _env_plugin_path():
109
 
        _load_plugins_from_entrypoints(state)
 
105
    _load_plugins(state, path)
110
106
    state.plugins = plugins()
111
107
 
112
108
 
113
 
def _load_plugins_from_entrypoints(state):
114
 
    try:
115
 
        import pkg_resources
116
 
    except ImportError:
117
 
        # No pkg_resources, no entrypoints.
118
 
        pass
119
 
    else:
120
 
        for ep in pkg_resources.iter_entry_points('breezy.plugin'):
121
 
            fullname = _MODULE_PREFIX + ep.name
122
 
            if fullname in sys.modules:
123
 
                continue
124
 
            sys.modules[fullname] = ep.load()
125
 
 
126
 
 
127
109
def plugin_name(module_name):
128
110
    """Gives unprefixed name from module_name or None."""
129
111
    if module_name.startswith(_MODULE_PREFIX):
224
206
    """Gives list of paths and contexts for plugins from environ key.
225
207
 
226
208
    Each entry is either a specific path to load plugins from and the value
227
 
    'path', or None and one of the values 'user', 'core', 'entrypoints', 'site'.
 
209
    'path', or None and one of the three values 'user', 'core', 'site'.
228
210
    """
229
211
    path_details = []
230
212
    env = osutils.path_from_environ(key)
231
 
    defaults = {
232
 
        "user": not env,
233
 
        "core": True,
234
 
        "site": True,
235
 
        'entrypoints': False,
236
 
        }
 
213
    defaults = {"user": not env, "core": True, "site": True}
237
214
    if env:
238
215
        # Add paths specified by user in order
239
216
        for p in env.split(os.pathsep):
246
223
                path_details.append((p, 'path'))
247
224
 
248
225
    # Add any remaining default paths
249
 
    for name in ('user', 'core', 'entrypoints', 'site'):
 
226
    for name in ('user', 'core', 'site'):
250
227
        if defaults[name]:
251
228
            path_details.append((None, name))
252
229
 
280
257
        sys.meta_path.insert(2, finder)
281
258
 
282
259
 
283
 
def _load_plugins_from_path(state, paths):
 
260
def _load_plugins(state, paths):
284
261
    """Do the importing all plugins from paths."""
285
262
    imported_names = set()
286
263
    for name, path in _iter_possible_plugins(paths):
410
387
 
411
388
 
412
389
def get_user_plugin_path():
413
 
    return osutils.pathjoin(bedding.config_dir(), 'plugins')
 
390
    return osutils.pathjoin(config.config_dir(), 'plugins')
414
391
 
415
392
 
416
393
def record_plugin_warning(warning_message):
445
422
            if sanitised_name.startswith('brz_'):
446
423
                sanitised_name = sanitised_name[len('brz_'):]
447
424
            trace.warning("Unable to load %r in %r as a plugin because the "
448
 
                          "file path isn't a valid module name; try renaming "
449
 
                          "it to %r." % (name, dir, sanitised_name))
 
425
                    "file path isn't a valid module name; try renaming "
 
426
                    "it to %r." % (name, dir, sanitised_name))
450
427
        else:
451
428
            return record_plugin_warning(
452
429
                'Unable to load plugin %r from %r: %s' % (name, dir, e))
461
438
    for fullname in sys.modules:
462
439
        if fullname.startswith(_MODULE_PREFIX):
463
440
            name = fullname[len(_MODULE_PREFIX):]
464
 
            if "." not in name and sys.modules[fullname] is not None:
 
441
            if not "." in name and sys.modules[fullname] is not None:
465
442
                result[name] = PlugIn(name, sys.modules[fullname])
466
443
    return result
467
444
 
488
465
    items = []
489
466
    for name, a_plugin in sorted(getattr(state, 'plugins', {}).items()):
490
467
        items.append("%s[%s]" %
491
 
                     (name, a_plugin.__version__))
 
468
            (name, a_plugin.__version__))
492
469
    return ', '.join(items)
493
470
 
494
471