/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

  • Committer: Jelmer Vernooij
  • Date: 2019-05-29 03:22:34 UTC
  • mfrom: (7303 work)
  • mto: This revision was merged to the branch mainline in revision 7306.
  • Revision ID: jelmer@jelmer.uk-20190529032234-mt3fuws8gq03tapi
Merge trunk.

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
50
49
from importlib import util as importlib_util
51
50
 
52
51
from breezy import (
102
101
        from breezy.plugins import __path__ as path
103
102
 
104
103
    state.plugin_warnings = {}
105
 
    _load_plugins(state, path)
 
104
    _load_plugins_from_path(state, path)
 
105
    if (None, 'entrypoints') in _env_plugin_path():
 
106
        _load_plugins_from_entrypoints(state)
106
107
    state.plugins = plugins()
107
108
 
108
109
 
 
110
def _load_plugins_from_entrypoints(state):
 
111
    try:
 
112
        import pkg_resources
 
113
    except ImportError:
 
114
        # No pkg_resources, no entrypoints.
 
115
        pass
 
116
    else:
 
117
        for ep in pkg_resources.iter_entry_points('breezy.plugin'):
 
118
            fullname = _MODULE_PREFIX + ep.name
 
119
            if fullname in sys.modules:
 
120
                continue
 
121
            sys.modules[fullname] = ep.load()
 
122
 
 
123
 
109
124
def plugin_name(module_name):
110
125
    """Gives unprefixed name from module_name or None."""
111
126
    if module_name.startswith(_MODULE_PREFIX):
206
221
    """Gives list of paths and contexts for plugins from environ key.
207
222
 
208
223
    Each entry is either a specific path to load plugins from and the value
209
 
    'path', or None and one of the three values 'user', 'core', 'site'.
 
224
    'path', or None and one of the values 'user', 'core', 'entrypoints', 'site'.
210
225
    """
211
226
    path_details = []
212
227
    env = osutils.path_from_environ(key)
213
 
    defaults = {"user": not env, "core": True, "site": True}
 
228
    defaults = {"user": not env, "core": True, "site": True, 'entrypoints': True}
214
229
    if env:
215
230
        # Add paths specified by user in order
216
231
        for p in env.split(os.pathsep):
223
238
                path_details.append((p, 'path'))
224
239
 
225
240
    # Add any remaining default paths
226
 
    for name in ('user', 'core', 'site'):
 
241
    for name in ('user', 'core', 'entrypoints', 'site'):
227
242
        if defaults[name]:
228
243
            path_details.append((None, name))
229
244
 
257
272
        sys.meta_path.insert(2, finder)
258
273
 
259
274
 
260
 
def _load_plugins(state, paths):
 
275
def _load_plugins_from_path(state, paths):
261
276
    """Do the importing all plugins from paths."""
262
277
    imported_names = set()
263
278
    for name, path in _iter_possible_plugins(paths):
422
437
            if sanitised_name.startswith('brz_'):
423
438
                sanitised_name = sanitised_name[len('brz_'):]
424
439
            trace.warning("Unable to load %r in %r as a plugin because the "
425
 
                    "file path isn't a valid module name; try renaming "
426
 
                    "it to %r." % (name, dir, sanitised_name))
 
440
                          "file path isn't a valid module name; try renaming "
 
441
                          "it to %r." % (name, dir, sanitised_name))
427
442
        else:
428
443
            return record_plugin_warning(
429
444
                'Unable to load plugin %r from %r: %s' % (name, dir, e))
438
453
    for fullname in sys.modules:
439
454
        if fullname.startswith(_MODULE_PREFIX):
440
455
            name = fullname[len(_MODULE_PREFIX):]
441
 
            if not "." in name and sys.modules[fullname] is not None:
 
456
            if "." not in name and sys.modules[fullname] is not None:
442
457
                result[name] = PlugIn(name, sys.modules[fullname])
443
458
    return result
444
459
 
465
480
    items = []
466
481
    for name, a_plugin in sorted(getattr(state, 'plugins', {}).items()):
467
482
        items.append("%s[%s]" %
468
 
            (name, a_plugin.__version__))
 
483
                     (name, a_plugin.__version__))
469
484
    return ', '.join(items)
470
485
 
471
486