98
def _append_new_path(paths, new_path):
99
"""Append a new path if it set and not already known."""
100
if new_path is not None and new_path not in paths:
101
paths.append(new_path)
105
def get_core_plugin_path():
95
def get_standard_plugins_path():
96
"""Determine a plugin path suitable for general use."""
97
path = os.environ.get('BZR_PLUGIN_PATH',
98
get_default_plugin_path()).split(os.pathsep)
99
# Get rid of trailing slashes, since Python can't handle them when
100
# it tries to import modules.
101
path = map(_strip_trailing_sep, path)
107
102
bzr_exe = bool(getattr(sys, 'frozen', None))
108
103
if bzr_exe: # expand path for bzr.exe
109
104
# We need to use relative path to system-wide plugin
116
111
# then plugins directory is
117
112
# C:\Program Files\Bazaar\plugins
118
113
# so relative path is ../../../plugins
119
core_path = osutils.abspath(osutils.pathjoin(
120
osutils.dirname(__file__), '../../../plugins'))
121
else: # don't look inside library.zip
114
path.append(osutils.abspath(osutils.pathjoin(
115
osutils.dirname(__file__), '../../../plugins')))
116
if not bzr_exe: # don't look inside library.zip
122
117
# search the plugin path before the bzrlib installed dir
123
core_path = os.path.dirname(_mod_plugins.__file__)
127
def get_site_plugin_path():
128
"""Returns the path for the site installed plugins."""
129
if sys.platform == 'win32':
130
# We don't have (yet) a good answer for windows since that is certainly
131
# related to the way we build the installers. -- vila20090821
135
from distutils.sysconfig import get_python_lib
137
# If distutuils is not available, we just don't know where they are
140
site_path = osutils.pathjoin(get_python_lib(), 'bzrlib', 'plugins')
144
def get_user_plugin_path():
145
return osutils.pathjoin(config.config_dir(), 'plugins')
148
def get_standard_plugins_path():
149
"""Determine a plugin path suitable for general use."""
150
# Ad-Hoc default: core is not overriden by site but user can overrides both
151
# The rationale is that:
152
# - 'site' comes last, because these plugins should always be available and
153
# are supposed to be in sync with the bzr installed on site.
154
# - 'core' comes before 'site' so that running bzr from sources or a user
155
# installed version overrides the site version.
156
# - 'user' comes first, because... user is always right.
157
# - the above rules clearly defines which plugin version will be loaded if
158
# several exist. Yet, it is sometimes desirable to disable some directory
159
# so that a set of plugins is disabled as once. This can be done via
160
# -site, -core, -user.
162
env_paths = os.environ.get('BZR_PLUGIN_PATH', '+user').split(os.pathsep)
163
defaults = ['+core', '+site']
165
# The predefined references
166
refs = dict(core=get_core_plugin_path(),
167
site=get_site_plugin_path(),
168
user=get_user_plugin_path())
170
# Unset paths that should be removed
171
for k,v in refs.iteritems():
173
# defaults can never mention removing paths as that will make it
174
# impossible for the user to revoke these removals.
175
if removed in env_paths:
176
env_paths.remove(removed)
181
for p in env_paths + defaults:
182
if p.startswith('+'):
183
# Resolve reference if they are known
187
# Leave them untouched otherwise, user may have paths starting
190
_append_new_path(paths, p)
192
# Get rid of trailing slashes, since Python can't handle them when
193
# it tries to import modules.
194
paths = map(_strip_trailing_sep, paths)
118
path.append(os.path.dirname(_mod_plugins.__file__))
119
# search the arch independent path if we can determine that and
120
# the plugin is found nowhere else
121
if sys.platform != 'win32':
123
from distutils.sysconfig import get_python_lib
125
# If distutuils is not available, we just won't add that path
128
archless_path = osutils.pathjoin(get_python_lib(), 'bzrlib',
130
if archless_path not in path:
131
path.append(archless_path)
198
135
def load_plugins(path=None):
317
254
trace.print_exception(sys.exc_info(), sys.stderr)
257
@deprecated_function(one_three)
258
def load_from_zip(zip_name):
259
"""Load all the plugins in a zip."""
260
valid_suffixes = ('.py', '.pyc', '.pyo') # only python modules/packages
263
index = zip_name.rindex('.zip')
266
archive = zip_name[:index+4]
267
prefix = zip_name[index+5:]
269
trace.mutter('Looking for plugins in %r', zip_name)
271
# use zipfile to get list of files/dirs inside zip
273
z = zipfile.ZipFile(archive)
274
namelist = z.namelist()
276
except zipfile.error:
281
prefix = prefix.replace('\\','/')
282
if prefix[-1] != '/':
285
namelist = [name[ix:]
287
if name.startswith(prefix)]
289
trace.mutter('Names in archive: %r', namelist)
291
for name in namelist:
292
if not name or name.endswith('/'):
295
# '/' is used to separate pathname components inside zip archives
298
head, tail = '', name
300
head, tail = name.rsplit('/',1)
302
# we don't need looking in subdirectories
305
base, suffix = osutils.splitext(tail)
306
if suffix not in valid_suffixes:
309
if base == '__init__':
320
if getattr(_mod_plugins, plugin_name, None):
321
trace.mutter('Plugin name %s already loaded', plugin_name)
325
exec "import bzrlib.plugins.%s" % plugin_name in {}
326
trace.mutter('Load plugin %s from zip %r', plugin_name, zip_name)
327
except KeyboardInterrupt:
330
## import pdb; pdb.set_trace()
331
trace.warning('Unable to load plugin %r from %r'
333
trace.log_exception_quietly()
334
if 'error' in debug.debug_flags:
335
trace.print_exception(sys.exc_info(), sys.stderr)
321
339
"""Return a dictionary of the plugins.