show color in hook outputs when attached to a tty

This commit is contained in:
Anthony Sottile
2019-10-12 15:57:40 -07:00
parent c8620f35e1
commit 7c3404ef1f
27 changed files with 200 additions and 76 deletions

View File

@@ -38,16 +38,17 @@ from pre_commit.languages import system
# version - A version specified in the hook configuration or 'default'.
# """
#
# def run_hook(hook, file_args):
# def run_hook(hook, file_args, color):
# """Runs a hook and returns the returncode and output of running that
# hook.
#
# Args:
# hook - `Hook`
# file_args - The files to be run
# color - whether the hook should be given a pty (when supported)
#
# Returns:
# (returncode, stdout, stderr)
# (returncode, output)
# """
languages = {

View File

@@ -95,15 +95,15 @@ def docker_cmd(): # pragma: windows no cover
)
def run_hook(hook, file_args): # pragma: windows no cover
def run_hook(hook, file_args, color): # 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(hook.prefix, pull=False)
hook_cmd = helpers.to_cmd(hook)
entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]
hook_cmd = hook.cmd
entry_exe, cmd_rest = hook.cmd[0], hook_cmd[1:]
entry_tag = ('--entrypoint', entry_exe, docker_tag(hook.prefix))
cmd = docker_cmd() + entry_tag + cmd_rest
return helpers.run_xargs(hook, cmd, file_args)
return helpers.run_xargs(hook, cmd, file_args, color=color)

View File

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

View File

@@ -9,7 +9,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(hook, file_args):
def run_hook(hook, file_args, color):
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

View File

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

View File

@@ -3,7 +3,6 @@ from __future__ import unicode_literals
import multiprocessing
import os
import random
import shlex
import six
@@ -25,10 +24,6 @@ def environment_dir(ENVIRONMENT_DIR, language_version):
return '{}-{}'.format(ENVIRONMENT_DIR, language_version)
def to_cmd(hook):
return tuple(shlex.split(hook.entry)) + tuple(hook.args)
def assert_version_default(binary, version):
if version != C.DEFAULT:
raise AssertionError(
@@ -83,8 +78,9 @@ def _shuffled(seq):
return seq
def run_xargs(hook, cmd, file_args):
def run_xargs(hook, cmd, file_args, **kwargs):
# Shuffle the files so that they more evenly fill out the xargs partitions,
# but do it deterministically in case a hook cares about ordering.
file_args = _shuffled(file_args)
return xargs(cmd, file_args, target_concurrency=target_concurrency(hook))
kwargs['target_concurrency'] = target_concurrency(hook)
return xargs(cmd, file_args, **kwargs)

View File

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

View File

@@ -13,10 +13,10 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(hook, file_args):
def run_hook(hook, file_args, color):
# For PCRE the entry is the regular expression to match
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.
return xargs(cmd, file_args, negate=True)
return xargs(cmd, file_args, negate=True, color=color)

View File

@@ -44,9 +44,9 @@ def _process_filename_at_once(pattern, filename):
return retv
def run_hook(hook, file_args):
def run_hook(hook, file_args, color):
exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,)
return xargs(exe, file_args)
return xargs(exe, file_args, color=color)
def main(argv=None):

View File

@@ -151,9 +151,9 @@ def py_interface(_dir, _make_venv):
)
return retcode == 0
def run_hook(hook, file_args):
def run_hook(hook, file_args, color):
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
def install_environment(prefix, version, additional_dependencies):
additional_dependencies = tuple(additional_dependencies)

View File

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

View File

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

View File

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

View File

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

View File

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