~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugin.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-11 09:27:55 UTC
  • mfrom: (5017.3.46 test-servers)
  • mto: This revision was merged to the branch mainline in revision 5030.
  • Revision ID: v.ladeuil+lp@free.fr-20100211092755-3vvu4vbwiwjjte3s
Move tests servers from bzrlib.transport to bzrlib.tests.test_server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2004, 2005, 2007, 2008, 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
52
52
from bzrlib import plugins as _mod_plugins
53
53
""")
54
54
 
55
 
from bzrlib.symbol_versioning import deprecated_function
 
55
from bzrlib.symbol_versioning import (
 
56
    deprecated_function,
 
57
    deprecated_in,
 
58
    )
56
59
 
57
60
 
58
61
DEFAULT_PLUGIN_PATH = None
59
62
_loaded = False
60
 
 
 
63
_plugins_disabled = False
 
64
 
 
65
 
 
66
def are_plugins_disabled():
 
67
    return _plugins_disabled
 
68
 
 
69
 
 
70
@deprecated_function(deprecated_in((2, 0, 0)))
61
71
def get_default_plugin_path():
62
72
    """Get the DEFAULT_PLUGIN_PATH"""
63
73
    global DEFAULT_PLUGIN_PATH
71
81
 
72
82
    Future calls to load_plugins() will be ignored.
73
83
    """
 
84
    global _plugins_disabled
 
85
    _plugins_disabled = True
74
86
    load_plugins([])
75
87
 
76
88
 
91
103
    return path
92
104
 
93
105
 
94
 
def get_standard_plugins_path():
95
 
    """Determine a plugin path suitable for general use."""
96
 
    path = os.environ.get('BZR_PLUGIN_PATH',
97
 
                          get_default_plugin_path()).split(os.pathsep)
98
 
    # Get rid of trailing slashes, since Python can't handle them when
99
 
    # it tries to import modules.
100
 
    path = map(_strip_trailing_sep, path)
 
106
def _append_new_path(paths, new_path):
 
107
    """Append a new path if it set and not already known."""
 
108
    if new_path is not None and new_path not in paths:
 
109
        paths.append(new_path)
 
110
    return paths
 
111
 
 
112
 
 
113
def get_core_plugin_path():
 
114
    core_path = None
101
115
    bzr_exe = bool(getattr(sys, 'frozen', None))
102
116
    if bzr_exe:    # expand path for bzr.exe
103
117
        # We need to use relative path to system-wide plugin
110
124
        # then plugins directory is
111
125
        # C:\Program Files\Bazaar\plugins
112
126
        # so relative path is ../../../plugins
113
 
        path.append(osutils.abspath(osutils.pathjoin(
114
 
            osutils.dirname(__file__), '../../../plugins')))
115
 
    if not bzr_exe:     # don't look inside library.zip
 
127
        core_path = osutils.abspath(osutils.pathjoin(
 
128
                osutils.dirname(__file__), '../../../plugins'))
 
129
    else:     # don't look inside library.zip
116
130
        # search the plugin path before the bzrlib installed dir
117
 
        path.append(os.path.dirname(_mod_plugins.__file__))
118
 
    # search the arch independent path if we can determine that and
119
 
    # the plugin is found nowhere else
120
 
    if sys.platform != 'win32':
121
 
        try:
122
 
            from distutils.sysconfig import get_python_lib
123
 
        except ImportError:
124
 
            # If distutuils is not available, we just won't add that path
125
 
            pass
126
 
        else:
127
 
            archless_path = osutils.pathjoin(get_python_lib(), 'bzrlib',
128
 
                    'plugins')
129
 
            if archless_path not in path:
130
 
                path.append(archless_path)
131
 
    return path
 
131
        core_path = os.path.dirname(_mod_plugins.__file__)
 
132
    return core_path
 
133
 
 
134
 
 
135
def get_site_plugin_path():
 
136
    """Returns the path for the site installed plugins."""
 
137
    if sys.platform == 'win32':
 
138
        # We don't have (yet) a good answer for windows since that is certainly
 
139
        # related to the way we build the installers. -- vila20090821
 
140
        return None
 
141
    site_path = None
 
142
    try:
 
143
        from distutils.sysconfig import get_python_lib
 
144
    except ImportError:
 
145
        # If distutuils is not available, we just don't know where they are
 
146
        pass
 
147
    else:
 
148
        site_path = osutils.pathjoin(get_python_lib(), 'bzrlib', 'plugins')
 
149
    return site_path
 
150
 
 
151
 
 
152
def get_user_plugin_path():
 
153
    return osutils.pathjoin(config.config_dir(), 'plugins')
 
154
 
 
155
 
 
156
def get_standard_plugins_path():
 
157
    """Determine a plugin path suitable for general use."""
 
158
    # Ad-Hoc default: core is not overriden by site but user can overrides both
 
159
    # The rationale is that:
 
160
    # - 'site' comes last, because these plugins should always be available and
 
161
    #   are supposed to be in sync with the bzr installed on site.
 
162
    # - 'core' comes before 'site' so that running bzr from sources or a user
 
163
    #   installed version overrides the site version.
 
164
    # - 'user' comes first, because... user is always right.
 
165
    # - the above rules clearly defines which plugin version will be loaded if
 
166
    #   several exist. Yet, it is sometimes desirable to disable some directory
 
167
    #   so that a set of plugins is disabled as once. This can be done via
 
168
    #   -site, -core, -user.
 
169
 
 
170
    env_paths = os.environ.get('BZR_PLUGIN_PATH', '+user').split(os.pathsep)
 
171
    defaults = ['+core', '+site']
 
172
 
 
173
    # The predefined references
 
174
    refs = dict(core=get_core_plugin_path(),
 
175
                site=get_site_plugin_path(),
 
176
                user=get_user_plugin_path())
 
177
 
 
178
    # Unset paths that should be removed
 
179
    for k,v in refs.iteritems():
 
180
        removed = '-%s' % k
 
181
        # defaults can never mention removing paths as that will make it
 
182
        # impossible for the user to revoke these removals.
 
183
        if removed in env_paths:
 
184
            env_paths.remove(removed)
 
185
            refs[k] = None
 
186
 
 
187
    # Expand references
 
188
    paths = []
 
189
    for p in env_paths + defaults:
 
190
        if p.startswith('+'):
 
191
            # Resolve reference if they are known
 
192
            try:
 
193
                p = refs[p[1:]]
 
194
            except KeyError:
 
195
                # Leave them untouched otherwise, user may have paths starting
 
196
                # with '+'...
 
197
                pass
 
198
        _append_new_path(paths, p)
 
199
 
 
200
    # Get rid of trailing slashes, since Python can't handle them when
 
201
    # it tries to import modules.
 
202
    paths = map(_strip_trailing_sep, paths)
 
203
    return paths
132
204
 
133
205
 
134
206
def load_plugins(path=None):