~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: 2007-10-25 08:29:08 UTC
  • mfrom: (2940.1.2 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20071025082908-abn3kunrb2ivdvth
renaming of experimental pack formats to include knitpack in their name (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 
42
41
 
43
42
from bzrlib import (
44
43
    config,
45
 
    debug,
46
44
    osutils,
47
 
    trace,
48
45
    )
49
46
from bzrlib import plugins as _mod_plugins
50
47
""")
51
48
 
52
 
from bzrlib.symbol_versioning import deprecated_function, one_three
 
49
from bzrlib.symbol_versioning import deprecated_function, zero_ninetyone
53
50
from bzrlib.trace import mutter, warning, log_exception_quietly
54
51
 
55
52
 
64
61
    return DEFAULT_PLUGIN_PATH
65
62
 
66
63
 
 
64
@deprecated_function(zero_ninetyone)
 
65
def all_plugins():
 
66
    """Return a dictionary of the plugins."""
 
67
    return dict((name, plugin.module) for name, plugin in plugins().items())
 
68
 
 
69
 
67
70
def disable_plugins():
68
71
    """Disable loading plugins.
69
72
 
74
77
    global _loaded
75
78
    _loaded = True
76
79
 
77
 
 
78
80
def _strip_trailing_sep(path):
79
81
    return path.rstrip("\\/")
80
82
 
81
 
 
82
83
def set_plugins_path():
83
84
    """Set the path for plugins to be loaded from."""
84
85
    path = os.environ.get('BZR_PLUGIN_PATH',
85
86
                          get_default_plugin_path()).split(os.pathsep)
86
 
    bzr_exe = bool(getattr(sys, 'frozen', None))
87
 
    if bzr_exe:    # expand path for bzr.exe
88
 
        # We need to use relative path to system-wide plugin
89
 
        # directory because bzrlib from standalone bzr.exe
90
 
        # could be imported by another standalone program
91
 
        # (e.g. bzr-config; or TortoiseBzr/Olive if/when they
92
 
        # will become standalone exe). [bialix 20071123]
93
 
        # __file__ typically is
94
 
        # C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc
95
 
        # then plugins directory is
96
 
        # C:\Program Files\Bazaar\plugins
97
 
        # so relative path is ../../../plugins
98
 
        path.append(osutils.abspath(osutils.pathjoin(
99
 
            osutils.dirname(__file__), '../../../plugins')))
100
87
    # Get rid of trailing slashes, since Python can't handle them when
101
88
    # it tries to import modules.
102
89
    path = map(_strip_trailing_sep, path)
103
 
    if not bzr_exe:     # don't look inside library.zip
104
 
        # search the plugin path before the bzrlib installed dir
105
 
        path.append(os.path.dirname(_mod_plugins.__file__))
106
 
    # search the arch independent path if we can determine that and
107
 
    # the plugin is found nowhere else
108
 
    if sys.platform != 'win32':
109
 
        try:
110
 
            from distutils.sysconfig import get_python_lib
111
 
        except ImportError:
112
 
            # If distutuils is not available, we just won't add that path
113
 
            pass
114
 
        else:
115
 
            archless_path = osutils.pathjoin(get_python_lib(), 'bzrlib',
116
 
                    'plugins')
117
 
            if archless_path not in path:
118
 
                path.append(archless_path)
 
90
    # search the plugin path before the bzrlib installed dir
 
91
    path.append(os.path.dirname(_mod_plugins.__file__))
119
92
    _mod_plugins.__path__ = path
120
93
    return path
121
94
 
165
138
        mutter('looking for plugins in %s', d)
166
139
        if os.path.isdir(d):
167
140
            load_from_dir(d)
 
141
        else:
 
142
            # it might be a zip: try loading from the zip.
 
143
            load_from_zip(d)
 
144
            continue
168
145
 
169
146
 
170
147
# backwards compatability: load_from_dirs was the old name
214
191
        except Exception, e:
215
192
            ## import pdb; pdb.set_trace()
216
193
            if re.search('\.|-| ', name):
217
 
                sanitised_name = re.sub('[-. ]', '_', name)
218
 
                if sanitised_name.startswith('bzr_'):
219
 
                    sanitised_name = sanitised_name[len('bzr_'):]
220
 
                warning("Unable to load %r in %r as a plugin because the "
221
 
                        "file path isn't a valid module name; try renaming "
222
 
                        "it to %r." % (name, d, sanitised_name))
 
194
                warning('Unable to load plugin %r from %r: '
 
195
                    'It is not a valid python module name.' % (name, d))
223
196
            else:
224
197
                warning('Unable to load plugin %r from %r' % (name, d))
225
198
            log_exception_quietly()
226
 
            if 'error' in debug.debug_flags:
227
 
                trace.print_exception(sys.exc_info(), sys.stderr)
228
 
 
229
 
 
230
 
@deprecated_function(one_three)
 
199
 
 
200
 
231
201
def load_from_zip(zip_name):
232
202
    """Load all the plugins in a zip."""
233
203
    valid_suffixes = ('.py', '.pyc', '.pyo')    # only python modules/packages
234
204
                                                # is allowed
 
205
 
235
206
    try:
236
207
        index = zip_name.rindex('.zip')
237
208
    except ValueError:
304
275
            warning('Unable to load plugin %r from %r'
305
276
                    % (name, zip_name))
306
277
            log_exception_quietly()
307
 
            if 'error' in debug.debug_flags:
308
 
                trace.print_exception(sys.exc_info(), sys.stderr)
309
278
 
310
279
 
311
280
def plugins():
404
373
        if getattr(self.module, '__path__', None) is not None:
405
374
            return os.path.abspath(self.module.__path__[0])
406
375
        elif getattr(self.module, '__file__', None) is not None:
407
 
            path = os.path.abspath(self.module.__file__)
408
 
            if path[-4:] in ('.pyc', '.pyo'):
409
 
                pypath = path[:-4] + '.py'
410
 
                if os.path.isfile(pypath):
411
 
                    path = pypath
412
 
            return path
 
376
            return os.path.abspath(self.module.__file__)
413
377
        else:
414
378
            return repr(self.module)
415
379
 
427
391
        else:
428
392
            return None
429
393
 
430
 
    def load_plugin_tests(self, loader):
431
 
        """Return the adapted plugin's test suite.
432
 
 
433
 
        :param loader: The custom loader that should be used to load additional
434
 
            tests.
435
 
 
436
 
        """
437
 
        if getattr(self.module, 'load_tests', None) is not None:
438
 
            return loader.loadTestsFromModule(self.module)
439
 
        else:
440
 
            return None
441
 
 
442
394
    def version_info(self):
443
395
        """Return the plugin's version_tuple or None if unknown."""
444
396
        version_info = getattr(self.module, 'version_info', None)
445
397
        if version_info is not None and len(version_info) == 3:
446
398
            version_info = tuple(version_info) + ('final', 0)
447
399
        return version_info
448
 
 
 
400
    
449
401
    def _get__version__(self):
450
402
        version_info = self.version_info()
451
403
        if version_info is None: