/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

[merge] bzr.dev 2255, resolve conflicts, update copyrights

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 by Canonical Ltd
 
1
# Copyright (C) 2004, 2005 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
39
39
# That could be either a separate Python interpreter or perhaps a new
40
40
# namespace inside this interpreter.
41
41
 
42
 
import imp
43
42
import os
44
43
import sys
 
44
 
 
45
from bzrlib.lazy_import import lazy_import
 
46
lazy_import(globals(), """
 
47
import imp
45
48
import types
46
49
 
47
 
import bzrlib
48
 
from bzrlib.config import config_dir
49
 
from bzrlib.trace import log_error, mutter, warning, \
50
 
        log_exception_quietly
51
 
from bzrlib.errors import BzrError
52
 
from bzrlib import plugins
53
 
from bzrlib.osutils import pathjoin
54
 
 
55
 
DEFAULT_PLUGIN_PATH = pathjoin(config_dir(), 'plugins')
 
50
from bzrlib import (
 
51
    config,
 
52
    osutils,
 
53
    plugins,
 
54
    )
 
55
""")
 
56
 
 
57
from bzrlib.trace import mutter, warning, log_exception_quietly
 
58
 
 
59
 
 
60
DEFAULT_PLUGIN_PATH = None
 
61
 
 
62
 
 
63
def get_default_plugin_path():
 
64
    """Get the DEFAULT_PLUGIN_PATH"""
 
65
    global DEFAULT_PLUGIN_PATH
 
66
    if DEFAULT_PLUGIN_PATH is None:
 
67
        DEFAULT_PLUGIN_PATH = osutils.pathjoin(config.config_dir(), 'plugins')
 
68
    return DEFAULT_PLUGIN_PATH
 
69
 
56
70
 
57
71
_loaded = False
58
72
 
60
74
def all_plugins():
61
75
    """Return a dictionary of the plugins."""
62
76
    result = {}
63
 
    for name, plugin in bzrlib.plugins.__dict__.items():
 
77
    for name, plugin in plugins.__dict__.items():
64
78
        if isinstance(plugin, types.ModuleType):
65
79
            result[name] = plugin
66
80
    return result
92
106
    if _loaded:
93
107
        # People can make sure plugins are loaded, they just won't be twice
94
108
        return
95
 
        #raise BzrError("plugins already initialized")
96
109
    _loaded = True
97
110
 
98
 
    dirs = os.environ.get('BZR_PLUGIN_PATH', DEFAULT_PLUGIN_PATH).split(os.pathsep)
 
111
    dirs = os.environ.get('BZR_PLUGIN_PATH',
 
112
                          get_default_plugin_path()).split(os.pathsep)
99
113
    dirs.insert(0, os.path.dirname(plugins.__file__))
100
114
 
101
115
    load_from_dirs(dirs)
 
116
    load_from_zips(dirs)
102
117
 
103
118
 
104
119
def load_from_dirs(dirs):
125
140
        if not os.path.isdir(d):
126
141
            continue
127
142
        for f in os.listdir(d):
128
 
            path = pathjoin(d, f)
 
143
            path = osutils.pathjoin(d, f)
129
144
            if os.path.isdir(path):
130
145
                for entry in package_entries:
131
146
                    # This directory should be a package, and thus added to
132
147
                    # the list
133
 
                    if os.path.isfile(pathjoin(path, entry)):
 
148
                    if os.path.isfile(osutils.pathjoin(path, entry)):
134
149
                        break
135
150
                else: # This directory is not a package
136
151
                    continue
143
158
                        break
144
159
                else:
145
160
                    continue
146
 
            if getattr(bzrlib.plugins, f, None):
 
161
            if getattr(plugins, f, None):
147
162
                mutter('Plugin name %s already loaded', f)
148
163
            else:
149
 
                mutter('add plugin name %s', f)
 
164
                # mutter('add plugin name %s', f)
150
165
                plugin_names.add(f)
151
166
 
152
167
        plugin_names = list(plugin_names)
154
169
        for name in plugin_names:
155
170
            try:
156
171
                plugin_info = imp.find_module(name, [d])
157
 
                mutter('load plugin %r', plugin_info)
 
172
                # mutter('load plugin %r', plugin_info)
158
173
                try:
159
174
                    plugin = imp.load_module('bzrlib.plugins.' + name,
160
175
                                             *plugin_info)
161
 
                    setattr(bzrlib.plugins, name, plugin)
 
176
                    setattr(plugins, name, plugin)
162
177
                finally:
163
178
                    if plugin_info[0] is not None:
164
179
                        plugin_info[0].close()
165
 
 
166
 
                mutter('loaded succesfully')
 
180
                # mutter('loaded succesfully')
167
181
            except KeyboardInterrupt:
168
182
                raise
169
183
            except Exception, e:
170
184
                ## import pdb; pdb.set_trace()
171
185
                warning('Unable to load plugin %r from %r' % (name, d))
172
186
                log_exception_quietly()
 
187
 
 
188
 
 
189
def load_from_zips(zips):
 
190
    """Load bzr plugins from zip archives with zipimport.
 
191
    It's similar to load_from_dirs but plugins searched inside archives.
 
192
    """
 
193
    import zipfile
 
194
    import zipimport
 
195
 
 
196
    valid_suffixes = ('.py', '.pyc', '.pyo')    # only python modules/packages
 
197
                                                # is allowed
 
198
    for zip_name in zips:
 
199
        if '.zip' not in zip_name:
 
200
            continue
 
201
        try:
 
202
            ziobj = zipimport.zipimporter(zip_name)
 
203
        except zipimport.ZipImportError:
 
204
            # not a valid zip
 
205
            continue
 
206
        mutter('Looking for plugins in %r', zip_name)
 
207
 
 
208
        # use zipfile to get list of files/dirs inside zip
 
209
        z = zipfile.ZipFile(ziobj.archive)
 
210
        namelist = z.namelist()
 
211
        z.close()
 
212
 
 
213
        if ziobj.prefix:
 
214
            prefix = ziobj.prefix.replace('\\','/')
 
215
            ix = len(prefix)
 
216
            namelist = [name[ix:]
 
217
                        for name in namelist
 
218
                        if name.startswith(prefix)]
 
219
 
 
220
        mutter('Names in archive: %r', namelist)
 
221
 
 
222
        for name in namelist:
 
223
            if not name or name.endswith('/'):
 
224
                continue
 
225
 
 
226
            # '/' is used to separate pathname components inside zip archives
 
227
            ix = name.rfind('/')
 
228
            if ix == -1:
 
229
                head, tail = '', name
 
230
            else:
 
231
                head, tail = name.rsplit('/',1)
 
232
            if '/' in head:
 
233
                # we don't need looking in subdirectories
 
234
                continue
 
235
 
 
236
            base, suffix = osutils.splitext(tail)
 
237
            if suffix not in valid_suffixes:
 
238
                continue
 
239
 
 
240
            if base == '__init__':
 
241
                # package
 
242
                plugin_name = head
 
243
            elif head == '':
 
244
                # module
 
245
                plugin_name = base
 
246
            else:
 
247
                continue
 
248
 
 
249
            if not plugin_name:
 
250
                continue
 
251
            if getattr(plugins, plugin_name, None):
 
252
                mutter('Plugin name %s already loaded', plugin_name)
 
253
                continue
 
254
 
 
255
            try:
 
256
                plugin = ziobj.load_module(plugin_name)
 
257
                setattr(plugins, plugin_name, plugin)
 
258
                mutter('Load plugin %s from zip %r', plugin_name, zip_name)
 
259
            except zipimport.ZipImportError, e:
 
260
                mutter('Unable to load plugin %r from %r: %s',
 
261
                       plugin_name, zip_name, str(e))
 
262
                continue
 
263
            except KeyboardInterrupt:
 
264
                raise
 
265
            except Exception, e:
 
266
                ## import pdb; pdb.set_trace()
 
267
                warning('Unable to load plugin %r from %r'
 
268
                        % (name, zip_name))
 
269
                log_exception_quietly()