mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 09:04:41 +04:00
Detect the python version based on the py launcher
This commit is contained in:
parent
a506a1cac1
commit
753979d720
1 changed files with 19 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ from pre_commit.envcontext import UNSET
|
||||||
from pre_commit.envcontext import Var
|
from pre_commit.envcontext import Var
|
||||||
from pre_commit.languages import helpers
|
from pre_commit.languages import helpers
|
||||||
from pre_commit.parse_shebang import find_executable
|
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 clean_path_on_failure
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.xargs import xargs
|
from pre_commit.xargs import xargs
|
||||||
|
|
@ -40,6 +41,17 @@ def in_env(repo_cmd_runner, language_version):
|
||||||
yield
|
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 _get_default_version(): # pragma: no cover (platform dependent)
|
||||||
def _norm(path):
|
def _norm(path):
|
||||||
_, exe = os.path.split(path.lower())
|
_, exe = os.path.split(path.lower())
|
||||||
|
|
@ -66,6 +78,9 @@ def _get_default_version(): # pragma: no cover (platform dependent)
|
||||||
if find_executable(exe):
|
if find_executable(exe):
|
||||||
return exe
|
return exe
|
||||||
|
|
||||||
|
if _find_by_py_launcher(exe):
|
||||||
|
return exe
|
||||||
|
|
||||||
# Give a best-effort try for windows
|
# Give a best-effort try for windows
|
||||||
if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
|
if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
|
||||||
return exe
|
return exe
|
||||||
|
|
@ -99,6 +114,10 @@ def norm_version(version):
|
||||||
if version_exec and version_exec != version:
|
if version_exec and version_exec != version:
|
||||||
return version_exec
|
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
|
# If it is in the form pythonx.x search in the default
|
||||||
# place on windows
|
# place on windows
|
||||||
if version.startswith('python'):
|
if version.startswith('python'):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue