Detect the python version based on the py launcher

This commit is contained in:
Anthony Sottile
2018-01-02 18:47:40 -08:00
parent a506a1cac1
commit 753979d720

View File

@@ -9,6 +9,7 @@ from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var
from pre_commit.languages import helpers
from pre_commit.parse_shebang import find_executable
from pre_commit.util import CalledProcessError
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output
from pre_commit.xargs import xargs
@@ -40,6 +41,17 @@ def in_env(repo_cmd_runner, language_version):
yield
def _find_by_py_launcher(version): # pragma: no cover (windows only)
if version.startswith('python'):
try:
return cmd_output(
'py', '-{}'.format(version[len('python'):]),
'-c', 'import sys; print(sys.executable)',
)[1].strip()
except CalledProcessError:
pass
def _get_default_version(): # pragma: no cover (platform dependent)
def _norm(path):
_, exe = os.path.split(path.lower())
@@ -66,6 +78,9 @@ def _get_default_version(): # pragma: no cover (platform dependent)
if find_executable(exe):
return exe
if _find_by_py_launcher(exe):
return exe
# Give a best-effort try for windows
if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
return exe
@@ -99,6 +114,10 @@ def norm_version(version):
if version_exec and version_exec != version:
return version_exec
version_exec = _find_by_py_launcher(version)
if version_exec:
return version_exec
# If it is in the form pythonx.x search in the default
# place on windows
if version.startswith('python'):