/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
1
# Copyright (C) 2005-2011 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
16
17
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
18
"""bzr python plugin support.
19
20
When load_plugins() is invoked, any python module in any directory in
21
$BZR_PLUGIN_PATH will be imported.  The module will be imported as
22
'bzrlib.plugins.$BASENAME(PLUGIN)'.  In the plugin's main body, it should
3620.4.1 by Robert Collins
plugin doc strings update.
23
update any bzrlib registries it wants to extend.
24
25
See the plugin-api developer documentation for information about writing
26
plugins.
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
27
28
BZR_PLUGIN_PATH is also honoured for any plugins imported via
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
29
'import bzrlib.plugins.PLUGINNAME', as long as set_plugins_path has been
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
30
called.
1185.16.83 by mbp at sourcefrog
- notes on testability of plugins
31
"""
32
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
33
import os
1185.16.82 by mbp at sourcefrog
- give a quieter warning if a plugin can't be loaded
34
import sys
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
35
3794.1.1 by Martin Pool
Update osutils imports to fix setup.py on Windows
36
from bzrlib import osutils
37
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
38
from bzrlib.lazy_import import lazy_import
3794.1.1 by Martin Pool
Update osutils imports to fix setup.py on Windows
39
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
40
lazy_import(globals(), """
41
import imp
2256.2.3 by Robert Collins
Review feedback.
42
import re
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
43
import types
1185.16.82 by mbp at sourcefrog
- give a quieter warning if a plugin can't be loaded
44
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
45
from bzrlib import (
3777.6.3 by Marius Kruger
Use bzrlib._format_version_tuple and map as per review from John.
46
    _format_version_tuple,
3224.5.10 by Andrew Bennetts
Replace some duplication with a different form of hackery.
47
    config,
3427.2.2 by James Westby
Just print the exception, keeping the API of log_exception_quietly the same.
48
    debug,
3766.3.2 by Robert Collins
Fix reporting of incompatible api plugin load errors, fixing bug 279451.
49
    errors,
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
50
    trace,
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
51
    )
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
52
from bzrlib import plugins as _mod_plugins
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
53
""")
54
4628.2.5 by Vincent Ladeuil
Fixes prompted by review.
55
from bzrlib.symbol_versioning import (
56
    deprecated_function,
57
    deprecated_in,
58
    )
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
59
60
61
DEFAULT_PLUGIN_PATH = None
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
62
_loaded = False
5017.2.2 by Martin Pool
Add import tariff tests
63
_plugins_disabled = False
64
65
66
def are_plugins_disabled():
67
    return _plugins_disabled
68
1996.3.17 by John Arbash Meinel
lazy_import plugin and transport/local
69
1551.3.11 by Aaron Bentley
Merge from Robert
70
def disable_plugins():
71
    """Disable loading plugins.
72
73
    Future calls to load_plugins() will be ignored.
74
    """
5017.2.2 by Martin Pool
Add import tariff tests
75
    global _plugins_disabled
76
    _plugins_disabled = True
3835.2.5 by Aaron Bentley
Simplify disable_plugins implementation
77
    load_plugins([])
1551.3.11 by Aaron Bentley
Merge from Robert
78
3010.4.1 by Alexander Belchenko
bzr.exe: enable to search system-wide plugins in "plugins" subdirectory of installation directory
79
2753.1.1 by Ian Clatworthy
(Blake Winton) BZR_PLUGIN_PATH should ignore trailiing slashes
80
def _strip_trailing_sep(path):
2652.2.6 by Blake Winton
Incorporate suggestions from Alexander Belchenko
81
    return path.rstrip("\\/")
1551.3.11 by Aaron Bentley
Merge from Robert
82
3010.4.1 by Alexander Belchenko
bzr.exe: enable to search system-wide plugins in "plugins" subdirectory of installation directory
83
5268.5.1 by Vincent Ladeuil
Reproduce bug #591215.
84
def _get_specific_plugin_paths(paths):
85
    """Returns the plugin paths from a string describing the associations.
86
87
    :param paths: A string describing the paths associated with the plugins.
88
89
    :returns: A list of (plugin name, path) tuples.
90
91
    For example, if paths is my_plugin@/test/my-test:her_plugin@/production/her,
92
    [('my_plugin', '/test/my-test'), ('her_plugin', '/production/her')] 
93
    will be returned.
94
95
    Note that ':' in the example above depends on the os.
96
    """
97
    if not paths:
98
        return []
99
    specs = []
100
    for spec in paths.split(os.pathsep):
5268.5.2 by Vincent Ladeuil
Catch the wrong path descriptions in BZR_PLUGINS_AT.
101
        try:
102
            name, path = spec.split('@')
103
        except ValueError:
104
            raise errors.BzrCommandError(
105
                '"%s" is not a valid <plugin_name>@<plugin_path> description '
106
                % spec)
5268.5.1 by Vincent Ladeuil
Reproduce bug #591215.
107
        specs.append((name, path))
108
    return specs
109
110
3835.2.2 by Aaron Bentley
Allow specifying plugin paths, disable co-installed plugins.
111
def set_plugins_path(path=None):
112
    """Set the path for plugins to be loaded from.
113
114
    :param path: The list of paths to search for plugins.  By default,
115
        path will be determined using get_standard_plugins_path.
116
        if path is [], no plugins can be loaded.
117
    """
118
    if path is None:
119
        path = get_standard_plugins_path()
5086.5.5 by Vincent Ladeuil
Use BZR_PLUGINS_AT and stop mucking with BZR_PLUGIN_PATH.
120
    _mod_plugins.__path__ = path
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
121
    PluginImporter.reset()
5086.5.5 by Vincent Ladeuil
Use BZR_PLUGINS_AT and stop mucking with BZR_PLUGIN_PATH.
122
    # Set up a blacklist for disabled plugins
5086.1.10 by Vincent Ladeuil
Fixed as per review comments.
123
    disabled_plugins = os.environ.get('BZR_DISABLE_PLUGINS', None)
124
    if disabled_plugins is not None:
125
        for name in disabled_plugins.split(os.pathsep):
5086.5.4 by Vincent Ladeuil
Merge for fixes from 411413-plugin-path
126
            PluginImporter.blacklist.add('bzrlib.plugins.' + name)
5086.5.5 by Vincent Ladeuil
Use BZR_PLUGINS_AT and stop mucking with BZR_PLUGIN_PATH.
127
    # Set up a the specific paths for plugins
5268.5.1 by Vincent Ladeuil
Reproduce bug #591215.
128
    for plugin_name, plugin_path in _get_specific_plugin_paths(os.environ.get(
129
            'BZR_PLUGINS_AT', None)):
5086.5.5 by Vincent Ladeuil
Use BZR_PLUGINS_AT and stop mucking with BZR_PLUGIN_PATH.
130
            PluginImporter.specific_paths[
131
                'bzrlib.plugins.%s' % plugin_name] = plugin_path
3835.2.2 by Aaron Bentley
Allow specifying plugin paths, disable co-installed plugins.
132
    return path
133
134
4628.2.1 by Vincent Ladeuil
Start introducing accessors for plugin paths.
135
def _append_new_path(paths, new_path):
136
    """Append a new path if it set and not already known."""
137
    if new_path is not None and new_path not in paths:
138
        paths.append(new_path)
139
    return paths
140
141
142
def get_core_plugin_path():
143
    core_path = None
144
    bzr_exe = bool(getattr(sys, 'frozen', None))
145
    if bzr_exe:    # expand path for bzr.exe
146
        # We need to use relative path to system-wide plugin
147
        # directory because bzrlib from standalone bzr.exe
148
        # could be imported by another standalone program
149
        # (e.g. bzr-config; or TortoiseBzr/Olive if/when they
150
        # will become standalone exe). [bialix 20071123]
151
        # __file__ typically is
152
        # C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc
153
        # then plugins directory is
154
        # C:\Program Files\Bazaar\plugins
155
        # so relative path is ../../../plugins
156
        core_path = osutils.abspath(osutils.pathjoin(
157
                osutils.dirname(__file__), '../../../plugins'))
158
    else:     # don't look inside library.zip
159
        # search the plugin path before the bzrlib installed dir
160
        core_path = os.path.dirname(_mod_plugins.__file__)
161
    return core_path
162
163
164
def get_site_plugin_path():
165
    """Returns the path for the site installed plugins."""
4628.2.5 by Vincent Ladeuil
Fixes prompted by review.
166
    if sys.platform == 'win32':
167
        # We don't have (yet) a good answer for windows since that is certainly
168
        # related to the way we build the installers. -- vila20090821
169
        return None
4628.2.1 by Vincent Ladeuil
Start introducing accessors for plugin paths.
170
    site_path = None
171
    try:
172
        from distutils.sysconfig import get_python_lib
173
    except ImportError:
174
        # If distutuils is not available, we just don't know where they are
175
        pass
176
    else:
177
        site_path = osutils.pathjoin(get_python_lib(), 'bzrlib', 'plugins')
178
    return site_path
179
180
181
def get_user_plugin_path():
182
    return osutils.pathjoin(config.config_dir(), 'plugins')
183
184
3835.2.2 by Aaron Bentley
Allow specifying plugin paths, disable co-installed plugins.
185
def get_standard_plugins_path():
186
    """Determine a plugin path suitable for general use."""
4628.2.2 by Vincent Ladeuil
Add [+-]{user|core|site} handling in BZR_PLUGIN_PATH.
187
    # Ad-Hoc default: core is not overriden by site but user can overrides both
4628.2.3 by Vincent Ladeuil
Update doc and add NEWS entry.
188
    # The rationale is that:
189
    # - 'site' comes last, because these plugins should always be available and
190
    #   are supposed to be in sync with the bzr installed on site.
191
    # - 'core' comes before 'site' so that running bzr from sources or a user
192
    #   installed version overrides the site version.
193
    # - 'user' comes first, because... user is always right.
194
    # - the above rules clearly defines which plugin version will be loaded if
195
    #   several exist. Yet, it is sometimes desirable to disable some directory
4672.1.1 by Vincent Ladeuil
BZR_PLUGIN_PATH can be used to fully control the plugin directories
196
    #   so that a set of plugins is disabled as once. This can be done via
4628.2.3 by Vincent Ladeuil
Update doc and add NEWS entry.
197
    #   -site, -core, -user.
198
199
    env_paths = os.environ.get('BZR_PLUGIN_PATH', '+user').split(os.pathsep)
200
    defaults = ['+core', '+site']
4628.2.2 by Vincent Ladeuil
Add [+-]{user|core|site} handling in BZR_PLUGIN_PATH.
201
202
    # The predefined references
203
    refs = dict(core=get_core_plugin_path(),
204
                site=get_site_plugin_path(),
205
                user=get_user_plugin_path())
206
4628.2.5 by Vincent Ladeuil
Fixes prompted by review.
207
    # Unset paths that should be removed
208
    for k,v in refs.iteritems():
209
        removed = '-%s' % k
210
        # defaults can never mention removing paths as that will make it
211
        # impossible for the user to revoke these removals.
212
        if removed in env_paths:
213
            env_paths.remove(removed)
214
            refs[k] = None
215
216
    # Expand references
217
    paths = []
218
    for p in env_paths + defaults:
219
        if p.startswith('+'):
5086.1.2 by Vincent Ladeuil
Cosmetic changes.
220
            # Resolve references if they are known
4628.2.5 by Vincent Ladeuil
Fixes prompted by review.
221
            try:
222
                p = refs[p[1:]]
223
            except KeyError:
5086.1.6 by Vincent Ladeuil
Crude fix for bug #411413.
224
                # Leave them untouched so user can still use paths starting
225
                # with '+'
4628.2.5 by Vincent Ladeuil
Fixes prompted by review.
226
                pass
227
        _append_new_path(paths, p)
4628.2.2 by Vincent Ladeuil
Add [+-]{user|core|site} handling in BZR_PLUGIN_PATH.
228
3835.2.7 by Aaron Bentley
Add tests for plugins
229
    # Get rid of trailing slashes, since Python can't handle them when
230
    # it tries to import modules.
4628.2.2 by Vincent Ladeuil
Add [+-]{user|core|site} handling in BZR_PLUGIN_PATH.
231
    paths = map(_strip_trailing_sep, paths)
232
    return paths
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
233
234
3835.2.4 by Aaron Bentley
Allow paths to be specified to load_plugins
235
def load_plugins(path=None):
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
236
    """Load bzrlib plugins.
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
237
238
    The environment variable BZR_PLUGIN_PATH is considered a delimited
239
    set of paths to look through. Each entry is searched for *.py
240
    files (and whatever other extensions are used in the platform,
241
    such as *.pyd).
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
242
5086.1.6 by Vincent Ladeuil
Crude fix for bug #411413.
243
    load_from_path() provides the underlying mechanism and is called with
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
244
    the default directory list to provide the normal behaviour.
3835.2.4 by Aaron Bentley
Allow paths to be specified to load_plugins
245
246
    :param path: The list of paths to search for plugins.  By default,
247
        path will be determined using get_standard_plugins_path.
248
        if path is [], no plugins can be loaded.
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
249
    """
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
250
    global _loaded
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
251
    if _loaded:
252
        # People can make sure plugins are loaded, they just won't be twice
253
        return
254
    _loaded = True
255
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
256
    # scan for all plugins in the path.
3835.2.4 by Aaron Bentley
Allow paths to be specified to load_plugins
257
    load_from_path(set_plugins_path(path))
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
258
259
260
def load_from_path(dirs):
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
261
    """Load bzrlib plugins found in each dir in dirs.
262
263
    Loading a plugin means importing it into the python interpreter.
264
    The plugin is expected to make calls to register commands when
265
    it's loaded (or perhaps access other hooks in future.)
266
267
    Plugins are loaded into bzrlib.plugins.NAME, and can be found there
268
    for future reference.
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
269
270
    The python module path for bzrlib.plugins will be modified to be 'dirs'.
2652.2.5 by Blake Winton
Get rid of CRs.
271
    """
5086.5.10 by Vincent Ladeuil
Cleanup docs.
272
    # Explicitly load the plugins with a specific path
5086.5.8 by Vincent Ladeuil
Make sure we can load from a non-standard directory name.
273
    for fullname, path in PluginImporter.specific_paths.iteritems():
274
        name = fullname[len('bzrlib.plugins.'):]
5086.5.10 by Vincent Ladeuil
Cleanup docs.
275
        _load_plugin_module(name, path)
5086.5.8 by Vincent Ladeuil
Make sure we can load from a non-standard directory name.
276
2652.2.5 by Blake Winton
Get rid of CRs.
277
    # We need to strip the trailing separators here as well as in the
278
    # set_plugins_path function because calling code can pass anything in to
279
    # this function, and since it sets plugins.__path__, it should set it to
280
    # something that will be valid for Python to use (in case people try to
2652.2.4 by Blake Winton
Add a note explaining why I strip the slashes twice.
281
    # run "import bzrlib.plugins.PLUGINNAME" after calling this function).
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
282
    _mod_plugins.__path__ = map(_strip_trailing_sep, dirs)
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
283
    for d in dirs:
284
        if not d:
285
            continue
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
286
        trace.mutter('looking for plugins in %s', d)
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
287
        if os.path.isdir(d):
288
            load_from_dir(d)
289
290
291
# backwards compatability: load_from_dirs was the old name
292
# This was changed in 0.15
293
load_from_dirs = load_from_path
294
295
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
296
def _find_plugin_module(dir, name):
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
297
    """Check if there is a valid python module that can be loaded as a plugin.
298
5086.5.10 by Vincent Ladeuil
Cleanup docs.
299
    :param dir: The directory where the search is performed.
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
300
    :param path: An existing file path, either a python file or a package
301
        directory.
302
303
    :return: (name, path, description) name is the module name, path is the
304
        file to load and description is the tuple returned by
305
        imp.get_suffixes().
306
    """
307
    path = osutils.pathjoin(dir, name)
308
    if os.path.isdir(path):
309
        # Check for a valid __init__.py file, valid suffixes depends on -O and
310
        # can be .py, .pyc and .pyo
311
        for suffix, mode, kind in imp.get_suffixes():
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
312
            if kind not in (imp.PY_SOURCE, imp.PY_COMPILED):
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
313
                # We don't recognize compiled modules (.so, .dll, etc)
314
                continue
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
315
            init_path = osutils.pathjoin(path, '__init__' + suffix)
316
            if os.path.isfile(init_path):
317
                return name, init_path, (suffix, mode, kind)
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
318
    else:
319
        for suffix, mode, kind in imp.get_suffixes():
320
            if name.endswith(suffix):
321
                # Clean up the module name
322
                name = name[:-len(suffix)]
323
                if kind == imp.C_EXTENSION and name.endswith('module'):
324
                    name = name[:-len('module')]
325
                return name, path, (suffix, mode, kind)
326
    # There is no python module here
327
    return None, None, (None, None, None)
328
329
5086.5.8 by Vincent Ladeuil
Make sure we can load from a non-standard directory name.
330
def _load_plugin_module(name, dir):
5086.5.14 by Vincent Ladeuil
Fix bug #552922 by controlling which files can be used to load a plugin.
331
    """Load plugin name from dir.
5086.5.10 by Vincent Ladeuil
Cleanup docs.
332
333
    :param name: The plugin name in the bzrlib.plugins namespace.
334
    :param dir: The directory the plugin is loaded from for error messages.
335
    """
5086.5.8 by Vincent Ladeuil
Make sure we can load from a non-standard directory name.
336
    if ('bzrlib.plugins.%s' % name) in PluginImporter.blacklist:
337
        return
338
    try:
339
        exec "import bzrlib.plugins.%s" % name in {}
340
    except KeyboardInterrupt:
341
        raise
342
    except errors.IncompatibleAPI, e:
343
        trace.warning("Unable to load plugin %r. It requested API version "
344
            "%s of module %s but the minimum exported version is %s, and "
345
            "the maximum is %s" %
346
            (name, e.wanted, e.api, e.minimum, e.current))
347
    except Exception, e:
348
        trace.warning("%s" % e)
349
        if re.search('\.|-| ', name):
350
            sanitised_name = re.sub('[-. ]', '_', name)
351
            if sanitised_name.startswith('bzr_'):
352
                sanitised_name = sanitised_name[len('bzr_'):]
353
            trace.warning("Unable to load %r in %r as a plugin because the "
354
                    "file path isn't a valid module name; try renaming "
355
                    "it to %r." % (name, dir, sanitised_name))
356
        else:
357
            trace.warning('Unable to load plugin %r from %r' % (name, dir))
358
        trace.log_exception_quietly()
359
        if 'error' in debug.debug_flags:
360
            trace.print_exception(sys.exc_info(), sys.stderr)
361
362
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
363
def load_from_dir(d):
3766.3.2 by Robert Collins
Fix reporting of incompatible api plugin load errors, fixing bug 279451.
364
    """Load the plugins in directory d.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
365
3766.3.2 by Robert Collins
Fix reporting of incompatible api plugin load errors, fixing bug 279451.
366
    d must be in the plugins module path already.
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
367
    This function is called once for each directory in the module path.
3766.3.2 by Robert Collins
Fix reporting of incompatible api plugin load errors, fixing bug 279451.
368
    """
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
369
    plugin_names = set()
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
370
    for p in os.listdir(d):
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
371
        name, path, desc = _find_plugin_module(d, p)
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
372
        if name is not None:
373
            if name == '__init__':
374
                # We do nothing with the __init__.py file in directories from
375
                # the bzrlib.plugins module path, we may want to, one day
376
                # -- vila 20100316.
377
                continue # We don't load __init__.py in the plugins dirs
378
            elif getattr(_mod_plugins, name, None) is not None:
379
                # The module has already been loaded from another directory
380
                # during a previous call.
381
                # FIXME: There should be a better way to report masked plugins
382
                # -- vila 20100316
383
                trace.mutter('Plugin name %s already loaded', name)
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
384
            else:
5086.5.1 by Vincent Ladeuil
Slight refactoring preparing fix for bug #82693.
385
                plugin_names.add(name)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
386
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
387
    for name in plugin_names:
5086.5.8 by Vincent Ladeuil
Make sure we can load from a non-standard directory name.
388
        _load_plugin_module(name, d)
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
389
390
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
391
def plugins():
392
    """Return a dictionary of the plugins.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
393
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
394
    Each item in the dictionary is a PlugIn object.
395
    """
396
    result = {}
397
    for name, plugin in _mod_plugins.__dict__.items():
398
        if isinstance(plugin, types.ModuleType):
399
            result[name] = PlugIn(name, plugin)
400
    return result
401
402
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
403
def format_concise_plugin_list():
404
    """Return a string holding a concise list of plugins and their version.
405
    """
406
    items = []
407
    for name, a_plugin in sorted(plugins().items()):
408
        items.append("%s[%s]" %
409
            (name, a_plugin.__version__))
410
    return ', '.join(items)
411
412
413
2432.1.24 by Robert Collins
Add plugins as a help index.
414
class PluginsHelpIndex(object):
415
    """A help index that returns help topics for plugins."""
416
417
    def __init__(self):
418
        self.prefix = 'plugins/'
419
420
    def get_topics(self, topic):
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
421
        """Search for topic in the loaded plugins.
422
423
        This will not trigger loading of new plugins.
424
425
        :param topic: A topic to search for.
426
        :return: A list which is either empty or contains a single
427
            RegisteredTopic entry.
428
        """
429
        if not topic:
430
            return []
431
        if topic.startswith(self.prefix):
432
            topic = topic[len(self.prefix):]
433
        plugin_module_name = 'bzrlib.plugins.%s' % topic
434
        try:
435
            module = sys.modules[plugin_module_name]
436
        except KeyError:
437
            return []
438
        else:
439
            return [ModuleHelpTopic(module)]
440
441
442
class ModuleHelpTopic(object):
443
    """A help topic which returns the docstring for a module."""
444
445
    def __init__(self, module):
446
        """Constructor.
447
448
        :param module: The module for which help should be generated.
449
        """
450
        self.module = module
451
3984.4.5 by Ian Clatworthy
help xxx is full help; xxx -h is concise help
452
    def get_help_text(self, additional_see_also=None, verbose=True):
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
453
        """Return a string with the help for this topic.
454
455
        :param additional_see_also: Additional help topics to be
456
            cross-referenced.
457
        """
458
        if not self.module.__doc__:
459
            result = "Plugin '%s' has no docstring.\n" % self.module.__name__
460
        else:
461
            result = self.module.__doc__
462
        if result[-1] != '\n':
463
            result += '\n'
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
464
        # there is code duplicated here and in bzrlib/help_topic.py's
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
465
        # matching Topic code. This should probably be factored in
466
        # to a helper function and a common base class.
467
        if additional_see_also is not None:
468
            see_also = sorted(set(additional_see_also))
469
        else:
470
            see_also = None
471
        if see_also:
472
            result += 'See also: '
473
            result += ', '.join(see_also)
474
            result += '\n'
475
        return result
2432.1.29 by Robert Collins
Add get_help_topic to ModuleHelpTopic.
476
477
    def get_help_topic(self):
2432.1.30 by Robert Collins
Fix the ModuleHelpTopic get_help_topic to be tested with closer to real world data and strip the bzrlib.plugins. prefix from the name.
478
        """Return the modules help topic - its __name__ after bzrlib.plugins.."""
479
        return self.module.__name__[len('bzrlib.plugins.'):]
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
480
481
482
class PlugIn(object):
483
    """The bzrlib representation of a plugin.
484
485
    The PlugIn object provides a way to manipulate a given plugin module.
486
    """
487
488
    def __init__(self, name, module):
489
        """Construct a plugin for module."""
490
        self.name = name
491
        self.module = module
492
493
    def path(self):
494
        """Get the path that this plugin was loaded from."""
495
        if getattr(self.module, '__path__', None) is not None:
496
            return os.path.abspath(self.module.__path__[0])
497
        elif getattr(self.module, '__file__', None) is not None:
3193.2.1 by Alexander Belchenko
show path to plugin module as *.py instead of *.pyc if python source available
498
            path = os.path.abspath(self.module.__file__)
499
            if path[-4:] in ('.pyc', '.pyo'):
500
                pypath = path[:-4] + '.py'
501
                if os.path.isfile(pypath):
502
                    path = pypath
503
            return path
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
504
        else:
505
            return repr(self.module)
506
507
    def __str__(self):
508
        return "<%s.%s object at %s, name=%s, module=%s>" % (
509
            self.__class__.__module__, self.__class__.__name__, id(self),
510
            self.name, self.module)
511
512
    __repr__ = __str__
513
514
    def test_suite(self):
515
        """Return the plugin's test suite."""
516
        if getattr(self.module, 'test_suite', None) is not None:
517
            return self.module.test_suite()
518
        else:
519
            return None
520
3302.8.21 by Vincent Ladeuil
Fixed as per Robert's review.
521
    def load_plugin_tests(self, loader):
3302.8.10 by Vincent Ladeuil
Prepare bzrlib.plugin to use the new test loader.
522
        """Return the adapted plugin's test suite.
523
524
        :param loader: The custom loader that should be used to load additional
525
            tests.
526
527
        """
528
        if getattr(self.module, 'load_tests', None) is not None:
3302.8.11 by Vincent Ladeuil
Simplify plugin.load_tests.
529
            return loader.loadTestsFromModule(self.module)
3302.8.10 by Vincent Ladeuil
Prepare bzrlib.plugin to use the new test loader.
530
        else:
531
            return None
532
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
533
    def version_info(self):
534
        """Return the plugin's version_tuple or None if unknown."""
535
        version_info = getattr(self.module, 'version_info', None)
3777.6.7 by Marius Kruger
* Can now also handle non-iteratable and string plugin versions.
536
        if version_info is not None:
537
            try:
538
                if isinstance(version_info, types.StringType):
539
                    version_info = version_info.split('.')
540
                elif len(version_info) == 3:
541
                    version_info = tuple(version_info) + ('final', 0)
542
            except TypeError, e:
543
                # The given version_info isn't even iteratible
544
                trace.log_exception_quietly()
545
                version_info = (version_info,)
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
546
        return version_info
3302.8.10 by Vincent Ladeuil
Prepare bzrlib.plugin to use the new test loader.
547
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
548
    def _get__version__(self):
549
        version_info = self.version_info()
3777.6.1 by Marius Kruger
Try to return something usefull for plugins with bad version numbers,
550
        if version_info is None or len(version_info) == 0:
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
551
            return "unknown"
3777.6.1 by Marius Kruger
Try to return something usefull for plugins with bad version numbers,
552
        try:
3777.6.3 by Marius Kruger
Use bzrlib._format_version_tuple and map as per review from John.
553
            version_string = _format_version_tuple(version_info)
3777.6.6 by Marius Kruger
catch only ValueError, TypeError, IndexError as per feedback from John
554
        except (ValueError, TypeError, IndexError), e:
555
            trace.log_exception_quietly()
3777.6.1 by Marius Kruger
Try to return something usefull for plugins with bad version numbers,
556
            # try to return something usefull for bad plugins, in stead of
557
            # stack tracing.
3777.6.3 by Marius Kruger
Use bzrlib._format_version_tuple and map as per review from John.
558
            version_string = '.'.join(map(str, version_info))
2762.2.1 by Robert Collins
* ``bzr plugins`` now lists the version number for each plugin in square
559
        return version_string
560
561
    __version__ = property(_get__version__)
5086.1.6 by Vincent Ladeuil
Crude fix for bug #411413.
562
563
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
564
class _PluginImporter(object):
5086.5.10 by Vincent Ladeuil
Cleanup docs.
565
    """An importer tailored to bzr specific needs.
566
567
    This is a singleton that takes care of:
568
    - disabled plugins specified in 'blacklist',
569
    - plugins that needs to be loaded from specific directories.
570
    """
5086.1.7 by Vincent Ladeuil
Cleaner fix for bug #411413.
571
572
    def __init__(self):
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
573
        self.reset()
574
575
    def reset(self):
576
        self.blacklist = set()
577
        self.specific_paths = {}
5086.1.6 by Vincent Ladeuil
Crude fix for bug #411413.
578
579
    def find_module(self, fullname, parent_path=None):
5086.5.10 by Vincent Ladeuil
Cleanup docs.
580
        """Search a plugin module.
581
582
        Disabled plugins raise an import error, plugins with specific paths
583
        returns a specific loader.
584
585
        :return: None if the plugin doesn't need special handling, self
586
            otherwise.
587
        """
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
588
        if not fullname.startswith('bzrlib.plugins.'):
589
            return None
5086.1.7 by Vincent Ladeuil
Cleaner fix for bug #411413.
590
        if fullname in self.blacklist:
5086.1.6 by Vincent Ladeuil
Crude fix for bug #411413.
591
            raise ImportError('%s is disabled' % fullname)
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
592
        if fullname in self.specific_paths:
593
            return self
5086.1.6 by Vincent Ladeuil
Crude fix for bug #411413.
594
        return None
5086.1.7 by Vincent Ladeuil
Cleaner fix for bug #411413.
595
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
596
    def load_module(self, fullname):
5086.5.10 by Vincent Ladeuil
Cleanup docs.
597
        """Load a plugin from a specific directory."""
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
598
        # We are called only for specific paths
5086.5.14 by Vincent Ladeuil
Fix bug #552922 by controlling which files can be used to load a plugin.
599
        plugin_path = self.specific_paths[fullname]
600
        loading_path = None
601
        if os.path.isdir(plugin_path):
602
            for suffix, mode, kind in imp.get_suffixes():
603
                if kind not in (imp.PY_SOURCE, imp.PY_COMPILED):
604
                    # We don't recognize compiled modules (.so, .dll, etc)
605
                    continue
606
                init_path = osutils.pathjoin(plugin_path, '__init__' + suffix)
607
                if os.path.isfile(init_path):
5268.6.3 by Vincent Ladeuil
BZR_PLUGINS_AT should use packages properly to handle relative imports.
608
                    # We've got a module here and load_module needs specific
609
                    # parameters.
610
                    loading_path = plugin_path
611
                    suffix = ''
612
                    mode = ''
613
                    kind = imp.PKG_DIRECTORY
5086.5.14 by Vincent Ladeuil
Fix bug #552922 by controlling which files can be used to load a plugin.
614
                    break
615
        else:
616
            for suffix, mode, kind in imp.get_suffixes():
617
                if plugin_path.endswith(suffix):
618
                    loading_path = plugin_path
619
                    break
620
        if loading_path is None:
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
621
            raise ImportError('%s cannot be loaded from %s'
5086.5.14 by Vincent Ladeuil
Fix bug #552922 by controlling which files can be used to load a plugin.
622
                              % (fullname, plugin_path))
5268.6.3 by Vincent Ladeuil
BZR_PLUGINS_AT should use packages properly to handle relative imports.
623
        if kind is imp.PKG_DIRECTORY:
624
            f = None
625
        else:
626
            f = open(loading_path, mode)
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
627
        try:
5086.5.14 by Vincent Ladeuil
Fix bug #552922 by controlling which files can be used to load a plugin.
628
            mod = imp.load_module(fullname, f, loading_path,
629
                                  (suffix, mode, kind))
5086.5.12 by Vincent Ladeuil
Force __package__ to fix pqm failure.
630
            mod.__package__ = fullname
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
631
            return mod
632
        finally:
5268.6.3 by Vincent Ladeuil
BZR_PLUGINS_AT should use packages properly to handle relative imports.
633
            if f is not None:
634
                f.close()
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
635
636
5086.5.10 by Vincent Ladeuil
Cleanup docs.
637
# Install a dedicated importer for plugins requiring special handling
5086.5.3 by Vincent Ladeuil
First shot at loading plugins from a specific directory.
638
PluginImporter = _PluginImporter()
639
sys.meta_path.append(PluginImporter)