Use Hook api in languages

This commit is contained in:
Anthony Sottile
2018-12-31 13:16:48 -08:00
parent 5f9a667470
commit b59d7197ff
18 changed files with 41 additions and 38 deletions

View File

@@ -39,13 +39,12 @@ from pre_commit.languages import system
# 'default'.
# """
#
# def run_hook(prefix, hook, file_args):
# def run_hook(hook, file_args):
# """Runs a hook and returns the returncode and output of running that
# hook.
#
# Args:
# prefix - `Prefix` bound to the repository.
# hook - Hook dictionary
# hook - `Hook`
# file_args - The files to be run
#
# Returns:

View File

@@ -85,15 +85,15 @@ def docker_cmd():
)
def run_hook(prefix, hook, file_args): # pragma: windows no cover
def run_hook(hook, file_args): # pragma: windows no cover
assert_docker_available()
# Rebuild the docker image in case it has gone missing, as many people do
# automated cleanup of docker images.
build_docker_image(prefix, pull=False)
build_docker_image(hook.prefix, pull=False)
hook_cmd = helpers.to_cmd(hook)
entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
entry_tag = ('--entrypoint', entry_exe, docker_tag(hook.prefix))
cmd = docker_cmd() + entry_tag + cmd_rest
return helpers.run_xargs(hook, cmd, file_args)

View File

@@ -12,7 +12,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args): # pragma: windows no cover
def run_hook(hook, file_args): # pragma: windows no cover
assert_docker_available()
cmd = docker_cmd() + helpers.to_cmd(hook)
return helpers.run_xargs(hook, cmd, file_args)

View File

@@ -9,7 +9,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
out = hook['entry'].encode('UTF-8') + b'\n\n'
def run_hook(hook, file_args):
out = hook.entry.encode('UTF-8') + b'\n\n'
out += b'\n'.join(f.encode('UTF-8') for f in file_args) + b'\n'
return 1, out, b''

View File

@@ -78,6 +78,6 @@ def install_environment(prefix, version, additional_dependencies):
rmtree(pkgdir)
def run_hook(prefix, hook, file_args):
with in_env(prefix):
def run_hook(hook, file_args):
with in_env(hook.prefix):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -26,7 +26,7 @@ def environment_dir(ENVIRONMENT_DIR, language_version):
def to_cmd(hook):
return tuple(shlex.split(hook['entry'])) + tuple(hook['args'])
return tuple(shlex.split(hook.entry)) + tuple(hook.args)
def assert_version_default(binary, version):
@@ -57,7 +57,7 @@ def no_install(prefix, version, additional_dependencies):
def target_concurrency(hook):
if hook['require_serial'] or 'PRE_COMMIT_NO_CONCURRENCY' in os.environ:
if hook.require_serial or 'PRE_COMMIT_NO_CONCURRENCY' in os.environ:
return 1
else:
# Travis appears to have a bunch of CPUs, but we can't use them all.

View File

@@ -68,6 +68,6 @@ def install_environment(prefix, version, additional_dependencies):
)
def run_hook(prefix, hook, file_args):
with in_env(prefix, hook['language_version']):
def run_hook(hook, file_args):
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -13,9 +13,9 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
def run_hook(hook, file_args):
# For PCRE the entry is the regular expression to match
cmd = (GREP, '-H', '-n', '-P') + tuple(hook['args']) + (hook['entry'],)
cmd = (GREP, '-H', '-n', '-P') + tuple(hook.args) + (hook.entry,)
# Grep usually returns 0 for matches, and nonzero for non-matches so we
# negate it here.

View File

@@ -44,9 +44,8 @@ def _process_filename_at_once(pattern, filename):
return retv
def run_hook(prefix, hook, file_args):
exe = (sys.executable, '-m', __name__)
exe += tuple(hook['args']) + (hook['entry'],)
def run_hook(hook, file_args):
exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,)
return xargs(exe, file_args)

View File

@@ -124,8 +124,8 @@ def py_interface(_dir, _make_venv):
)
return retcode == 0
def run_hook(prefix, hook, file_args):
with in_env(prefix, hook['language_version']):
def run_hook(hook, file_args):
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)
def install_environment(prefix, version, additional_dependencies):

View File

@@ -123,6 +123,6 @@ def install_environment(
)
def run_hook(prefix, hook, file_args): # pragma: windows no cover
with in_env(prefix, hook['language_version']):
def run_hook(hook, file_args): # pragma: windows no cover
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -88,6 +88,6 @@ def install_environment(prefix, version, additional_dependencies):
)
def run_hook(prefix, hook, file_args):
with in_env(prefix):
def run_hook(hook, file_args):
with in_env(hook.prefix):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -9,7 +9,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
def run_hook(hook, file_args):
cmd = helpers.to_cmd(hook)
cmd = (prefix.path(cmd[0]),) + cmd[1:]
cmd = (hook.prefix.path(cmd[0]),) + cmd[1:]
return helpers.run_xargs(hook, cmd, file_args)

View File

@@ -50,6 +50,6 @@ def install_environment(
)
def run_hook(prefix, hook, file_args): # pragma: windows no cover
with in_env(prefix):
def run_hook(hook, file_args): # pragma: windows no cover
with in_env(hook.prefix):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

View File

@@ -9,5 +9,5 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(prefix, hook, file_args):
def run_hook(hook, file_args):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)