Merge pull request #2701 from pre-commit/run-hook-no-Hook

adjust the run_hook api to no longer take Hook
This commit is contained in:
Anthony Sottile 2023-01-16 17:11:10 -05:00 committed by GitHub
commit 59ed51a309
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 163 additions and 192 deletions

View file

@ -190,7 +190,14 @@ def _run_single_hook(
time_before = time.time() time_before = time.time()
language = languages[hook.language] language = languages[hook.language]
with language.in_env(hook.prefix, hook.language_version): with language.in_env(hook.prefix, hook.language_version):
retcode, out = language.run_hook(hook, filenames, use_color) retcode, out = language.run_hook(
hook.prefix,
hook.entry,
hook.args,
filenames,
require_serial=hook.require_serial,
color=use_color,
)
duration = round(time.time() - time_before, 2) or 0 duration = round(time.time() - time_before, 2) or 0
diff_after = _get_diff() diff_after = _get_diff()

View file

@ -1,7 +1,6 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
import shlex
from typing import Any from typing import Any
from typing import NamedTuple from typing import NamedTuple
from typing import Sequence from typing import Sequence
@ -37,10 +36,6 @@ class Hook(NamedTuple):
stages: Sequence[str] stages: Sequence[str]
verbose: bool verbose: bool
@property
def cmd(self) -> tuple[str, ...]:
return (*shlex.split(self.entry), *self.args)
@property @property
def install_key(self) -> tuple[Prefix, str, str, tuple[str, ...]]: def install_key(self) -> tuple[Prefix, str, str, tuple[str, ...]]:
return ( return (

View file

@ -4,7 +4,6 @@ from typing import ContextManager
from typing import Protocol from typing import Protocol
from typing import Sequence from typing import Sequence
from pre_commit.hook import Hook
from pre_commit.languages import conda from pre_commit.languages import conda
from pre_commit.languages import coursier from pre_commit.languages import coursier
from pre_commit.languages import dart from pre_commit.languages import dart
@ -62,8 +61,12 @@ class Language(Protocol):
# execute a hook and return the exit code and output # execute a hook and return the exit code and output
def run_hook( def run_hook(
self, self,
hook: Hook, prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*,
require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
... ...

View file

@ -10,7 +10,6 @@ from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import SubstitutionT from pre_commit.envcontext import SubstitutionT
from pre_commit.envcontext import UNSET from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output_b from pre_commit.util import cmd_output_b
@ -18,6 +17,7 @@ from pre_commit.util import cmd_output_b
ENVIRONMENT_DIR = 'conda' ENVIRONMENT_DIR = 'conda'
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
def get_env_patch(env: str) -> PatchesT: def get_env_patch(env: str) -> PatchesT:
@ -74,15 +74,3 @@ def install_environment(
conda_exe, 'install', '-p', env_dir, *additional_dependencies, conda_exe, 'install', '-p', env_dir, *additional_dependencies,
cwd=prefix.prefix_dir, cwd=prefix.prefix_dir,
) )
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
# TODO: Some rare commands need to be run using `conda run` but mostly we
# can run them without which is much quicker and produces a better
# output.
# cmd = ('conda', 'run', '-p', env_dir) + hook.cmd
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -8,7 +8,6 @@ from typing import Sequence
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.parse_shebang import find_executable from pre_commit.parse_shebang import find_executable
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
@ -17,6 +16,7 @@ ENVIRONMENT_DIR = 'coursier'
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
def install_environment( def install_environment(
@ -64,11 +64,3 @@ def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version) envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
with envcontext(get_env_patch(envdir)): with envcontext(get_env_patch(envdir)):
yield yield
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -10,7 +10,6 @@ from typing import Sequence
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import win_exe from pre_commit.util import win_exe
@ -20,6 +19,7 @@ ENVIRONMENT_DIR = 'dartenv'
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
def get_env_patch(venv: str) -> PatchesT: def get_env_patch(venv: str) -> PatchesT:
@ -95,11 +95,3 @@ def install_environment(
raise AssertionError( raise AssertionError(
f'could not find pubspec.yaml for {dep_s}', f'could not find pubspec.yaml for {dep_s}',
) )
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -5,7 +5,6 @@ import json
import os import os
from typing import Sequence from typing import Sequence
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import CalledProcessError from pre_commit.util import CalledProcessError
@ -123,16 +122,25 @@ def docker_cmd() -> tuple[str, ...]: # pragma: win32 no cover
def run_hook( def run_hook(
hook: Hook, prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*,
require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover ) -> tuple[int, bytes]: # pragma: win32 no cover
# Rebuild the docker image in case it has gone missing, as many people do # Rebuild the docker image in case it has gone missing, as many people do
# automated cleanup of docker images. # automated cleanup of docker images.
build_docker_image(hook.prefix, pull=False) build_docker_image(prefix, pull=False)
entry_exe, *cmd_rest = hook.cmd entry_exe, *cmd_rest = helpers.hook_cmd(entry, args)
entry_tag = ('--entrypoint', entry_exe, docker_tag(hook.prefix)) entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
cmd = (*docker_cmd(), *entry_tag, *cmd_rest) cmd = (*docker_cmd(), *entry_tag, *cmd_rest)
return helpers.run_xargs(hook, cmd, file_args, color=color) return helpers.run_xargs(
cmd,
file_args,
require_serial=require_serial,
color=color,
)

View file

@ -2,9 +2,9 @@ from __future__ import annotations
from typing import Sequence from typing import Sequence
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.languages.docker import docker_cmd from pre_commit.languages.docker import docker_cmd
from pre_commit.prefix import Prefix
ENVIRONMENT_DIR = None ENVIRONMENT_DIR = None
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
@ -14,9 +14,18 @@ in_env = helpers.no_env
def run_hook( def run_hook(
hook: Hook, prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*,
require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover ) -> tuple[int, bytes]: # pragma: win32 no cover
cmd = docker_cmd() + hook.cmd cmd = docker_cmd() + helpers.hook_cmd(entry, args)
return helpers.run_xargs(hook, cmd, file_args, color=color) return helpers.run_xargs(
cmd,
file_args,
require_serial=require_serial,
color=color,
)

View file

@ -12,7 +12,6 @@ from typing import Sequence
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
@ -21,6 +20,7 @@ BIN_DIR = 'bin'
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
def get_env_patch(venv: str) -> PatchesT: def get_env_patch(venv: str) -> PatchesT:
@ -113,11 +113,3 @@ def install_environment(
# Clean the git dir, ignoring the environment dir # Clean the git dir, ignoring the environment dir
clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*') clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*')
helpers.run_setup_cmd(prefix, clean_cmd) helpers.run_setup_cmd(prefix, clean_cmd)
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -2,8 +2,8 @@ from __future__ import annotations
from typing import Sequence from typing import Sequence
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix
ENVIRONMENT_DIR = None ENVIRONMENT_DIR = None
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
@ -13,10 +13,14 @@ in_env = helpers.no_env
def run_hook( def run_hook(
hook: Hook, prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*,
require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
out = f'{hook.entry}\n\n'.encode() out = f'{entry}\n\n'.encode()
out += b'\n'.join(f.encode() for f in file_args) + b'\n' out += b'\n'.join(f.encode() for f in file_args) + b'\n'
return 1, out return 1, out

View file

@ -22,7 +22,6 @@ import pre_commit.constants as C
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
@ -30,6 +29,7 @@ from pre_commit.util import rmtree
ENVIRONMENT_DIR = 'golangenv' ENVIRONMENT_DIR = 'golangenv'
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
_ARCH_ALIASES = { _ARCH_ALIASES = {
'x86_64': 'amd64', 'x86_64': 'amd64',
@ -157,11 +157,3 @@ def install_environment(
pkgdir = os.path.join(env_dir, 'pkg') pkgdir = os.path.join(env_dir, 'pkg')
if os.path.exists(pkgdir): # pragma: no branch (always true on windows?) if os.path.exists(pkgdir): # pragma: no branch (always true on windows?)
rmtree(pkgdir) rmtree(pkgdir)
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -5,6 +5,7 @@ import multiprocessing
import os import os
import random import random
import re import re
import shlex
from typing import Any from typing import Any
from typing import Generator from typing import Generator
from typing import NoReturn from typing import NoReturn
@ -12,7 +13,6 @@ from typing import Sequence
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit import parse_shebang from pre_commit import parse_shebang
from pre_commit.hook import Hook
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output_b from pre_commit.util import cmd_output_b
from pre_commit.xargs import xargs from pre_commit.xargs import xargs
@ -94,8 +94,8 @@ def no_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
yield yield
def target_concurrency(hook: Hook) -> int: def target_concurrency() -> int:
if hook.require_serial or 'PRE_COMMIT_NO_CONCURRENCY' in os.environ: if 'PRE_COMMIT_NO_CONCURRENCY' in os.environ:
return 1 return 1
else: else:
# Travis appears to have a bunch of CPUs, but we can't use them all. # Travis appears to have a bunch of CPUs, but we can't use them all.
@ -119,13 +119,39 @@ def _shuffled(seq: Sequence[str]) -> list[str]:
def run_xargs( def run_xargs(
hook: Hook,
cmd: tuple[str, ...], cmd: tuple[str, ...],
file_args: Sequence[str], file_args: Sequence[str],
**kwargs: Any, *,
require_serial: bool,
color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
# Shuffle the files so that they more evenly fill out the xargs partitions, if require_serial:
# but do it deterministically in case a hook cares about ordering. jobs = 1
else:
# 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) file_args = _shuffled(file_args)
kwargs['target_concurrency'] = target_concurrency(hook) jobs = target_concurrency()
return xargs(cmd, file_args, **kwargs) return xargs(cmd, file_args, target_concurrency=jobs, color=color)
def hook_cmd(entry: str, args: Sequence[str]) -> tuple[str, ...]:
return (*shlex.split(entry), *args)
def basic_run_hook(
prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str],
*,
require_serial: bool,
color: bool,
) -> tuple[int, bytes]:
return run_xargs(
hook_cmd(entry, args),
file_args,
require_serial=require_serial,
color=color,
)

View file

@ -9,7 +9,6 @@ from typing import Sequence
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
@ -17,6 +16,7 @@ from pre_commit.util import cmd_output
ENVIRONMENT_DIR = 'lua_env' ENVIRONMENT_DIR = 'lua_env'
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
def _get_lua_version() -> str: # pragma: win32 no cover def _get_lua_version() -> str: # pragma: win32 no cover
@ -73,11 +73,3 @@ def install_environment(
for dependency in additional_dependencies: for dependency in additional_dependencies:
cmd = ('luarocks', '--tree', envdir, 'install', dependency) cmd = ('luarocks', '--tree', envdir, 'install', dependency)
helpers.run_setup_cmd(prefix, cmd) helpers.run_setup_cmd(prefix, cmd)
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -12,7 +12,6 @@ from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import UNSET from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.languages.python import bin_dir from pre_commit.languages.python import bin_dir
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
@ -21,6 +20,7 @@ from pre_commit.util import cmd_output_b
from pre_commit.util import rmtree from pre_commit.util import rmtree
ENVIRONMENT_DIR = 'node_env' ENVIRONMENT_DIR = 'node_env'
run_hook = helpers.basic_run_hook
@functools.lru_cache(maxsize=1) @functools.lru_cache(maxsize=1)
@ -108,11 +108,3 @@ def install_environment(
if prefix.exists('node_modules'): # pragma: win32 no cover if prefix.exists('node_modules'): # pragma: win32 no cover
rmtree(prefix.path('node_modules')) rmtree(prefix.path('node_modules'))
os.remove(pkg) os.remove(pkg)
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -9,13 +9,13 @@ from typing import Sequence
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
ENVIRONMENT_DIR = 'perl_env' ENVIRONMENT_DIR = 'perl_env'
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
def get_env_patch(venv: str) -> PatchesT: def get_env_patch(venv: str) -> PatchesT:
@ -48,11 +48,3 @@ def install_environment(
helpers.run_setup_cmd( helpers.run_setup_cmd(
prefix, ('cpan', '-T', '.', *additional_dependencies), prefix, ('cpan', '-T', '.', *additional_dependencies),
) )
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -8,8 +8,8 @@ from typing import Pattern
from typing import Sequence from typing import Sequence
from pre_commit import output from pre_commit import output
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix
from pre_commit.xargs import xargs from pre_commit.xargs import xargs
ENVIRONMENT_DIR = None ENVIRONMENT_DIR = None
@ -88,12 +88,16 @@ FNS = {
def run_hook( def run_hook(
hook: Hook, prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*,
require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
exe = (sys.executable, '-m', __name__) + tuple(hook.args) + (hook.entry,) cmd = (sys.executable, '-m', __name__, *args, entry)
return xargs(exe, file_args, color=color) return xargs(cmd, file_args, color=color)
def main(argv: Sequence[str] | None = None) -> int: def main(argv: Sequence[str] | None = None) -> int:

View file

@ -12,7 +12,6 @@ from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import UNSET from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.parse_shebang import find_executable from pre_commit.parse_shebang import find_executable
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
@ -22,6 +21,7 @@ from pre_commit.util import cmd_output_b
from pre_commit.util import win_exe from pre_commit.util import win_exe
ENVIRONMENT_DIR = 'py_env' ENVIRONMENT_DIR = 'py_env'
run_hook = helpers.basic_run_hook
@functools.lru_cache(maxsize=None) @functools.lru_cache(maxsize=None)
@ -212,11 +212,3 @@ def install_environment(
cmd_output_b(*venv_cmd, cwd='/') cmd_output_b(*venv_cmd, cwd='/')
with in_env(prefix, version): with in_env(prefix, version):
helpers.run_setup_cmd(prefix, install_cmd) helpers.run_setup_cmd(prefix, install_cmd)
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -10,7 +10,6 @@ from typing import Sequence
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import UNSET from pre_commit.envcontext import UNSET
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output_b from pre_commit.util import cmd_output_b
@ -70,15 +69,15 @@ def _entry_validate(entry: list[str]) -> None:
) )
def _cmd_from_hook(hook: Hook) -> tuple[str, ...]: def _cmd_from_hook(
entry = shlex.split(hook.entry) prefix: Prefix,
_entry_validate(entry) entry: str,
args: Sequence[str],
) -> tuple[str, ...]:
cmd = shlex.split(entry)
_entry_validate(cmd)
return ( return (cmd[0], *RSCRIPT_OPTS, *_prefix_if_file_entry(cmd, prefix), *args)
entry[0], *RSCRIPT_OPTS,
*_prefix_if_file_entry(entry, hook.prefix),
*hook.args,
)
def install_environment( def install_environment(
@ -149,10 +148,18 @@ def _inline_r_setup(code: str) -> str:
def run_hook( def run_hook(
hook: Hook, prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*,
require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
cmd = _cmd_from_hook(prefix, entry, args)
return helpers.run_xargs( return helpers.run_xargs(
hook, _cmd_from_hook(hook), file_args, color=color, cmd,
file_args,
require_serial=require_serial,
color=color,
) )

View file

@ -13,7 +13,6 @@ from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import UNSET from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import CalledProcessError from pre_commit.util import CalledProcessError
@ -21,6 +20,7 @@ from pre_commit.util import resource_bytesio
ENVIRONMENT_DIR = 'rbenv' ENVIRONMENT_DIR = 'rbenv'
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
@functools.lru_cache(maxsize=1) @functools.lru_cache(maxsize=1)
@ -133,11 +133,3 @@ def install_environment(
*prefix.star('.gem'), *additional_dependencies, *prefix.star('.gem'), *additional_dependencies,
), ),
) )
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -15,7 +15,6 @@ from pre_commit import parse_shebang
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output_b from pre_commit.util import cmd_output_b
@ -24,6 +23,7 @@ from pre_commit.util import win_exe
ENVIRONMENT_DIR = 'rustenv' ENVIRONMENT_DIR = 'rustenv'
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
run_hook = helpers.basic_run_hook
@functools.lru_cache(maxsize=1) @functools.lru_cache(maxsize=1)
@ -154,11 +154,3 @@ def install_environment(
'cargo', 'install', '--bins', '--root', envdir, *args, 'cargo', 'install', '--bins', '--root', envdir, *args,
cwd=prefix.prefix_dir, cwd=prefix.prefix_dir,
) )
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -2,8 +2,8 @@ from __future__ import annotations
from typing import Sequence from typing import Sequence
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix
ENVIRONMENT_DIR = None ENVIRONMENT_DIR = None
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
@ -13,9 +13,19 @@ in_env = helpers.no_env
def run_hook( def run_hook(
hook: Hook, prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str], file_args: Sequence[str],
*,
require_serial: bool,
color: bool, color: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
cmd = (hook.prefix.path(hook.cmd[0]), *hook.cmd[1:]) cmd = helpers.hook_cmd(entry, args)
return helpers.run_xargs(hook, cmd, file_args, color=color) cmd = (prefix.path(cmd[0]), *cmd[1:])
return helpers.run_xargs(
cmd,
file_args,
require_serial=require_serial,
color=color,
)

View file

@ -8,16 +8,17 @@ from typing import Sequence
from pre_commit.envcontext import envcontext from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output_b from pre_commit.util import cmd_output_b
BUILD_DIR = '.build'
BUILD_CONFIG = 'release'
ENVIRONMENT_DIR = 'swift_env' ENVIRONMENT_DIR = 'swift_env'
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
BUILD_DIR = '.build' run_hook = helpers.basic_run_hook
BUILD_CONFIG = 'release'
def get_env_patch(venv: str) -> PatchesT: # pragma: win32 no cover def get_env_patch(venv: str) -> PatchesT: # pragma: win32 no cover
@ -47,11 +48,3 @@ def install_environment(
'-c', BUILD_CONFIG, '-c', BUILD_CONFIG,
'--build-path', os.path.join(envdir, BUILD_DIR), '--build-path', os.path.join(envdir, BUILD_DIR),
) )
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -1,8 +1,5 @@
from __future__ import annotations from __future__ import annotations
from typing import Sequence
from pre_commit.hook import Hook
from pre_commit.languages import helpers from pre_commit.languages import helpers
ENVIRONMENT_DIR = None ENVIRONMENT_DIR = None
@ -10,11 +7,4 @@ get_default_version = helpers.basic_get_default_version
health_check = helpers.basic_health_check health_check = helpers.basic_health_check
install_environment = helpers.no_install install_environment = helpers.no_install
in_env = helpers.no_env in_env = helpers.no_env
run_hook = helpers.basic_run_hook
def run_hook(
hook: Hook,
file_args: Sequence[str],
color: bool,
) -> tuple[int, bytes]:
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

View file

@ -12,7 +12,6 @@ from pre_commit import parse_shebang
from pre_commit.languages import helpers from pre_commit.languages import helpers
from pre_commit.prefix import Prefix from pre_commit.prefix import Prefix
from pre_commit.util import CalledProcessError from pre_commit.util import CalledProcessError
from testing.auto_namedtuple import auto_namedtuple
@pytest.fixture @pytest.fixture
@ -94,31 +93,22 @@ def test_assert_no_additional_deps():
) )
SERIAL_FALSE = auto_namedtuple(require_serial=False)
SERIAL_TRUE = auto_namedtuple(require_serial=True)
def test_target_concurrency_normal(): def test_target_concurrency_normal():
with mock.patch.object(multiprocessing, 'cpu_count', return_value=123): with mock.patch.object(multiprocessing, 'cpu_count', return_value=123):
with mock.patch.dict(os.environ, {}, clear=True): with mock.patch.dict(os.environ, {}, clear=True):
assert helpers.target_concurrency(SERIAL_FALSE) == 123 assert helpers.target_concurrency() == 123
def test_target_concurrency_cpu_count_require_serial_true():
with mock.patch.dict(os.environ, {}, clear=True):
assert helpers.target_concurrency(SERIAL_TRUE) == 1
def test_target_concurrency_testing_env_var(): def test_target_concurrency_testing_env_var():
with mock.patch.dict( with mock.patch.dict(
os.environ, {'PRE_COMMIT_NO_CONCURRENCY': '1'}, clear=True, os.environ, {'PRE_COMMIT_NO_CONCURRENCY': '1'}, clear=True,
): ):
assert helpers.target_concurrency(SERIAL_FALSE) == 1 assert helpers.target_concurrency() == 1
def test_target_concurrency_on_travis(): def test_target_concurrency_on_travis():
with mock.patch.dict(os.environ, {'TRAVIS': '1'}, clear=True): with mock.patch.dict(os.environ, {'TRAVIS': '1'}, clear=True):
assert helpers.target_concurrency(SERIAL_FALSE) == 2 assert helpers.target_concurrency() == 2
def test_target_concurrency_cpu_count_not_implemented(): def test_target_concurrency_cpu_count_not_implemented():
@ -126,10 +116,20 @@ def test_target_concurrency_cpu_count_not_implemented():
multiprocessing, 'cpu_count', side_effect=NotImplementedError, multiprocessing, 'cpu_count', side_effect=NotImplementedError,
): ):
with mock.patch.dict(os.environ, {}, clear=True): with mock.patch.dict(os.environ, {}, clear=True):
assert helpers.target_concurrency(SERIAL_FALSE) == 1 assert helpers.target_concurrency() == 1
def test_shuffled_is_deterministic(): def test_shuffled_is_deterministic():
seq = [str(i) for i in range(10)] seq = [str(i) for i in range(10)]
expected = ['4', '0', '5', '1', '8', '6', '2', '3', '7', '9'] expected = ['4', '0', '5', '1', '8', '6', '2', '3', '7', '9']
assert helpers._shuffled(seq) == expected assert helpers._shuffled(seq) == expected
def test_xargs_require_serial_is_not_shuffled():
ret, out = helpers.run_xargs(
('echo',), [str(i) for i in range(10)],
require_serial=True,
color=False,
)
assert ret == 0
assert out.strip() == b'0 1 2 3 4 5 6 7 8 9'

View file

@ -23,7 +23,7 @@ def _test_r_parsing(
repo = make_repo(tempdir_factory, 'r_hooks_repo') repo = make_repo(tempdir_factory, 'r_hooks_repo')
config = make_config_from_repo(repo) config = make_config_from_repo(repo)
hook = _get_hook_no_install(config, store, hook_id) hook = _get_hook_no_install(config, store, hook_id)
ret = r._cmd_from_hook(hook) ret = r._cmd_from_hook(hook.prefix, hook.entry, hook.args)
expected_path = os.path.join(hook.prefix.prefix_dir, f'{hook_id}.R') expected_path = os.path.join(hook.prefix.prefix_dir, f'{hook_id}.R')
expected = ( expected = (
'Rscript', 'Rscript',
@ -111,7 +111,7 @@ def test_r_parsing_file_local(tempdir_factory, store):
}], }],
} }
hook = _get_hook_no_install(config, store, 'local-r') hook = _get_hook_no_install(config, store, 'local-r')
ret = r._cmd_from_hook(hook) ret = r._cmd_from_hook(hook.prefix, hook.entry, hook.args)
assert ret == ( assert ret == (
'Rscript', 'Rscript',
'--no-save', '--no-restore', '--no-site-file', '--no-environ', '--no-save', '--no-restore', '--no-site-file', '--no-environ',

View file

@ -45,7 +45,14 @@ def _norm_out(b):
def _hook_run(hook, filenames, color): def _hook_run(hook, filenames, color):
with languages[hook.language].in_env(hook.prefix, hook.language_version): with languages[hook.language].in_env(hook.prefix, hook.language_version):
return languages[hook.language].run_hook(hook, filenames, color) return languages[hook.language].run_hook(
hook.prefix,
hook.entry,
hook.args,
filenames,
require_serial=hook.require_serial,
color=color,
)
def _get_hook_no_install(repo_config, store, hook_id): def _get_hook_no_install(repo_config, store, hook_id):