mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
allow opt-in for using venv over virtualenv
This commit is contained in:
parent
085768b673
commit
495dc33dac
2 changed files with 36 additions and 21 deletions
|
|
@ -114,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):
|
||||||
|
|
@ -141,25 +147,22 @@ def install_environment(prefix, version, additional_dependencies):
|
||||||
env_dir = prefix.path(directory)
|
env_dir = prefix.path(directory)
|
||||||
with clean_path_on_failure(env_dir):
|
with clean_path_on_failure(env_dir):
|
||||||
if version != 'default':
|
if version != 'default':
|
||||||
target_python = norm_version(version)
|
target_python, use_venv = norm_version(version)
|
||||||
else:
|
else:
|
||||||
target_python = os.path.realpath(sys.executable)
|
target_python = os.path.realpath(sys.executable)
|
||||||
|
use_venv = False
|
||||||
|
|
||||||
try:
|
if use_venv:
|
||||||
subprocess.check_call(
|
venv_cmd = [target_python, '-m', 'venv', env_dir]
|
||||||
[target_python, '-c', 'import venv'],
|
else:
|
||||||
stderr=devnull,
|
venv_cmd = [
|
||||||
stdout=devnull,
|
sys.executable,
|
||||||
)
|
'-m',
|
||||||
venv_python = target_python
|
'virtualenv',
|
||||||
venv_module = 'venv'
|
'-p',
|
||||||
extra_cmd = []
|
target_python,
|
||||||
except subprocess.CalledProcessError:
|
env_dir,
|
||||||
venv_python = sys.executable
|
]
|
||||||
venv_module = 'virtualenv'
|
|
||||||
extra_cmd = ['-p', target_python]
|
|
||||||
|
|
||||||
venv_cmd = [venv_python, '-m', venv_module, env_dir] + extra_cmd
|
|
||||||
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):
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue