Skip to content

Commit 7d87da8

Browse files
committed
Move PrefixedCommandRunner -> Prefix
1 parent c751f62 commit 7d87da8

23 files changed

+270
-372
lines changed

pre_commit/languages/all.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@
2323
# return 'default' if there is no better option.
2424
# """
2525
#
26-
# def healthy(repo_cmd_runner, language_version):
26+
# def healthy(prefix, language_version):
2727
# """Return whether or not the environment is considered functional."""
2828
#
29-
# def install_environment(repo_cmd_runner, version, additional_dependencies):
29+
# def install_environment(prefix, version, additional_dependencies):
3030
# """Installs a repository in the given repository. Note that the current
3131
# working directory will already be inside the repository.
3232
#
3333
# Args:
34-
# repo_cmd_runner - `PrefixedCommandRunner` bound to the repository.
34+
# prefix - `Prefix` bound to the repository.
3535
# version - A version specified in the hook configuration or
3636
# 'default'.
3737
# """
3838
#
39-
# def run_hook(repo_cmd_runner, hook, file_args):
39+
# def run_hook(prefix, hook, file_args):
4040
# """Runs a hook and returns the returncode and output of running that
4141
# hook.
4242
#
4343
# Args:
44-
# repo_cmd_runner - `PrefixedCommandRunner` bound to the repository.
44+
# prefix - `Prefix` bound to the repository.
4545
# hook - Hook dictionary
4646
# file_args - The files to be run
4747
#
@@ -62,6 +62,4 @@
6262
'swift': swift,
6363
'system': system,
6464
}
65-
66-
67-
all_languages = languages.keys()
65+
all_languages = sorted(languages)

pre_commit/languages/docker.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ def md5(s): # pragma: windows no cover
2222
return hashlib.md5(five.to_bytes(s)).hexdigest()
2323

2424

25-
def docker_tag(repo_cmd_runner): # pragma: windows no cover
26-
return 'pre-commit-{}'.format(
27-
md5(os.path.basename(repo_cmd_runner.path())),
28-
).lower()
25+
def docker_tag(prefix): # pragma: windows no cover
26+
md5sum = md5(os.path.basename(prefix.prefix_dir)).lower()
27+
return 'pre-commit-{}'.format(md5sum)
2928

3029

3130
def docker_is_running(): # pragma: windows no cover
@@ -41,39 +40,36 @@ def assert_docker_available(): # pragma: windows no cover
4140
)
4241

4342

44-
def build_docker_image(repo_cmd_runner, **kwargs): # pragma: windows no cover
43+
def build_docker_image(prefix, **kwargs): # pragma: windows no cover
4544
pull = kwargs.pop('pull')
4645
assert not kwargs, kwargs
4746
cmd = (
4847
'docker', 'build',
49-
'--tag', docker_tag(repo_cmd_runner),
48+
'--tag', docker_tag(prefix),
5049
'--label', PRE_COMMIT_LABEL,
5150
)
5251
if pull:
5352
cmd += ('--pull',)
5453
# This must come last for old versions of docker. See #477
5554
cmd += ('.',)
56-
helpers.run_setup_cmd(repo_cmd_runner, cmd)
55+
helpers.run_setup_cmd(prefix, cmd)
5756

5857

5958
def install_environment(
60-
repo_cmd_runner, version, additional_dependencies,
59+
prefix, version, additional_dependencies,
6160
): # pragma: windows no cover
62-
assert repo_cmd_runner.exists('Dockerfile'), (
63-
'No Dockerfile was found in the hook repository'
64-
)
6561
helpers.assert_version_default('docker', version)
6662
helpers.assert_no_additional_deps('docker', additional_dependencies)
6763
assert_docker_available()
6864

69-
directory = repo_cmd_runner.path(
65+
directory = prefix.path(
7066
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
7167
)
7268

7369
# Docker doesn't really have relevant disk environment, but pre-commit
7470
# still needs to cleanup it's state files on failure
7571
with clean_path_on_failure(directory):
76-
build_docker_image(repo_cmd_runner, pull=True)
72+
build_docker_image(prefix, pull=True)
7773
os.mkdir(directory)
7874

7975

@@ -90,15 +86,15 @@ def docker_cmd():
9086
)
9187

9288

93-
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
89+
def run_hook(prefix, hook, file_args): # pragma: windows no cover
9490
assert_docker_available()
9591
# Rebuild the docker image in case it has gone missing, as many people do
9692
# automated cleanup of docker images.
97-
build_docker_image(repo_cmd_runner, pull=False)
93+
build_docker_image(prefix, pull=False)
9894

9995
hook_cmd = helpers.to_cmd(hook)
10096
entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]
10197

102-
entry_tag = ('--entrypoint', entry_exe, docker_tag(repo_cmd_runner))
98+
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
10399
cmd = docker_cmd() + entry_tag + cmd_rest
104100
return xargs(cmd, file_args)

pre_commit/languages/docker_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
install_environment = helpers.no_install
1414

1515

16-
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
16+
def run_hook(prefix, hook, file_args): # pragma: windows no cover
1717
assert_docker_available()
1818
cmd = docker_cmd() + helpers.to_cmd(hook)
1919
return xargs(cmd, file_args)

pre_commit/languages/golang.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def get_env_patch(venv):
2626

2727

2828
@contextlib.contextmanager
29-
def in_env(repo_cmd_runner):
30-
envdir = repo_cmd_runner.path(
29+
def in_env(prefix):
30+
envdir = prefix.path(
3131
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
3232
)
3333
with envcontext(get_env_patch(envdir)):
@@ -50,20 +50,18 @@ def guess_go_dir(remote_url):
5050
return 'unknown_src_dir'
5151

5252

53-
def install_environment(repo_cmd_runner, version, additional_dependencies):
53+
def install_environment(prefix, version, additional_dependencies):
5454
helpers.assert_version_default('golang', version)
55-
directory = repo_cmd_runner.path(
55+
directory = prefix.path(
5656
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
5757
)
5858

5959
with clean_path_on_failure(directory):
60-
remote = git.get_remote_url(repo_cmd_runner.path())
60+
remote = git.get_remote_url(prefix.prefix_dir)
6161
repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))
6262

6363
# Clone into the goenv we'll create
64-
helpers.run_setup_cmd(
65-
repo_cmd_runner, ('git', 'clone', '.', repo_src_dir),
66-
)
64+
helpers.run_setup_cmd(prefix, ('git', 'clone', '.', repo_src_dir))
6765

6866
if sys.platform == 'cygwin': # pragma: no cover
6967
_, gopath, _ = cmd_output('cygpath', '-w', directory)
@@ -75,10 +73,10 @@ def install_environment(repo_cmd_runner, version, additional_dependencies):
7573
for dependency in additional_dependencies:
7674
cmd_output('go', 'get', dependency, cwd=repo_src_dir, env=env)
7775
# Same some disk space, we don't need these after installation
78-
rmtree(repo_cmd_runner.path(directory, 'src'))
79-
rmtree(repo_cmd_runner.path(directory, 'pkg'))
76+
rmtree(prefix.path(directory, 'src'))
77+
rmtree(prefix.path(directory, 'pkg'))
8078

8179

82-
def run_hook(repo_cmd_runner, hook, file_args):
83-
with in_env(repo_cmd_runner):
80+
def run_hook(prefix, hook, file_args):
81+
with in_env(prefix):
8482
return xargs(helpers.to_cmd(hook), file_args)

pre_commit/languages/helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from pre_commit.util import cmd_output
66

77

8-
def run_setup_cmd(runner, cmd):
9-
cmd_output(*cmd, cwd=runner.prefix_dir, encoding=None)
8+
def run_setup_cmd(prefix, cmd):
9+
cmd_output(*cmd, cwd=prefix.prefix_dir, encoding=None)
1010

1111

1212
def environment_dir(ENVIRONMENT_DIR, language_version):
@@ -39,9 +39,9 @@ def basic_get_default_version():
3939
return 'default'
4040

4141

42-
def basic_healthy(repo_cmd_runner, language_version):
42+
def basic_healthy(prefix, language_version):
4343
return True
4444

4545

46-
def no_install(repo_cmd_runner, version, additional_dependencies):
46+
def no_install(prefix, version, additional_dependencies):
4747
raise AssertionError('This type is not installable')

pre_commit/languages/node.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,35 @@ def get_env_patch(venv): # pragma: windows no cover
3333

3434

3535
@contextlib.contextmanager
36-
def in_env(repo_cmd_runner, language_version): # pragma: windows no cover
37-
envdir = repo_cmd_runner.path(
36+
def in_env(prefix, language_version): # pragma: windows no cover
37+
envdir = prefix.path(
3838
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
3939
)
4040
with envcontext(get_env_patch(envdir)):
4141
yield
4242

4343

4444
def install_environment(
45-
repo_cmd_runner, version, additional_dependencies,
45+
prefix, version, additional_dependencies,
4646
): # pragma: windows no cover
4747
additional_dependencies = tuple(additional_dependencies)
48-
assert repo_cmd_runner.exists('package.json')
48+
assert prefix.exists('package.json')
4949
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
5050

51-
env_dir = repo_cmd_runner.path(directory)
51+
env_dir = prefix.path(directory)
5252
with clean_path_on_failure(env_dir):
53-
cmd = [
54-
sys.executable, '-m', 'nodeenv', '--prebuilt',
55-
'{{prefix}}{}'.format(directory),
56-
]
57-
53+
cmd = [sys.executable, '-m', 'nodeenv', '--prebuilt', env_dir]
5854
if version != 'default':
5955
cmd.extend(['-n', version])
56+
cmd_output(*cmd)
6057

61-
repo_cmd_runner.run(cmd)
62-
63-
with in_env(repo_cmd_runner, version):
58+
with in_env(prefix, version):
6459
helpers.run_setup_cmd(
65-
repo_cmd_runner,
60+
prefix,
6661
('npm', 'install', '-g', '.') + additional_dependencies,
6762
)
6863

6964

70-
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
71-
with in_env(repo_cmd_runner, hook['language_version']):
65+
def run_hook(prefix, hook, file_args): # pragma: windows no cover
66+
with in_env(prefix, hook['language_version']):
7267
return xargs(helpers.to_cmd(hook), file_args)

pre_commit/languages/pcre.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
install_environment = helpers.no_install
1414

1515

16-
def run_hook(repo_cmd_runner, hook, file_args):
16+
def run_hook(prefix, hook, file_args):
1717
# For PCRE the entry is the regular expression to match
1818
cmd = (GREP, '-H', '-n', '-P') + tuple(hook['args']) + (hook['entry'],)
1919

pre_commit/languages/pygrep.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _process_filename_by_line(pattern, filename):
2727
return retv
2828

2929

30-
def run_hook(repo_cmd_runner, hook, file_args):
30+
def run_hook(prefix, hook, file_args):
3131
exe = (sys.executable, '-m', __name__)
3232
exe += tuple(hook['args']) + (hook['entry'],)
3333
return xargs(exe, file_args)

pre_commit/languages/python.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def get_env_patch(venv):
3333

3434

3535
@contextlib.contextmanager
36-
def in_env(repo_cmd_runner, language_version):
37-
envdir = repo_cmd_runner.path(
36+
def in_env(prefix, language_version):
37+
envdir = prefix.path(
3838
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
3939
)
4040
with envcontext(get_env_patch(envdir)):
@@ -98,8 +98,8 @@ def get_default_version():
9898
return get_default_version()
9999

100100

101-
def healthy(repo_cmd_runner, language_version):
102-
with in_env(repo_cmd_runner, language_version):
101+
def healthy(prefix, language_version):
102+
with in_env(prefix, language_version):
103103
retcode, _, _ = cmd_output(
104104
'python', '-c', 'import ctypes, datetime, io, os, ssl, weakref',
105105
retcode=None,
@@ -127,29 +127,26 @@ def norm_version(version):
127127
return os.path.expanduser(version)
128128

129129

130-
def install_environment(repo_cmd_runner, version, additional_dependencies):
130+
def install_environment(prefix, version, additional_dependencies):
131131
additional_dependencies = tuple(additional_dependencies)
132132
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
133133

134134
# Install a virtualenv
135-
with clean_path_on_failure(repo_cmd_runner.path(directory)):
136-
venv_cmd = [
137-
sys.executable, '-m', 'virtualenv',
138-
'{{prefix}}{}'.format(directory),
139-
]
135+
env_dir = prefix.path(directory)
136+
with clean_path_on_failure(env_dir):
137+
venv_cmd = [sys.executable, '-m', 'virtualenv', env_dir]
140138
if version != 'default':
141139
venv_cmd.extend(['-p', norm_version(version)])
142140
else:
143141
venv_cmd.extend(['-p', os.path.realpath(sys.executable)])
144142
venv_env = dict(os.environ, VIRTUALENV_NO_DOWNLOAD='1')
145-
repo_cmd_runner.run(venv_cmd, cwd='/', env=venv_env)
146-
with in_env(repo_cmd_runner, version):
143+
cmd_output(*venv_cmd, cwd='/', env=venv_env)
144+
with in_env(prefix, version):
147145
helpers.run_setup_cmd(
148-
repo_cmd_runner,
149-
('pip', 'install', '.') + additional_dependencies,
146+
prefix, ('pip', 'install', '.') + additional_dependencies,
150147
)
151148

152149

153-
def run_hook(repo_cmd_runner, hook, file_args):
154-
with in_env(repo_cmd_runner, hook['language_version']):
150+
def run_hook(prefix, hook, file_args):
151+
with in_env(prefix, hook['language_version']):
155152
return xargs(helpers.to_cmd(hook), file_args)

0 commit comments

Comments
 (0)