/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: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
plugins.
35
35
"""
36
36
 
37
 
from __future__ import absolute_import
38
 
 
39
37
import os
40
38
import re
41
39
import sys
46
44
from .lazy_import import lazy_import
47
45
lazy_import(globals(), """
48
46
import imp
49
 
import importlib
50
47
from importlib import util as importlib_util
51
48
 
52
49
from breezy import (
53
 
    config,
 
50
    bedding,
54
51
    debug,
55
 
    errors,
56
52
    help_topics,
57
53
    trace,
58
54
    )
59
55
""")
60
56
 
 
57
from . import (
 
58
    errors,
 
59
    )
 
60
 
61
61
 
62
62
_MODULE_PREFIX = "breezy.plugins."
63
63
 
64
 
if __debug__ or sys.version_info > (3,):
65
 
    COMPILED_EXT = ".pyc"
66
 
else:
67
 
    COMPILED_EXT = ".pyo"
 
64
COMPILED_EXT = ".pyc"
68
65
 
69
66
 
70
67
def disable_plugins(state=None):
102
99
        from breezy.plugins import __path__ as path
103
100
 
104
101
    state.plugin_warnings = {}
105
 
    _load_plugins(state, path)
 
102
    _load_plugins_from_path(state, path)
 
103
    if (None, 'entrypoints') in _env_plugin_path():
 
104
        _load_plugins_from_entrypoints(state)
106
105
    state.plugins = plugins()
107
106
 
108
107
 
 
108
def _load_plugins_from_entrypoints(state):
 
109
    try:
 
110
        import pkg_resources
 
111
    except ImportError:
 
112
        # No pkg_resources, no entrypoints.
 
113
        pass
 
114
    else:
 
115
        for ep in pkg_resources.iter_entry_points('breezy.plugin'):
 
116
            fullname = _MODULE_PREFIX + ep.name
 
117
            if fullname in sys.modules:
 
118
                continue
 
119
            sys.modules[fullname] = ep.load()
 
120
 
 
121
 
109
122
def plugin_name(module_name):
110
123
    """Gives unprefixed name from module_name or None."""
111
124
    if module_name.startswith(_MODULE_PREFIX):
206
219
    """Gives list of paths and contexts for plugins from environ key.
207
220
 
208
221
    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'.
 
222
    'path', or None and one of the values 'user', 'core', 'entrypoints', 'site'.
210
223
    """
211
224
    path_details = []
212
225
    env = osutils.path_from_environ(key)
213
 
    defaults = {"user": not env, "core": True, "site": True}
 
226
    defaults = {
 
227
        "user": not env,
 
228
        "core": True,
 
229
        "site": True,
 
230
        'entrypoints': False,
 
231
        }
214
232
    if env:
215
233
        # Add paths specified by user in order
216
234
        for p in env.split(os.pathsep):
223
241
                path_details.append((p, 'path'))
224
242
 
225
243
    # Add any remaining default paths
226
 
    for name in ('user', 'core', 'site'):
 
244
    for name in ('user', 'core', 'entrypoints', 'site'):
227
245
        if defaults[name]:
228
246
            path_details.append((None, name))
229
247
 
257
275
        sys.meta_path.insert(2, finder)
258
276
 
259
277
 
260
 
def _load_plugins(state, paths):
 
278
def _load_plugins_from_path(state, paths):
261
279
    """Do the importing all plugins from paths."""
262
280
    imported_names = set()
263
281
    for name, path in _iter_possible_plugins(paths):
387
405
 
388
406
 
389
407
def get_user_plugin_path():
390
 
    return osutils.pathjoin(config.config_dir(), 'plugins')
 
408
    return osutils.pathjoin(bedding.config_dir(), 'plugins')
391
409
 
392
410
 
393
411
def record_plugin_warning(warning_message):
422
440
            if sanitised_name.startswith('brz_'):
423
441
                sanitised_name = sanitised_name[len('brz_'):]
424
442
            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))
 
443
                          "file path isn't a valid module name; try renaming "
 
444
                          "it to %r." % (name, dir, sanitised_name))
427
445
        else:
428
446
            return record_plugin_warning(
429
447
                'Unable to load plugin %r from %r: %s' % (name, dir, e))
438
456
    for fullname in sys.modules:
439
457
        if fullname.startswith(_MODULE_PREFIX):
440
458
            name = fullname[len(_MODULE_PREFIX):]
441
 
            if not "." in name and sys.modules[fullname] is not None:
 
459
            if "." not in name and sys.modules[fullname] is not None:
442
460
                result[name] = PlugIn(name, sys.modules[fullname])
443
461
    return result
444
462
 
463
481
    if state is None:
464
482
        state = breezy.get_global_state()
465
483
    items = []
466
 
    for name, a_plugin in sorted(state.plugins.items()):
 
484
    for name, a_plugin in sorted(getattr(state, 'plugins', {}).items()):
467
485
        items.append("%s[%s]" %
468
 
            (name, a_plugin.__version__))
 
486
                     (name, a_plugin.__version__))
469
487
    return ', '.join(items)
470
488
 
471
489