mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
Use python -mvenv if possible
This commit is contained in:
parent
834ed0f229
commit
085768b673
1 changed files with 24 additions and 3 deletions
|
|
@ -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'
|
||||||
|
|
||||||
|
|
@ -134,11 +140,26 @@ 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 = norm_version(version)
|
||||||
else:
|
else:
|
||||||
venv_cmd.extend(['-p', os.path.realpath(sys.executable)])
|
target_python = os.path.realpath(sys.executable)
|
||||||
|
|
||||||
|
try:
|
||||||
|
subprocess.check_call(
|
||||||
|
[target_python, '-c', 'import venv'],
|
||||||
|
stderr=devnull,
|
||||||
|
stdout=devnull,
|
||||||
|
)
|
||||||
|
venv_python = target_python
|
||||||
|
venv_module = 'venv'
|
||||||
|
extra_cmd = []
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
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):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue