Skip to content

Commit 9a559cd

Browse files
authored
Merge pull request pre-commit#739 from ojii/language-python-venv
added python venv language
2 parents 3d49db7 + b5af5a5 commit 9a559cd

File tree

14 files changed

+116
-52
lines changed

14 files changed

+116
-52
lines changed

pre_commit/languages/all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pre_commit.languages import pcre
88
from pre_commit.languages import pygrep
99
from pre_commit.languages import python
10+
from pre_commit.languages import python_venv
1011
from pre_commit.languages import ruby
1112
from pre_commit.languages import script
1213
from pre_commit.languages import swift
@@ -57,6 +58,7 @@
5758
'pcre': pcre,
5859
'pygrep': pygrep,
5960
'python': python,
61+
'python_venv': python_venv,
6062
'ruby': ruby,
6163
'script': script,
6264
'swift': swift,

pre_commit/languages/python.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ def get_env_patch(venv):
3232
)
3333

3434

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-
4435
def _find_by_py_launcher(version): # pragma: no cover (windows only)
4536
if version.startswith('python'):
4637
try:
@@ -98,15 +89,6 @@ def get_default_version():
9889
return get_default_version()
9990

10091

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-
11092
def norm_version(version):
11193
if os.name == 'nt': # pragma: no cover (windows)
11294
# Try looking up by name
@@ -123,30 +105,54 @@ def norm_version(version):
123105
if version.startswith('python'):
124106
return r'C:\{}\python.exe'.format(version.replace('.', ''))
125107

126-
# Otherwise assume it is a path
108+
# Otherwise assume it is a path
127109
return os.path.expanduser(version)
128110

129111

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,
112+
def py_interface(_dir, _make_venv):
113+
@contextlib.contextmanager
114+
def in_env(prefix, language_version):
115+
envdir = prefix.path(helpers.environment_dir(_dir, language_version))
116+
with envcontext(get_env_patch(envdir)):
117+
yield
118+
119+
def healthy(prefix, language_version):
120+
with in_env(prefix, language_version):
121+
retcode, _, _ = cmd_output(
122+
'python', '-c',
123+
'import ctypes, datetime, io, os, ssl, weakref',
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from __future__ import unicode_literals
2+
3+
from pre_commit.languages import python
4+
from pre_commit.util import cmd_output
5+
6+
7+
ENVIRONMENT_DIR = 'py_venv'
8+
9+
10+
def make_venv(envdir, python):
11+
cmd_output(python, '-mvenv', envdir, cwd='/')
12+
13+
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

testing/resources/python3_hooks_repo/python3_hook/main.py renamed to testing/resources/python3_hooks_repo/py3_hook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44

55

6-
def func():
6+
def main():
77
print(sys.version_info[0])
88
print(repr(sys.argv[1:]))
99
print('Hello World')
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from setuptools import find_packages
21
from setuptools import setup
32

43
setup(
54
name='python3_hook',
65
version='0.0.0',
7-
packages=find_packages('.'),
8-
entry_points={
9-
'console_scripts': ['python3-hook = python3_hook.main:func'],
10-
},
6+
py_modules=['py3_hook'],
7+
entry_points={'console_scripts': ['python3-hook = py3_hook:main']},
118
)

testing/resources/python_hooks_repo/foo/main.py renamed to testing/resources/python_hooks_repo/foo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44

55

6-
def func():
6+
def main():
77
print(repr(sys.argv[1:]))
88
print('Hello World')
99
return 0

testing/resources/python_hooks_repo/foo/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from setuptools import find_packages
21
from setuptools import setup
32

43
setup(
5-
name='Foo',
4+
name='foo',
65
version='0.0.0',
7-
packages=find_packages('.'),
8-
entry_points={
9-
'console_scripts': ['foo = foo.main:func'],
10-
},
6+
py_modules=['foo'],
7+
entry_points={'console_scripts': ['foo = foo:main']},
118
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- id: foo
2+
name: Foo
3+
entry: foo
4+
language: python_venv
5+
files: \.py$
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import print_function
2+
3+
import sys
4+
5+
6+
def main():
7+
print(repr(sys.argv[1:]))
8+
print('Hello World')
9+
return 0

0 commit comments

Comments
 (0)