~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
# That could be either a separate Python interpreter or perhaps a new
40
40
# namespace inside this interpreter.
41
41
 
42
 
import imp
43
42
import os
44
43
import sys
 
44
 
 
45
from bzrlib.lazy_import import lazy_import
 
46
lazy_import(globals(), """
 
47
import imp
45
48
import types
46
49
 
47
 
import bzrlib
48
 
from bzrlib.config import config_dir
49
 
from bzrlib.trace import log_error, mutter, warning, \
50
 
        log_exception_quietly
51
 
from bzrlib.errors import BzrError
52
 
from bzrlib import plugins
53
 
from bzrlib.osutils import pathjoin
54
 
 
55
 
DEFAULT_PLUGIN_PATH = pathjoin(config_dir(), 'plugins')
 
50
from bzrlib import (
 
51
    config,
 
52
    osutils,
 
53
    plugins,
 
54
    )
 
55
""")
 
56
 
 
57
from bzrlib.trace import mutter, warning, log_exception_quietly
 
58
 
 
59
 
 
60
DEFAULT_PLUGIN_PATH = None
 
61
 
 
62
 
 
63
def get_default_plugin_path():
 
64
    """Get the DEFAULT_PLUGIN_PATH"""
 
65
    global DEFAULT_PLUGIN_PATH
 
66
    if DEFAULT_PLUGIN_PATH is None:
 
67
        DEFAULT_PLUGIN_PATH = osutils.pathjoin(config.config_dir(), 'plugins')
 
68
    return DEFAULT_PLUGIN_PATH
 
69
 
56
70
 
57
71
_loaded = False
58
72
 
60
74
def all_plugins():
61
75
    """Return a dictionary of the plugins."""
62
76
    result = {}
63
 
    for name, plugin in bzrlib.plugins.__dict__.items():
 
77
    for name, plugin in plugins.__dict__.items():
64
78
        if isinstance(plugin, types.ModuleType):
65
79
            result[name] = plugin
66
80
    return result
92
106
    if _loaded:
93
107
        # People can make sure plugins are loaded, they just won't be twice
94
108
        return
95
 
        #raise BzrError("plugins already initialized")
96
109
    _loaded = True
97
110
 
98
 
    dirs = os.environ.get('BZR_PLUGIN_PATH', DEFAULT_PLUGIN_PATH).split(os.pathsep)
 
111
    dirs = os.environ.get('BZR_PLUGIN_PATH',
 
112
                          get_default_plugin_path()).split(os.pathsep)
99
113
    dirs.insert(0, os.path.dirname(plugins.__file__))
100
114
 
101
115
    load_from_dirs(dirs)
125
139
        if not os.path.isdir(d):
126
140
            continue
127
141
        for f in os.listdir(d):
128
 
            path = pathjoin(d, f)
 
142
            path = osutils.pathjoin(d, f)
129
143
            if os.path.isdir(path):
130
144
                for entry in package_entries:
131
145
                    # This directory should be a package, and thus added to
132
146
                    # the list
133
 
                    if os.path.isfile(pathjoin(path, entry)):
 
147
                    if os.path.isfile(osutils.pathjoin(path, entry)):
134
148
                        break
135
149
                else: # This directory is not a package
136
150
                    continue
143
157
                        break
144
158
                else:
145
159
                    continue
146
 
            if getattr(bzrlib.plugins, f, None):
 
160
            if getattr(plugins, f, None):
147
161
                mutter('Plugin name %s already loaded', f)
148
162
            else:
149
163
                mutter('add plugin name %s', f)
158
172
                try:
159
173
                    plugin = imp.load_module('bzrlib.plugins.' + name,
160
174
                                             *plugin_info)
161
 
                    setattr(bzrlib.plugins, name, plugin)
 
175
                    setattr(plugins, name, plugin)
162
176
                finally:
163
177
                    if plugin_info[0] is not None:
164
178
                        plugin_info[0].close()