204
def _is_mod_installed(module_name, version=None):
204
def get_module_version(module_name):
205
"""Return module version or None if version can't be retrieved."""
206
mod = __import__(module_name)
207
return getattr(mod, '__version__', getattr(mod, 'VERSION', None))
210
def is_module_installed(module_name, version=None, installed_version=None,
205
212
"""Return True if module *module_name* is installed
207
214
If version is not None, checking module version
208
215
(module must have an attribute named '__version__')
210
217
version may starts with =, >=, > or < to specify the exact requirement ;
211
multiple conditions may be separated by ';' (e.g. '>=0.13;<1.0')"""
213
mod = __import__(module_name)
219
for ver in version.split(';'):
220
output = output and _is_mod_installed(module_name, ver)
222
match = re.search('[0-9]', version)
223
assert match is not None, "Invalid version number"
224
symb = version[:match.start()]
227
assert symb in ('>=', '>', '=', '<'),\
228
"Invalid version condition '%s'" % symb
229
version = version[match.start():]
230
actver = getattr(mod, '__version__', getattr(mod, 'VERSION', None))
234
return check_version(actver, version, symb)
239
def is_module_installed(module_name, version=None, interpreter=''):
241
Check if a module is installed with a given version in a determined
218
multiple conditions may be separated by ';' (e.g. '>=0.13;<1.0')
220
interpreter: check if a module is installed with a given version
221
in a determined interpreter"""
245
223
if not osp.isdir(TEMPDIR):
246
224
os.mkdir(TEMPDIR)
248
226
if osp.isfile(interpreter) and ('python' in interpreter):
249
227
checkver = inspect.getsource(check_version)
250
ismod_inst = inspect.getsource(_is_mod_installed)
228
get_modver = inspect.getsource(get_module_version)
229
ismod_inst = inspect.getsource(is_module_installed)
251
230
fd, script = tempfile.mkstemp(suffix='.py', dir=TEMPDIR)
252
231
with os.fdopen(fd, 'w') as f:
253
232
f.write("# -*- coding: utf-8 -*-" + "\n\n")
254
233
f.write("from distutils.version import LooseVersion" + "\n")
255
234
f.write("import re" + "\n\n")
256
235
f.write(checkver + "\n")
236
f.write(get_modver + "\n")
257
237
f.write(ismod_inst + "\n")
259
f.write("print _is_mod_installed('%s','%s')" % (module_name,
239
f.write("print is_module_installed('%s','%s')"\
240
% (module_name, version))
262
f.write("print _is_mod_installed('%s')" % module_name)
242
f.write("print is_module_installed('%s')" % module_name)
264
244
output = subprocess.Popen([interpreter, script],
265
245
stdout=subprocess.PIPE).communicate()[0]
278
return _is_mod_installed(module_name, version)
258
if installed_version is None:
260
actver = get_module_version(module_name)
262
# Module is not installed
265
actver = installed_version
266
if actver is None and version is not None:
268
elif version is None:
273
for ver in version.split(';'):
274
output = output and is_module_installed(module_name, ver)
276
match = re.search('[0-9]', version)
277
assert match is not None, "Invalid version number"
278
symb = version[:match.start()]
281
assert symb in ('>=', '>', '=', '<'),\
282
"Invalid version condition '%s'" % symb
283
version = version[match.start():]
284
return check_version(actver, version, symb)
281
287
if __name__ == '__main__':