~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

  • Committer: John Arbash Meinel
  • Date: 2008-10-30 00:55:00 UTC
  • mto: (3815.2.5 prepare-1.9)
  • mto: This revision was merged to the branch mainline in revision 3811.
  • Revision ID: john@arbash-meinel.com-20081030005500-r5cej1cxflqhs3io
Switch so that we are using a simple timestamp as the first action.

Show diffs side-by-side

added added

removed removed

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