forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
38 lines (25 loc) · 1.04 KB
/
python.py
File metadata and controls
38 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import unicode_literals
import contextlib
from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure
ENVIRONMENT_DIR = 'py_env'
class PythonEnv(helpers.Environment):
@property
def env_prefix(self):
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
@contextlib.contextmanager
def in_env(repo_cmd_runner):
yield PythonEnv(repo_cmd_runner)
def install_environment(repo_cmd_runner, version='default'):
assert repo_cmd_runner.exists('setup.py')
# Install a virtualenv
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
venv_cmd = ['virtualenv', '{{prefix}}{0}'.format(ENVIRONMENT_DIR)]
if version != 'default':
venv_cmd.extend(['-p', version])
repo_cmd_runner.run(venv_cmd)
with in_env(repo_cmd_runner) as env:
env.run('cd {prefix} && pip install .')
def run_hook(repo_cmd_runner, hook, file_args):
with in_env(repo_cmd_runner) as env:
return helpers.run_hook(env, hook, file_args)