This commit is contained in:
Jonas Obrist 2018-04-26 03:52:47 +00:00 committed by GitHub
commit 9ac529381a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 8 deletions

View file

@ -2,6 +2,7 @@ from __future__ import unicode_literals
import contextlib import contextlib
import os import os
import subprocess
import sys import sys
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
@ -14,6 +15,11 @@ 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
try:
devnull = subprocess.DEVNULL
except AttributeError:
devnull = open(os.devnull, 'w')
ENVIRONMENT_DIR = 'py_env' ENVIRONMENT_DIR = 'py_env'
@ -108,23 +114,29 @@ def healthy(prefix, language_version):
def norm_version(version): def norm_version(version):
if version.endswith('+venv'):
use_venv = True
version = version[:-5]
else:
use_venv = False
if os.name == 'nt': # pragma: no cover (windows) if os.name == 'nt': # pragma: no cover (windows)
# Try looking up by name # Try looking up by name
version_exec = find_executable(version) version_exec = find_executable(version)
if version_exec and version_exec != version: if version_exec and version_exec != version:
return version_exec return version_exec, use_venv
version_exec = _find_by_py_launcher(version) version_exec = _find_by_py_launcher(version)
if version_exec: if version_exec:
return version_exec return version_exec, use_venv
# 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'):
return r'C:\{}\python.exe'.format(version.replace('.', '')) path = r'C:\{}\python.exe'.format(version.replace('.', ''))
return path, use_venv
# Otherwise assume it is a path # Otherwise assume it is a path
return os.path.expanduser(version) return os.path.expanduser(version), use_venv
def install_environment(prefix, version, additional_dependencies): def install_environment(prefix, version, additional_dependencies):
@ -134,11 +146,23 @@ def install_environment(prefix, version, additional_dependencies):
# Install a virtualenv # Install a virtualenv
env_dir = prefix.path(directory) env_dir = prefix.path(directory)
with clean_path_on_failure(env_dir): with clean_path_on_failure(env_dir):
venv_cmd = [sys.executable, '-m', 'virtualenv', env_dir]
if version != 'default': if version != 'default':
venv_cmd.extend(['-p', norm_version(version)]) target_python, use_venv = norm_version(version)
else: else:
venv_cmd.extend(['-p', os.path.realpath(sys.executable)]) target_python = os.path.realpath(sys.executable)
use_venv = False
if use_venv:
venv_cmd = [target_python, '-m', 'venv', env_dir]
else:
venv_cmd = [
sys.executable,
'-m',
'virtualenv',
'-p',
target_python,
env_dir,
]
venv_env = dict(os.environ, VIRTUALENV_NO_DOWNLOAD='1') venv_env = dict(os.environ, VIRTUALENV_NO_DOWNLOAD='1')
cmd_output(*venv_cmd, cwd='/', env=venv_env) cmd_output(*venv_cmd, cwd='/', env=venv_env)
with in_env(prefix, version): with in_env(prefix, version):

View file

@ -3,6 +3,8 @@ from __future__ import unicode_literals
import os.path import os.path
import pytest
from pre_commit.languages import python from pre_commit.languages import python
@ -15,4 +17,14 @@ def test_norm_version_expanduser():
path = '~/.pyenv/versions/3.4.3/bin/python' path = '~/.pyenv/versions/3.4.3/bin/python'
expected_path = home + '/.pyenv/versions/3.4.3/bin/python' expected_path = home + '/.pyenv/versions/3.4.3/bin/python'
result = python.norm_version(path) result = python.norm_version(path)
assert result == expected_path assert result == (expected_path, False)
@pytest.mark.parametrize(
'version,use_venv', [
('python3.6', False),
('python3.6+venv', True),
],
)
def test_norm_version_venv(version, use_venv):
assert python.norm_version(version)[1] == use_venv