/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: Andrew Bennetts
  • Date: 2008-03-27 06:10:18 UTC
  • mfrom: (3309 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3320.
  • Revision ID: andrew.bennetts@canonical.com-20080327061018-dxztpxyv6yoeg3am
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
from bzrlib import plugins as _mod_plugins
47
47
""")
48
48
 
49
 
from bzrlib.symbol_versioning import deprecated_function, zero_ninetyone
 
49
from bzrlib.symbol_versioning import deprecated_function, one_three
50
50
from bzrlib.trace import mutter, warning, log_exception_quietly
51
51
 
52
52
 
57
57
    """Get the DEFAULT_PLUGIN_PATH"""
58
58
    global DEFAULT_PLUGIN_PATH
59
59
    if DEFAULT_PLUGIN_PATH is None:
60
 
        path = [osutils.pathjoin(config.config_dir(), 'plugins')]
61
 
        if getattr(sys, 'frozen', None):    # bzr.exe
62
 
            # We need to use relative path to system-wide plugin
63
 
            # directory because bzrlib from standalone bzr.exe
64
 
            # could be imported by another standalone program
65
 
            # (e.g. bzr-config; or TortoiseBzr/Olive if/when they
66
 
            # will become standalone exe). [bialix 20071123]
67
 
            # __file__ typically is
68
 
            # C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc
69
 
            # then plugins directory is
70
 
            # C:\Program Files\Bazaar\plugins
71
 
            # so relative path is ../../../plugins
72
 
            path.append(osutils.abspath(osutils.pathjoin(
73
 
                osutils.dirname(__file__), '../../../plugins')))
74
 
        DEFAULT_PLUGIN_PATH = os.pathsep.join(path)
 
60
        DEFAULT_PLUGIN_PATH = osutils.pathjoin(config.config_dir(), 'plugins')
75
61
    return DEFAULT_PLUGIN_PATH
76
62
 
77
63
 
78
 
@deprecated_function(zero_ninetyone)
79
 
def all_plugins():
80
 
    """Return a dictionary of the plugins."""
81
 
    return dict((name, plugin.module) for name, plugin in plugins().items())
82
 
 
83
 
 
84
64
def disable_plugins():
85
65
    """Disable loading plugins.
86
66
 
100
80
    """Set the path for plugins to be loaded from."""
101
81
    path = os.environ.get('BZR_PLUGIN_PATH',
102
82
                          get_default_plugin_path()).split(os.pathsep)
 
83
    bzr_exe = bool(getattr(sys, 'frozen', None))
 
84
    if bzr_exe:    # expand path for bzr.exe
 
85
        # We need to use relative path to system-wide plugin
 
86
        # directory because bzrlib from standalone bzr.exe
 
87
        # could be imported by another standalone program
 
88
        # (e.g. bzr-config; or TortoiseBzr/Olive if/when they
 
89
        # will become standalone exe). [bialix 20071123]
 
90
        # __file__ typically is
 
91
        # C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc
 
92
        # then plugins directory is
 
93
        # C:\Program Files\Bazaar\plugins
 
94
        # so relative path is ../../../plugins
 
95
        path.append(osutils.abspath(osutils.pathjoin(
 
96
            osutils.dirname(__file__), '../../../plugins')))
103
97
    # Get rid of trailing slashes, since Python can't handle them when
104
98
    # it tries to import modules.
105
99
    path = map(_strip_trailing_sep, path)
106
 
    # search the plugin path before the bzrlib installed dir
107
 
    path.append(os.path.dirname(_mod_plugins.__file__))
 
100
    if not bzr_exe:     # don't look inside library.zip
 
101
        # search the plugin path before the bzrlib installed dir
 
102
        path.append(os.path.dirname(_mod_plugins.__file__))
108
103
    _mod_plugins.__path__ = path
109
104
    return path
110
105
 
154
149
        mutter('looking for plugins in %s', d)
155
150
        if os.path.isdir(d):
156
151
            load_from_dir(d)
157
 
        else:
158
 
            # it might be a zip: try loading from the zip.
159
 
            load_from_zip(d)
160
152
 
161
153
 
162
154
# backwards compatability: load_from_dirs was the old name
207
199
            ## import pdb; pdb.set_trace()
208
200
            if re.search('\.|-| ', name):
209
201
                sanitised_name = re.sub('[-. ]', '_', name)
210
 
                warning("Unable to load %r in %r as a plugin because file path"
211
 
                        " isn't a valid module name; try renaming it to %r."
212
 
                        % (name, d, sanitised_name))
 
202
                if sanitised_name.startswith('bzr_'):
 
203
                    sanitised_name = sanitised_name[len('bzr_'):]
 
204
                warning("Unable to load %r in %r as a plugin because the "
 
205
                        "file path isn't a valid module name; try renaming "
 
206
                        "it to %r." % (name, d, sanitised_name))
213
207
            else:
214
208
                warning('Unable to load plugin %r from %r' % (name, d))
215
209
            log_exception_quietly()
216
210
 
217
211
 
 
212
@deprecated_function(one_three)
218
213
def load_from_zip(zip_name):
219
214
    """Load all the plugins in a zip."""
220
215
    valid_suffixes = ('.py', '.pyc', '.pyo')    # only python modules/packages
221
216
                                                # is allowed
222
 
 
223
217
    try:
224
218
        index = zip_name.rindex('.zip')
225
219
    except ValueError:
390
384
        if getattr(self.module, '__path__', None) is not None:
391
385
            return os.path.abspath(self.module.__path__[0])
392
386
        elif getattr(self.module, '__file__', None) is not None:
393
 
            return os.path.abspath(self.module.__file__)
 
387
            path = os.path.abspath(self.module.__file__)
 
388
            if path[-4:] in ('.pyc', '.pyo'):
 
389
                pypath = path[:-4] + '.py'
 
390
                if os.path.isfile(pypath):
 
391
                    path = pypath
 
392
            return path
394
393
        else:
395
394
            return repr(self.module)
396
395