/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 bzrlib/plugin.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-08-29 05:16:58 UTC
  • mfrom: (2762.2.3 plugin-versions)
  • Revision ID: pqm@pqm.ubuntu.com-20070829051658-yvj0n3mvso1v8uz3
(robertc) Show the version of plugins in the output of bzr plugins. (Robert Collins, #125421)

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
from bzrlib import (
43
43
    config,
44
44
    osutils,
45
 
    plugins,
46
45
    )
 
46
from bzrlib import plugins as _mod_plugins
47
47
""")
48
48
 
 
49
from bzrlib.symbol_versioning import deprecated_function, zero_ninetyone
49
50
from bzrlib.trace import mutter, warning, log_exception_quietly
50
51
 
51
52
 
60
61
    return DEFAULT_PLUGIN_PATH
61
62
 
62
63
 
 
64
@deprecated_function(zero_ninetyone)
63
65
def all_plugins():
64
66
    """Return a dictionary of the plugins."""
65
 
    result = {}
66
 
    for name, plugin in plugins.__dict__.items():
67
 
        if isinstance(plugin, types.ModuleType):
68
 
            result[name] = plugin
69
 
    return result
 
67
    return dict((name, plugin.module) for name, plugin in plugins().items())
70
68
 
71
69
 
72
70
def disable_plugins():
90
88
    # it tries to import modules.
91
89
    path = map(_strip_trailing_sep, path)
92
90
    # search the plugin path before the bzrlib installed dir
93
 
    path.append(os.path.dirname(plugins.__file__))
94
 
    plugins.__path__ = path
 
91
    path.append(os.path.dirname(_mod_plugins.__file__))
 
92
    _mod_plugins.__path__ = path
95
93
    return path
96
94
 
97
95
 
133
131
    # this function, and since it sets plugins.__path__, it should set it to
134
132
    # something that will be valid for Python to use (in case people try to
135
133
    # run "import bzrlib.plugins.PLUGINNAME" after calling this function).
136
 
    plugins.__path__ = map(_strip_trailing_sep, dirs)
 
134
    _mod_plugins.__path__ = map(_strip_trailing_sep, dirs)
137
135
    for d in dirs:
138
136
        if not d:
139
137
            continue
179
177
                    break
180
178
            else:
181
179
                continue
182
 
        if getattr(plugins, f, None):
 
180
        if getattr(_mod_plugins, f, None):
183
181
            mutter('Plugin name %s already loaded', f)
184
182
        else:
185
183
            # mutter('add plugin name %s', f)
263
261
    
264
262
        if not plugin_name:
265
263
            continue
266
 
        if getattr(plugins, plugin_name, None):
 
264
        if getattr(_mod_plugins, plugin_name, None):
267
265
            mutter('Plugin name %s already loaded', plugin_name)
268
266
            continue
269
267
    
279
277
            log_exception_quietly()
280
278
 
281
279
 
 
280
def plugins():
 
281
    """Return a dictionary of the plugins.
 
282
    
 
283
    Each item in the dictionary is a PlugIn object.
 
284
    """
 
285
    result = {}
 
286
    for name, plugin in _mod_plugins.__dict__.items():
 
287
        if isinstance(plugin, types.ModuleType):
 
288
            result[name] = PlugIn(name, plugin)
 
289
    return result
 
290
 
 
291
 
282
292
class PluginsHelpIndex(object):
283
293
    """A help index that returns help topics for plugins."""
284
294
 
345
355
    def get_help_topic(self):
346
356
        """Return the modules help topic - its __name__ after bzrlib.plugins.."""
347
357
        return self.module.__name__[len('bzrlib.plugins.'):]
 
358
 
 
359
 
 
360
class PlugIn(object):
 
361
    """The bzrlib representation of a plugin.
 
362
 
 
363
    The PlugIn object provides a way to manipulate a given plugin module.
 
364
    """
 
365
 
 
366
    def __init__(self, name, module):
 
367
        """Construct a plugin for module."""
 
368
        self.name = name
 
369
        self.module = module
 
370
 
 
371
    def path(self):
 
372
        """Get the path that this plugin was loaded from."""
 
373
        if getattr(self.module, '__path__', None) is not None:
 
374
            return os.path.abspath(self.module.__path__[0])
 
375
        elif getattr(self.module, '__file__', None) is not None:
 
376
            return os.path.abspath(self.module.__file__)
 
377
        else:
 
378
            return repr(self.module)
 
379
 
 
380
    def __str__(self):
 
381
        return "<%s.%s object at %s, name=%s, module=%s>" % (
 
382
            self.__class__.__module__, self.__class__.__name__, id(self),
 
383
            self.name, self.module)
 
384
 
 
385
    __repr__ = __str__
 
386
 
 
387
    def test_suite(self):
 
388
        """Return the plugin's test suite."""
 
389
        if getattr(self.module, 'test_suite', None) is not None:
 
390
            return self.module.test_suite()
 
391
        else:
 
392
            return None
 
393
 
 
394
    def version_info(self):
 
395
        """Return the plugin's version_tuple or None if unknown."""
 
396
        version_info = getattr(self.module, 'version_info', None)
 
397
        if version_info is not None and len(version_info) == 3:
 
398
            version_info = tuple(version_info) + ('final', 0)
 
399
        return version_info
 
400
    
 
401
    def _get__version__(self):
 
402
        version_info = self.version_info()
 
403
        if version_info is None:
 
404
            return "unknown"
 
405
        if version_info[3] == 'final':
 
406
            version_string = '%d.%d.%d' % version_info[:3]
 
407
        else:
 
408
            version_string = '%d.%d.%d%s%d' % version_info
 
409
        return version_string
 
410
 
 
411
    __version__ = property(_get__version__)