Skip to content

Commit e8954e2

Browse files
committed
Simplify python_venv interface
1 parent d6825fa commit e8954e2

File tree

2 files changed

+51
-102
lines changed

2 files changed

+51
-102
lines changed

pre_commit/languages/python.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
ENVIRONMENT_DIR = 'py_env'
19+
HEALTH_MODS = ('datetime', 'io', 'os', 'ssl', 'weakref')
1920

2021

2122
def bin_dir(venv):
@@ -32,15 +33,6 @@ def get_env_patch(venv):
3233
)
3334

3435

35-
@contextlib.contextmanager
36-
def in_env(prefix, language_version):
37-
envdir = prefix.path(
38-
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
39-
)
40-
with envcontext(get_env_patch(envdir)):
41-
yield
42-
43-
4436
def _find_by_py_launcher(version): # pragma: no cover (windows only)
4537
if version.startswith('python'):
4638
try:
@@ -98,15 +90,6 @@ def get_default_version():
9890
return get_default_version()
9991

10092

101-
def healthy(prefix, language_version):
102-
with in_env(prefix, language_version):
103-
retcode, _, _ = cmd_output(
104-
'python', '-c', 'import ctypes, datetime, io, os, ssl, weakref',
105-
retcode=None,
106-
)
107-
return retcode == 0
108-
109-
11093
def norm_version(version):
11194
if os.name == 'nt': # pragma: no cover (windows)
11295
# Try looking up by name
@@ -123,30 +106,53 @@ def norm_version(version):
123106
if version.startswith('python'):
124107
return r'C:\{}\python.exe'.format(version.replace('.', ''))
125108

126-
# Otherwise assume it is a path
109+
# Otherwise assume it is a path
127110
return os.path.expanduser(version)
128111

129112

130-
def install_environment(prefix, version, additional_dependencies):
131-
additional_dependencies = tuple(additional_dependencies)
132-
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
133-
134-
# Install a virtualenv
135-
env_dir = prefix.path(directory)
136-
with clean_path_on_failure(env_dir):
137-
venv_cmd = [sys.executable, '-m', 'virtualenv', env_dir]
138-
if version != 'default':
139-
venv_cmd.extend(['-p', norm_version(version)])
140-
else:
141-
venv_cmd.extend(['-p', os.path.realpath(sys.executable)])
142-
venv_env = dict(os.environ, VIRTUALENV_NO_DOWNLOAD='1')
143-
cmd_output(*venv_cmd, cwd='/', env=venv_env)
144-
with in_env(prefix, version):
145-
helpers.run_setup_cmd(
146-
prefix, ('pip', 'install', '.') + additional_dependencies,
113+
def py_interface(_dir, _make_venv):
114+
@contextlib.contextmanager
115+
def in_env(prefix, language_version):
116+
envdir = prefix.path(helpers.environment_dir(_dir, language_version))
117+
with envcontext(get_env_patch(envdir)):
118+
yield
119+
120+
def healthy(prefix, language_version):
121+
with in_env(prefix, language_version):
122+
retcode, _, _ = cmd_output(
123+
'python', '-c', 'import {}'.format(','.join(HEALTH_MODS)),
124+
retcode=None,
147125
)
126+
return retcode == 0
127+
128+
def run_hook(prefix, hook, file_args):
129+
with in_env(prefix, hook['language_version']):
130+
return xargs(helpers.to_cmd(hook), file_args)
131+
132+
def install_environment(prefix, version, additional_dependencies):
133+
additional_dependencies = tuple(additional_dependencies)
134+
directory = helpers.environment_dir(_dir, version)
135+
136+
env_dir = prefix.path(directory)
137+
with clean_path_on_failure(env_dir):
138+
if version != 'default':
139+
python = norm_version(version)
140+
else:
141+
python = os.path.realpath(sys.executable)
142+
_make_venv(env_dir, python)
143+
with in_env(prefix, version):
144+
helpers.run_setup_cmd(
145+
prefix, ('pip', 'install', '.') + additional_dependencies,
146+
)
147+
148+
return in_env, healthy, run_hook, install_environment
149+
150+
151+
def make_venv(envdir, python):
152+
env = dict(os.environ, VIRTUALENV_NO_DOWNLOAD='1')
153+
cmd = (sys.executable, '-mvirtualenv', envdir, '-p', python)
154+
cmd_output(*cmd, env=env, cwd='/')
148155

149156

150-
def run_hook(prefix, hook, file_args):
151-
with in_env(prefix, hook['language_version']):
152-
return xargs(helpers.to_cmd(hook), file_args)
157+
_interface = py_interface(ENVIRONMENT_DIR, make_venv)
158+
in_env, healthy, run_hook, install_environment = _interface
Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,16 @@
11
from __future__ import unicode_literals
22

3-
import contextlib
4-
import os
5-
import sys
6-
7-
from pre_commit.envcontext import envcontext
8-
from pre_commit.envcontext import UNSET
9-
from pre_commit.envcontext import Var
10-
from pre_commit.languages import helpers
11-
from pre_commit.languages.python import get_default_version # noqa: F401
12-
from pre_commit.languages.python import norm_version
13-
from pre_commit.util import clean_path_on_failure
3+
from pre_commit.languages import python
144
from pre_commit.util import cmd_output
15-
from pre_commit.xargs import xargs
165

176

187
ENVIRONMENT_DIR = 'py_venv'
198

209

21-
def bin_dir(venv):
22-
"""On windows there's a different directory for the virtualenv"""
23-
bin_part = 'Scripts' if os.name == 'nt' else 'bin'
24-
return os.path.join(venv, bin_part)
25-
26-
27-
def get_env_patch(venv):
28-
return (
29-
('PYTHONHOME', UNSET),
30-
('VIRTUAL_ENV', venv),
31-
('PATH', (bin_dir(venv), os.pathsep, Var('PATH'))),
32-
)
33-
34-
35-
@contextlib.contextmanager
36-
def in_env(prefix, language_version):
37-
envdir = prefix.path(
38-
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
39-
)
40-
with envcontext(get_env_patch(envdir)):
41-
yield
42-
43-
44-
def healthy(prefix, language_version):
45-
with in_env(prefix, language_version):
46-
retcode, _, _ = cmd_output(
47-
'python', '-c', 'import ctypes, datetime, io, os, ssl, weakref',
48-
retcode=None,
49-
)
50-
return retcode == 0
51-
52-
53-
def install_environment(prefix, version, additional_dependencies):
54-
additional_dependencies = tuple(additional_dependencies)
55-
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
56-
57-
# Install a virtualenv
58-
env_dir = prefix.path(directory)
59-
with clean_path_on_failure(env_dir):
60-
if version != 'default':
61-
executable = norm_version(version)
62-
else:
63-
executable = os.path.realpath(sys.executable)
64-
cmd_output(executable, '-m', 'venv', env_dir, cwd='/')
65-
with in_env(prefix, version):
66-
helpers.run_setup_cmd(
67-
prefix, ('pip', 'install', '.') + additional_dependencies,
68-
)
10+
def make_venv(envdir, python):
11+
cmd_output(python, '-mvenv', envdir, cwd='/')
6912

7013

71-
def run_hook(prefix, hook, file_args):
72-
with in_env(prefix, hook['language_version']):
73-
return xargs(helpers.to_cmd(hook), file_args)
14+
get_default_version = python.get_default_version
15+
_interface = python.py_interface(ENVIRONMENT_DIR, make_venv)
16+
in_env, healthy, run_hook, install_environment = _interface

0 commit comments

Comments
 (0)