~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

  • Committer: Martin Pool
  • Date: 2010-06-02 04:56:07 UTC
  • mto: This revision was merged to the branch mainline in revision 5279.
  • Revision ID: mbp@canonical.com-20100602045607-uhn6m12c9k5rjlxz
Change _LINUX_NL to _UNIX_NL

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
63
63
_plugins_disabled = False
64
64
 
65
65
 
66
 
plugin_warnings = {}
67
 
# Map from plugin name, to list of string warnings about eg plugin
68
 
# dependencies.
69
 
 
70
 
 
71
66
def are_plugins_disabled():
72
67
    return _plugins_disabled
73
68
 
82
77
    load_plugins([])
83
78
 
84
79
 
85
 
def describe_plugins(show_paths=False):
86
 
    """Generate text description of plugins.
87
 
 
88
 
    Includes both those that have loaded, and those that failed to 
89
 
    load.
90
 
 
91
 
    :param show_paths: If true,
92
 
    :returns: Iterator of text lines (including newlines.)
93
 
    """
94
 
    from inspect import getdoc
95
 
    loaded_plugins = plugins()
96
 
    all_names = sorted(list(set(
97
 
        loaded_plugins.keys() + plugin_warnings.keys())))
98
 
    for name in all_names:
99
 
        if name in loaded_plugins:
100
 
            plugin = loaded_plugins[name]
101
 
            version = plugin.__version__
102
 
            if version == 'unknown':
103
 
                version = ''
104
 
            yield '%s %s\n' % (name, version)
105
 
            d = getdoc(plugin.module)
106
 
            if d:
107
 
                doc = d.split('\n')[0]
108
 
            else:
109
 
                doc = '(no description)'
110
 
            yield ("  %s\n" % doc)
111
 
            if show_paths:
112
 
                yield ("   %s\n" % plugin.path())
113
 
            del plugin
114
 
        else:
115
 
            yield "%s (failed to load)\n" % name
116
 
        if name in plugin_warnings:
117
 
            for line in plugin_warnings[name]:
118
 
                yield "  ** " + line + '\n'
119
 
        yield '\n'
120
 
 
121
 
 
122
80
def _strip_trailing_sep(path):
123
81
    return path.rstrip("\\/")
124
82
 
125
83
 
126
 
def _get_specific_plugin_paths(paths):
127
 
    """Returns the plugin paths from a string describing the associations.
128
 
 
129
 
    :param paths: A string describing the paths associated with the plugins.
130
 
 
131
 
    :returns: A list of (plugin name, path) tuples.
132
 
 
133
 
    For example, if paths is my_plugin@/test/my-test:her_plugin@/production/her,
134
 
    [('my_plugin', '/test/my-test'), ('her_plugin', '/production/her')] 
135
 
    will be returned.
136
 
 
137
 
    Note that ':' in the example above depends on the os.
138
 
    """
139
 
    if not paths:
140
 
        return []
141
 
    specs = []
142
 
    for spec in paths.split(os.pathsep):
143
 
        try:
144
 
            name, path = spec.split('@')
145
 
        except ValueError:
146
 
            raise errors.BzrCommandError(
147
 
                '"%s" is not a valid <plugin_name>@<plugin_path> description '
148
 
                % spec)
149
 
        specs.append((name, path))
150
 
    return specs
151
 
 
152
 
 
153
84
def set_plugins_path(path=None):
154
85
    """Set the path for plugins to be loaded from.
155
86
 
167
98
        for name in disabled_plugins.split(os.pathsep):
168
99
            PluginImporter.blacklist.add('bzrlib.plugins.' + name)
169
100
    # Set up a the specific paths for plugins
170
 
    for plugin_name, plugin_path in _get_specific_plugin_paths(os.environ.get(
171
 
            'BZR_PLUGINS_AT', None)):
 
101
    specific_plugins = os.environ.get('BZR_PLUGINS_AT', None)
 
102
    if specific_plugins is not None:
 
103
        for spec in specific_plugins.split(os.pathsep):
 
104
            plugin_name, plugin_path = spec.split('@')
172
105
            PluginImporter.specific_paths[
173
106
                'bzrlib.plugins.%s' % plugin_name] = plugin_path
174
107
    return path
369
302
    return None, None, (None, None, None)
370
303
 
371
304
 
372
 
def record_plugin_warning(plugin_name, warning_message):
373
 
    trace.mutter(warning_message)
374
 
    plugin_warnings.setdefault(plugin_name, []).append(warning_message)
375
 
 
376
 
 
377
305
def _load_plugin_module(name, dir):
378
306
    """Load plugin name from dir.
379
307
 
387
315
    except KeyboardInterrupt:
388
316
        raise
389
317
    except errors.IncompatibleAPI, e:
390
 
        warning_message = (
391
 
            "Unable to load plugin %r. It requested API version "
 
318
        trace.warning("Unable to load plugin %r. It requested API version "
392
319
            "%s of module %s but the minimum exported version is %s, and "
393
320
            "the maximum is %s" %
394
321
            (name, e.wanted, e.api, e.minimum, e.current))
395
 
        record_plugin_warning(name, warning_message)
396
322
    except Exception, e:
397
323
        trace.warning("%s" % e)
398
324
        if re.search('\.|-| ', name):
403
329
                    "file path isn't a valid module name; try renaming "
404
330
                    "it to %r." % (name, dir, sanitised_name))
405
331
        else:
406
 
            record_plugin_warning(
407
 
                name,
408
 
                'Unable to load plugin %r from %r' % (name, dir))
 
332
            trace.warning('Unable to load plugin %r from %r' % (name, dir))
409
333
        trace.log_exception_quietly()
410
334
        if 'error' in debug.debug_flags:
411
335
            trace.print_exception(sys.exc_info(), sys.stderr)
638
562
        # We are called only for specific paths
639
563
        plugin_path = self.specific_paths[fullname]
640
564
        loading_path = None
 
565
        package = False
641
566
        if os.path.isdir(plugin_path):
642
567
            for suffix, mode, kind in imp.get_suffixes():
643
568
                if kind not in (imp.PY_SOURCE, imp.PY_COMPILED):
645
570
                    continue
646
571
                init_path = osutils.pathjoin(plugin_path, '__init__' + suffix)
647
572
                if os.path.isfile(init_path):
648
 
                    # We've got a module here and load_module needs specific
649
 
                    # parameters.
650
 
                    loading_path = plugin_path
651
 
                    suffix = ''
652
 
                    mode = ''
653
 
                    kind = imp.PKG_DIRECTORY
 
573
                    loading_path = init_path
 
574
                    package = True
654
575
                    break
655
576
        else:
656
577
            for suffix, mode, kind in imp.get_suffixes():
660
581
        if loading_path is None:
661
582
            raise ImportError('%s cannot be loaded from %s'
662
583
                              % (fullname, plugin_path))
663
 
        if kind is imp.PKG_DIRECTORY:
664
 
            f = None
665
 
        else:
666
 
            f = open(loading_path, mode)
 
584
        f = open(loading_path, mode)
667
585
        try:
668
586
            mod = imp.load_module(fullname, f, loading_path,
669
587
                                  (suffix, mode, kind))
 
588
            if package:
 
589
                # The plugin can contain modules, so be ready
 
590
                mod.__path__ = [plugin_path]
670
591
            mod.__package__ = fullname
671
592
            return mod
672
593
        finally:
673
 
            if f is not None:
674
 
                f.close()
 
594
            f.close()
675
595
 
676
596
 
677
597
# Install a dedicated importer for plugins requiring special handling