/usr/lib/python3/dist-packages/tox/interpreters.py is in tox 2.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | import sys
import py
import re
import inspect
from tox import hookimpl
class Interpreters:
def __init__(self, hook):
self.name2executable = {}
self.executable2info = {}
self.hook = hook
def get_executable(self, envconfig):
""" return path object to the executable for the given
name (e.g. python2.6, python2.7, python etc.)
if name is already an existing path, return name.
If an interpreter cannot be found, return None.
"""
try:
return self.name2executable[envconfig.envname]
except KeyError:
exe = self.hook.tox_get_python_executable(envconfig=envconfig)
self.name2executable[envconfig.envname] = exe
return exe
def get_info(self, envconfig):
executable = self.get_executable(envconfig)
name = envconfig.basepython
if not executable:
return NoInterpreterInfo(name=name)
try:
return self.executable2info[executable]
except KeyError:
info = run_and_get_interpreter_info(name, executable)
self.executable2info[executable] = info
return info
def get_sitepackagesdir(self, info, envdir):
if not info.executable:
return ""
envdir = str(envdir)
try:
res = exec_on_interpreter(info.executable,
[inspect.getsource(sitepackagesdir),
"print(sitepackagesdir(%r))" % envdir])
except ExecFailed:
val = sys.exc_info()[1]
print("execution failed: %s -- %s" % (val.out, val.err))
return ""
else:
return res["dir"]
def run_and_get_interpreter_info(name, executable):
assert executable
try:
result = exec_on_interpreter(executable,
[inspect.getsource(pyinfo), "print(pyinfo())"])
except ExecFailed:
val = sys.exc_info()[1]
return NoInterpreterInfo(name, executable=val.executable,
out=val.out, err=val.err)
else:
return InterpreterInfo(name, executable, **result)
def exec_on_interpreter(executable, source):
if isinstance(source, list):
source = "\n".join(source)
from subprocess import Popen, PIPE
args = [str(executable)]
popen = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
popen.stdin.write(source.encode("utf8"))
out, err = popen.communicate()
if popen.returncode:
raise ExecFailed(executable, source, out, err)
try:
result = eval(out.strip())
except Exception:
raise ExecFailed(executable, source, out,
"could not decode %r" % out)
return result
class ExecFailed(Exception):
def __init__(self, executable, source, out, err):
self.executable = executable
self.source = source
self.out = out
self.err = err
class InterpreterInfo:
runnable = True
def __init__(self, name, executable, version_info, sysplatform):
assert executable and version_info
self.name = name
self.executable = executable
self.version_info = version_info
self.sysplatform = sysplatform
def __str__(self):
return "<executable at %s, version_info %s>" % (
self.executable, self.version_info)
class NoInterpreterInfo:
runnable = False
def __init__(self, name, executable=None,
out=None, err="not found"):
self.name = name
self.executable = executable
self.version_info = None
self.out = out
self.err = err
def __str__(self):
if self.executable:
return "<executable at %s, not runnable>"
else:
return "<executable not found for: %s>" % self.name
if sys.platform != "win32":
@hookimpl
def tox_get_python_executable(envconfig):
return py.path.local.sysfind(envconfig.basepython)
else:
@hookimpl
def tox_get_python_executable(envconfig):
name = envconfig.basepython
p = py.path.local.sysfind(name)
if p:
return p
actual = None
# Is this a standard PythonX.Y name?
m = re.match(r"python(\d)\.(\d)", name)
if m:
# The standard names are in predictable places.
actual = r"c:\python%s%s\python.exe" % m.groups()
if not actual:
actual = win32map.get(name, None)
if actual:
actual = py.path.local(actual)
if actual.check():
return actual
# The standard executables can be found as a last resort via the
# Python launcher py.exe
if m:
return locate_via_py(*m.groups())
# Exceptions to the usual windows mapping
win32map = {
'python': sys.executable,
'jython': "c:\jython2.5.1\jython.bat",
}
def locate_via_py(v_maj, v_min):
ver = "-%s.%s" % (v_maj, v_min)
script = "import sys; print(sys.executable)"
py_exe = py.path.local.sysfind('py')
if py_exe:
try:
exe = py_exe.sysexec(ver, '-c', script).strip()
except py.process.cmdexec.Error:
exe = None
if exe:
exe = py.path.local(exe)
if exe.check():
return exe
def pyinfo():
import sys
return dict(version_info=tuple(sys.version_info),
sysplatform=sys.platform)
def sitepackagesdir(envdir):
from distutils.sysconfig import get_python_lib
return dict(dir=get_python_lib(prefix=envdir))
|