Skip to content

Commit a5b56bd

Browse files
committed
Factor out bash and activate files
1 parent 00a3a9a commit a5b56bd

File tree

13 files changed

+151
-191
lines changed

13 files changed

+151
-191
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
REBUILD_FLAG =
32

43
.PHONY: all
@@ -21,7 +20,7 @@ test: .venv.touch
2120

2221
.PHONY: clean
2322
clean:
24-
find . -iname '*.pyc' | xargs rm -f
23+
find . -name '*.pyc' -delete
2524
rm -rf .tox
2625
rm -rf ./venv-*
2726
rm -f .venv.touch

pre_commit/languages/all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# def install_environment(
1616
# repo_cmd_runner,
1717
# version='default',
18-
# additional_dependencies=None,
18+
# additional_dependencies=(),
1919
# ):
2020
# """Installs a repository in the given repository. Note that the current
2121
# working directory will already be inside the repository.

pre_commit/languages/helpers.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from __future__ import unicode_literals
22

3-
import pipes
3+
from pre_commit.util import cmd_output
4+
5+
6+
def run_setup_cmd(runner, cmd):
7+
cmd_output(*cmd, cwd=runner.prefix_dir, encoding=None)
48

59

610
def environment_dir(ENVIRONMENT_DIR, language_version):
@@ -14,39 +18,12 @@ def file_args_to_stdin(file_args):
1418
return '\0'.join(list(file_args) + [''])
1519

1620

17-
def run_hook(env, hook, file_args):
18-
quoted_args = [pipes.quote(arg) for arg in hook['args']]
19-
return env.run(
21+
def run_hook(cmd_args, file_args):
22+
return cmd_output(
2023
# Use -s 4000 (slightly less than posix mandated minimum)
2124
# This is to prevent "xargs: ... Bad file number" on windows
22-
' '.join(['xargs', '-0', '-s4000', hook['entry']] + quoted_args),
25+
'xargs', '-0', '-s4000', *cmd_args,
2326
stdin=file_args_to_stdin(file_args),
2427
retcode=None,
25-
encoding=None,
28+
encoding=None
2629
)
27-
28-
29-
class Environment(object):
30-
def __init__(self, repo_cmd_runner, language_version):
31-
self.repo_cmd_runner = repo_cmd_runner
32-
self.language_version = language_version
33-
34-
@property
35-
def env_prefix(self):
36-
"""env_prefix is a value that is prefixed to the command that is run.
37-
38-
Usually this is to source a virtualenv, etc.
39-
40-
Commands basically end up looking like:
41-
42-
bash -c '{env_prefix} {cmd}'
43-
44-
so you'll often want to end your prefix with &&
45-
"""
46-
raise NotImplementedError
47-
48-
def run(self, cmd, **kwargs):
49-
"""Returns (returncode, stdout, stderr)."""
50-
return self.repo_cmd_runner.run(
51-
['bash', '-c', ' '.join([self.env_prefix, cmd])], **kwargs
52-
)

pre_commit/languages/node.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
from __future__ import unicode_literals
22

33
import contextlib
4+
import os
45
import sys
56

7+
from pre_commit.envcontext import envcontext
8+
from pre_commit.envcontext import Var
69
from pre_commit.languages import helpers
710
from pre_commit.util import clean_path_on_failure
8-
from pre_commit.util import shell_escape
911

1012

1113
ENVIRONMENT_DIR = 'node_env'
1214

1315

14-
class NodeEnv(helpers.Environment):
15-
@property
16-
def env_prefix(self):
17-
return ". '{{prefix}}{0}/bin/activate' &&".format(
18-
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version),
19-
)
16+
def get_env_patch(venv):
17+
return (
18+
('NODE_VIRTUAL_ENV', venv),
19+
('NPM_CONFIG_PREFIX', venv),
20+
('npm_config_prefix', venv),
21+
('NODE_PATH', os.path.join(venv, 'lib', 'node_modules')),
22+
('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))),
23+
)
2024

2125

2226
@contextlib.contextmanager
2327
def in_env(repo_cmd_runner, language_version):
24-
yield NodeEnv(repo_cmd_runner, language_version)
28+
envdir = os.path.join(
29+
repo_cmd_runner.prefix_dir,
30+
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
31+
)
32+
with envcontext(get_env_patch(envdir)):
33+
yield
2534

2635

2736
def install_environment(
2837
repo_cmd_runner,
2938
version='default',
30-
additional_dependencies=None,
39+
additional_dependencies=(),
3140
):
41+
additional_dependencies = tuple(additional_dependencies)
3242
assert repo_cmd_runner.exists('package.json')
3343
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
3444

@@ -44,18 +54,15 @@ def install_environment(
4454

4555
repo_cmd_runner.run(cmd)
4656

47-
with in_env(repo_cmd_runner, version) as node_env:
48-
node_env.run("cd '{prefix}' && npm install -g", encoding=None)
49-
if additional_dependencies:
50-
node_env.run(
51-
"cd '{prefix}' && npm install -g " +
52-
' '.join(
53-
shell_escape(dep) for dep in additional_dependencies
54-
),
55-
encoding=None,
56-
)
57+
with in_env(repo_cmd_runner, version):
58+
helpers.run_setup_cmd(
59+
repo_cmd_runner,
60+
('npm', 'install', '-g', '.') + additional_dependencies,
61+
)
5762

5863

5964
def run_hook(repo_cmd_runner, hook, file_args):
60-
with in_env(repo_cmd_runner, hook['language_version']) as env:
61-
return helpers.run_hook(env, hook, file_args)
65+
with in_env(repo_cmd_runner, hook['language_version']):
66+
return helpers.run_hook(
67+
(hook['entry'],) + tuple(hook['args']), file_args,
68+
)

pre_commit/languages/pcre.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from sys import platform
44

5-
from pre_commit.languages.helpers import file_args_to_stdin
5+
from pre_commit.languages import helpers
66
from pre_commit.util import shell_escape
77

88

@@ -12,29 +12,28 @@
1212
def install_environment(
1313
repo_cmd_runner,
1414
version='default',
15-
additional_dependencies=None,
15+
additional_dependencies=(),
1616
):
1717
"""Installation for pcre type is a noop."""
1818
raise AssertionError('Cannot install pcre repo.')
1919

2020

2121
def run_hook(repo_cmd_runner, hook, file_args):
22-
grep_command = 'grep -H -n -P'
23-
if platform == 'darwin': # pragma: no cover (osx)
24-
grep_command = 'ggrep -H -n -P'
22+
grep_command = '{0} -H -n -P'.format(
23+
'ggrep' if platform == 'darwin' else 'grep',
24+
)
2525

2626
# For PCRE the entry is the regular expression to match
27-
return repo_cmd_runner.run(
28-
[
29-
'xargs', '-0', 'sh', '-c',
27+
return helpers.run_hook(
28+
(
29+
'sh', '-c',
3030
# Grep usually returns 0 for matches, and nonzero for non-matches
3131
# so we flip it here.
3232
'! {0} {1} {2} $@'.format(
3333
grep_command, ' '.join(hook['args']),
34-
shell_escape(hook['entry'])),
34+
shell_escape(hook['entry']),
35+
),
3536
'--',
36-
],
37-
stdin=file_args_to_stdin(file_args),
38-
retcode=None,
39-
encoding=None,
37+
),
38+
file_args,
4039
)

pre_commit/languages/python.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,38 @@
55
import os
66
import sys
77

8+
from pre_commit.envcontext import envcontext
9+
from pre_commit.envcontext import UNSET
10+
from pre_commit.envcontext import Var
811
from pre_commit.languages import helpers
912
from pre_commit.util import clean_path_on_failure
10-
from pre_commit.util import shell_escape
1113

1214

1315
ENVIRONMENT_DIR = 'py_env'
1416

1517

1618
def bin_dir(venv):
1719
"""On windows there's a different directory for the virtualenv"""
18-
if os.name == 'nt': # pragma: no cover (windows)
19-
return os.path.join(venv, 'Scripts')
20-
else:
21-
return os.path.join(venv, 'bin')
22-
23-
24-
class PythonEnv(helpers.Environment):
25-
@property
26-
def env_prefix(self):
27-
return ". '{{prefix}}{0}{1}activate' &&".format(
28-
bin_dir(
29-
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version)
30-
),
31-
os.sep,
32-
)
20+
bin_part = 'Scripts' if os.name == 'nt' else 'bin'
21+
return os.path.join(venv, bin_part)
22+
23+
24+
def get_env_patch(venv):
25+
return (
26+
('PYTHONHOME', UNSET),
27+
('VIRTUAL_ENV', venv),
28+
('PATH', (bin_dir(venv), os.pathsep, Var('PATH'))),
29+
)
3330

3431

3532
@contextlib.contextmanager
3633
def in_env(repo_cmd_runner, language_version):
37-
yield PythonEnv(repo_cmd_runner, language_version)
34+
envdir = os.path.join(
35+
repo_cmd_runner.prefix_dir,
36+
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
37+
)
38+
with envcontext(get_env_patch(envdir)):
39+
yield
3840

3941

4042
def norm_version(version):
@@ -55,9 +57,9 @@ def norm_version(version):
5557
def install_environment(
5658
repo_cmd_runner,
5759
version='default',
58-
additional_dependencies=None,
60+
additional_dependencies=(),
5961
):
60-
assert repo_cmd_runner.exists('setup.py')
62+
additional_dependencies = tuple(additional_dependencies)
6163
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
6264

6365
# Install a virtualenv
@@ -69,18 +71,15 @@ def install_environment(
6971
if version != 'default':
7072
venv_cmd.extend(['-p', norm_version(version)])
7173
repo_cmd_runner.run(venv_cmd)
72-
with in_env(repo_cmd_runner, version) as env:
73-
env.run("cd '{prefix}' && pip install .", encoding=None)
74-
if additional_dependencies:
75-
env.run(
76-
"cd '{prefix}' && pip install " +
77-
' '.join(
78-
shell_escape(dep) for dep in additional_dependencies
79-
),
80-
encoding=None,
81-
)
74+
with in_env(repo_cmd_runner, version):
75+
helpers.run_setup_cmd(
76+
repo_cmd_runner,
77+
('pip', 'install', '.') + additional_dependencies,
78+
)
8279

8380

8481
def run_hook(repo_cmd_runner, hook, file_args):
85-
with in_env(repo_cmd_runner, hook['language_version']) as env:
86-
return helpers.run_hook(env, hook, file_args)
82+
with in_env(repo_cmd_runner, hook['language_version']):
83+
return helpers.run_hook(
84+
(hook['entry'],) + tuple(hook['args']), file_args,
85+
)

0 commit comments

Comments
 (0)