~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-06-06 13:56:24 UTC
  • mfrom: (3477.1.2 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080606135624-1ambbf8pct52xfh8
(jam) Move bzrlib.tests.test_diff.UnicodeFilename into
        bzrlib.tests.UnicodeFilenameFeature

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2004, 2005, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
When load_plugins() is invoked, any python module in any directory in
21
21
$BZR_PLUGIN_PATH will be imported.  The module will be imported as
22
22
'bzrlib.plugins.$BASENAME(PLUGIN)'.  In the plugin's main body, it should
23
 
update any bzrlib registries it wants to extend.
24
 
 
25
 
See the plugin-api developer documentation for information about writing
26
 
plugins.
 
23
update any bzrlib registries it wants to extend; for example, to add new
 
24
commands, import bzrlib.commands and add your new command to the plugin_cmds
 
25
variable.
27
26
 
28
27
BZR_PLUGIN_PATH is also honoured for any plugins imported via
29
28
'import bzrlib.plugins.PLUGINNAME', as long as set_plugins_path has been 
33
32
import os
34
33
import sys
35
34
 
36
 
from bzrlib import osutils
37
 
 
38
35
from bzrlib.lazy_import import lazy_import
39
 
 
40
36
lazy_import(globals(), """
41
37
import imp
42
38
import re
46
42
from bzrlib import (
47
43
    config,
48
44
    debug,
49
 
    errors,
 
45
    osutils,
50
46
    trace,
51
47
    )
52
48
from bzrlib import plugins as _mod_plugins
53
49
""")
54
50
 
55
51
from bzrlib.symbol_versioning import deprecated_function, one_three
 
52
from bzrlib.trace import mutter, warning, log_exception_quietly
56
53
 
57
54
 
58
55
DEFAULT_PLUGIN_PATH = None
164
161
    for d in dirs:
165
162
        if not d:
166
163
            continue
167
 
        trace.mutter('looking for plugins in %s', d)
 
164
        mutter('looking for plugins in %s', d)
168
165
        if os.path.isdir(d):
169
166
            load_from_dir(d)
170
167
 
175
172
 
176
173
 
177
174
def load_from_dir(d):
178
 
    """Load the plugins in directory d.
179
 
    
180
 
    d must be in the plugins module path already.
181
 
    """
 
175
    """Load the plugins in directory d."""
182
176
    # Get the list of valid python suffixes for __init__.py?
183
177
    # this includes .py, .pyc, and .pyo (depending on if we are running -O)
184
178
    # but it doesn't include compiled modules (.so, .dll, etc)
205
199
                    break
206
200
            else:
207
201
                continue
208
 
        if f == '__init__':
209
 
            continue # We don't load __init__.py again in the plugin dir
210
 
        elif getattr(_mod_plugins, f, None):
211
 
            trace.mutter('Plugin name %s already loaded', f)
 
202
        if getattr(_mod_plugins, f, None):
 
203
            mutter('Plugin name %s already loaded', f)
212
204
        else:
213
 
            # trace.mutter('add plugin name %s', f)
 
205
            # mutter('add plugin name %s', f)
214
206
            plugin_names.add(f)
215
207
    
216
208
    for name in plugin_names:
218
210
            exec "import bzrlib.plugins.%s" % name in {}
219
211
        except KeyboardInterrupt:
220
212
            raise
221
 
        except errors.IncompatibleAPI, e:
222
 
            trace.warning("Unable to load plugin %r. It requested API version "
223
 
                "%s of module %s but the minimum exported version is %s, and "
224
 
                "the maximum is %s" %
225
 
                (name, e.wanted, e.api, e.minimum, e.current))
226
213
        except Exception, e:
227
 
            trace.warning("%s" % e)
228
214
            ## import pdb; pdb.set_trace()
229
215
            if re.search('\.|-| ', name):
230
216
                sanitised_name = re.sub('[-. ]', '_', name)
231
217
                if sanitised_name.startswith('bzr_'):
232
218
                    sanitised_name = sanitised_name[len('bzr_'):]
233
 
                trace.warning("Unable to load %r in %r as a plugin because the "
 
219
                warning("Unable to load %r in %r as a plugin because the "
234
220
                        "file path isn't a valid module name; try renaming "
235
221
                        "it to %r." % (name, d, sanitised_name))
236
222
            else:
237
 
                trace.warning('Unable to load plugin %r from %r' % (name, d))
238
 
            trace.log_exception_quietly()
 
223
                warning('Unable to load plugin %r from %r' % (name, d))
 
224
            log_exception_quietly()
239
225
            if 'error' in debug.debug_flags:
240
226
                trace.print_exception(sys.exc_info(), sys.stderr)
241
227
 
252
238
    archive = zip_name[:index+4]
253
239
    prefix = zip_name[index+5:]
254
240
 
255
 
    trace.mutter('Looking for plugins in %r', zip_name)
 
241
    mutter('Looking for plugins in %r', zip_name)
256
242
 
257
243
    # use zipfile to get list of files/dirs inside zip
258
244
    try:
272
258
                    for name in namelist
273
259
                    if name.startswith(prefix)]
274
260
 
275
 
    trace.mutter('Names in archive: %r', namelist)
 
261
    mutter('Names in archive: %r', namelist)
276
262
    
277
263
    for name in namelist:
278
264
        if not name or name.endswith('/'):
304
290
        if not plugin_name:
305
291
            continue
306
292
        if getattr(_mod_plugins, plugin_name, None):
307
 
            trace.mutter('Plugin name %s already loaded', plugin_name)
 
293
            mutter('Plugin name %s already loaded', plugin_name)
308
294
            continue
309
295
    
310
296
        try:
311
297
            exec "import bzrlib.plugins.%s" % plugin_name in {}
312
 
            trace.mutter('Load plugin %s from zip %r', plugin_name, zip_name)
 
298
            mutter('Load plugin %s from zip %r', plugin_name, zip_name)
313
299
        except KeyboardInterrupt:
314
300
            raise
315
301
        except Exception, e:
316
302
            ## import pdb; pdb.set_trace()
317
 
            trace.warning('Unable to load plugin %r from %r'
 
303
            warning('Unable to load plugin %r from %r'
318
304
                    % (name, zip_name))
319
 
            trace.log_exception_quietly()
 
305
            log_exception_quietly()
320
306
            if 'error' in debug.debug_flags:
321
307
                trace.print_exception(sys.exc_info(), sys.stderr)
322
308