80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
'''
|
|
Created on 24 mai 2015
|
|
|
|
@author: coissac
|
|
'''
|
|
from distutils.version import StrictVersion
|
|
from distutils import sysconfig
|
|
import subprocess
|
|
import os
|
|
import glob
|
|
import re
|
|
|
|
from obidistutils.serenity.checksystem import is_windows_system
|
|
|
|
|
|
def is_python_version(path=None,minversion='3.4',maxversion=None):
|
|
'''
|
|
Checks that the python version is in the range {minversion,maxversion[
|
|
|
|
@param path: if None consider the running python
|
|
otherwise the python pointed by the path
|
|
@param minversion: the minimum version to consider
|
|
@param maxversion: the maximum version to consider (strictly inferior to)
|
|
|
|
@return: True if the python version match
|
|
@rtype: bool
|
|
'''
|
|
if path is None:
|
|
pythonversion = StrictVersion(sysconfig.get_python_version())
|
|
else:
|
|
command = """'%s' -c 'from distutils import sysconfig; """ \
|
|
"""print(sysconfig.get_python_version())'""" % path
|
|
|
|
p = subprocess.Popen(command,
|
|
shell=True,
|
|
stdout=subprocess.PIPE)
|
|
pythonversion=str(p.communicate()[0],'utf8').strip()
|
|
pythonversion = StrictVersion(pythonversion)
|
|
|
|
return ( pythonversion >=StrictVersion(minversion)
|
|
and ( maxversion is None
|
|
or pythonversion < StrictVersion(maxversion))
|
|
)
|
|
|
|
|
|
def lookfor_good_python(minversion='3.4',maxversion=None,followLink=False):
|
|
'''
|
|
Look for all python interpreters present in the system path that
|
|
match the version constraints.
|
|
|
|
@param minversion: the minimum version to consider
|
|
@param maxversion: the maximum version to consider (strictly inferior to)
|
|
@param followLink: a boolean value indicating if link must be substituted
|
|
by their real path.
|
|
|
|
@return: a list of path to interpreters
|
|
'''
|
|
exe = []
|
|
if not is_windows_system():
|
|
paths = os.environ['PATH'].split(os.pathsep)
|
|
for p in paths:
|
|
candidates = glob.glob(os.path.join(p,'python*'))
|
|
pexe = []
|
|
pythonpat=re.compile('python([0-9]|[0-9]\.[0-9])?$')
|
|
for e in candidates:
|
|
print(e)
|
|
if pythonpat.search(e) is not None:
|
|
if followLink and os.path.islink(e):
|
|
e = os.path.realpath(e)
|
|
if (os.path.isfile(e) and
|
|
os.access(e, os.X_OK) and
|
|
is_python_version(e,minversion,maxversion)):
|
|
pexe.append(e)
|
|
exe.extend(set(pexe))
|
|
|
|
return exe
|
|
|
|
|
|
|