mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 04:50:20 -06:00
Merge pull request #2700 from pre-commit/in-env-api
make in_env part of the language api
This commit is contained in:
@@ -189,7 +189,8 @@ def _run_single_hook(
|
||||
filenames = ()
|
||||
time_before = time.time()
|
||||
language = languages[hook.language]
|
||||
retcode, out = language.run_hook(hook, filenames, use_color)
|
||||
with language.in_env(hook.prefix, hook.language_version):
|
||||
retcode, out = language.run_hook(hook, filenames, use_color)
|
||||
duration = round(time.time() - time_before, 2) or 0
|
||||
diff_after = _get_diff()
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import ContextManager
|
||||
from typing import Protocol
|
||||
from typing import Sequence
|
||||
|
||||
@@ -50,6 +51,14 @@ class Language(Protocol):
|
||||
) -> None:
|
||||
...
|
||||
|
||||
# modify the environment for hook execution
|
||||
def in_env(
|
||||
self,
|
||||
prefix: Prefix,
|
||||
version: str,
|
||||
) -> ContextManager[None]:
|
||||
...
|
||||
|
||||
# execute a hook and return the exit code and output
|
||||
def run_hook(
|
||||
self,
|
||||
|
||||
@@ -40,11 +40,8 @@ def get_env_patch(env: str) -> PatchesT:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -88,5 +85,4 @@ def run_hook(
|
||||
# can run them without which is much quicker and produces a better
|
||||
# output.
|
||||
# cmd = ('conda', 'run', '-p', env_dir) + hook.cmd
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -59,12 +59,9 @@ def get_env_patch(target_dir: str) -> PatchesT: # pragma: win32 no cover
|
||||
)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]: # pragma: win32 no cover
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
@contextlib.contextmanager # pragma: win32 no cover
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -74,5 +71,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]: # pragma: win32 no cover
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -7,7 +7,6 @@ import tempfile
|
||||
from typing import Generator
|
||||
from typing import Sequence
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.envcontext import PatchesT
|
||||
from pre_commit.envcontext import Var
|
||||
@@ -30,8 +29,8 @@ def get_env_patch(venv: str) -> PatchesT:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -103,5 +102,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -5,7 +5,6 @@ import json
|
||||
import os
|
||||
from typing import Sequence
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.hook import Hook
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.prefix import Prefix
|
||||
@@ -16,6 +15,7 @@ ENVIRONMENT_DIR = 'docker'
|
||||
PRE_COMMIT_LABEL = 'PRE_COMMIT'
|
||||
get_default_version = helpers.basic_get_default_version
|
||||
health_check = helpers.basic_health_check
|
||||
in_env = helpers.no_env # no special environment for docker
|
||||
|
||||
|
||||
def _is_in_docker() -> bool:
|
||||
@@ -94,7 +94,7 @@ def install_environment(
|
||||
helpers.assert_version_default('docker', version)
|
||||
helpers.assert_no_additional_deps('docker', additional_dependencies)
|
||||
|
||||
directory = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
directory = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
|
||||
# Docker doesn't really have relevant disk environment, but pre-commit
|
||||
# still needs to cleanup its state files on failure
|
||||
|
||||
@@ -10,6 +10,7 @@ ENVIRONMENT_DIR = None
|
||||
get_default_version = helpers.basic_get_default_version
|
||||
health_check = helpers.basic_health_check
|
||||
install_environment = helpers.no_install
|
||||
in_env = helpers.no_env
|
||||
|
||||
|
||||
def run_hook(
|
||||
|
||||
@@ -9,7 +9,6 @@ import zipfile
|
||||
from typing import Generator
|
||||
from typing import Sequence
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.envcontext import PatchesT
|
||||
from pre_commit.envcontext import Var
|
||||
@@ -31,8 +30,8 @@ def get_env_patch(venv: str) -> PatchesT:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -121,5 +120,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -9,6 +9,7 @@ ENVIRONMENT_DIR = None
|
||||
get_default_version = helpers.basic_get_default_version
|
||||
health_check = helpers.basic_health_check
|
||||
install_environment = helpers.no_install
|
||||
in_env = helpers.no_env
|
||||
|
||||
|
||||
def run_hook(
|
||||
|
||||
@@ -164,5 +164,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import multiprocessing
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
from typing import Any
|
||||
from typing import Generator
|
||||
from typing import NoReturn
|
||||
from typing import Sequence
|
||||
|
||||
@@ -84,7 +86,12 @@ def no_install(
|
||||
version: str,
|
||||
additional_dependencies: Sequence[str],
|
||||
) -> NoReturn:
|
||||
raise AssertionError('This type is not installable')
|
||||
raise AssertionError('This language is not installable')
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def no_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
yield
|
||||
|
||||
|
||||
def target_concurrency(hook: Hook) -> int:
|
||||
|
||||
@@ -6,7 +6,6 @@ import sys
|
||||
from typing import Generator
|
||||
from typing import Sequence
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.envcontext import PatchesT
|
||||
from pre_commit.envcontext import Var
|
||||
@@ -45,8 +44,8 @@ def get_env_patch(d: str) -> PatchesT: # pragma: win32 no cover
|
||||
|
||||
|
||||
@contextlib.contextmanager # pragma: win32 no cover
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -58,8 +57,8 @@ def install_environment(
|
||||
) -> None: # pragma: win32 no cover
|
||||
helpers.assert_version_default('lua', version)
|
||||
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
with in_env(prefix):
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with in_env(prefix, version):
|
||||
# luarocks doesn't bootstrap a tree prior to installing
|
||||
# so ensure the directory exists.
|
||||
os.makedirs(envdir, exist_ok=True)
|
||||
@@ -81,5 +80,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]: # pragma: win32 no cover
|
||||
with in_env(hook.prefix):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -59,11 +59,8 @@ def get_env_patch(venv: str) -> PatchesT:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -118,5 +115,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -33,11 +33,8 @@ def get_env_patch(venv: str) -> PatchesT:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -58,5 +55,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -16,6 +16,7 @@ ENVIRONMENT_DIR = None
|
||||
get_default_version = helpers.basic_get_default_version
|
||||
health_check = helpers.basic_health_check
|
||||
install_environment = helpers.no_install
|
||||
in_env = helpers.no_env
|
||||
|
||||
|
||||
def _process_filename_by_line(pattern: Pattern[bytes], filename: str) -> int:
|
||||
|
||||
@@ -152,11 +152,8 @@ def norm_version(version: str) -> str | None:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -222,5 +219,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -30,11 +30,8 @@ def get_env_patch(venv: str) -> PatchesT:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -156,7 +153,6 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(
|
||||
hook, _cmd_from_hook(hook), file_args, color=color,
|
||||
)
|
||||
return helpers.run_xargs(
|
||||
hook, _cmd_from_hook(hook), file_args, color=color,
|
||||
)
|
||||
|
||||
@@ -67,12 +67,9 @@ def get_env_patch(
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir, language_version)):
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir, version)):
|
||||
yield
|
||||
|
||||
|
||||
@@ -143,5 +140,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -62,12 +62,9 @@ def get_env_patch(target_dir: str, version: str) -> PatchesT:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir, language_version)):
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir, version)):
|
||||
yield
|
||||
|
||||
|
||||
@@ -164,5 +161,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]:
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -9,6 +9,7 @@ ENVIRONMENT_DIR = None
|
||||
get_default_version = helpers.basic_get_default_version
|
||||
health_check = helpers.basic_health_check
|
||||
install_environment = helpers.no_install
|
||||
in_env = helpers.no_env
|
||||
|
||||
|
||||
def run_hook(
|
||||
|
||||
@@ -5,7 +5,6 @@ import os
|
||||
from typing import Generator
|
||||
from typing import Sequence
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.envcontext import PatchesT
|
||||
from pre_commit.envcontext import Var
|
||||
@@ -27,8 +26,8 @@ def get_env_patch(venv: str) -> PatchesT: # pragma: win32 no cover
|
||||
|
||||
|
||||
@contextlib.contextmanager # pragma: win32 no cover
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
@@ -38,7 +37,7 @@ def install_environment(
|
||||
) -> None: # pragma: win32 no cover
|
||||
helpers.assert_version_default('swift', version)
|
||||
helpers.assert_no_additional_deps('swift', additional_dependencies)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
|
||||
# Build the swift package
|
||||
os.mkdir(envdir)
|
||||
@@ -55,5 +54,4 @@ def run_hook(
|
||||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]: # pragma: win32 no cover
|
||||
with in_env(hook.prefix):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
||||
@@ -5,11 +5,11 @@ from typing import Sequence
|
||||
from pre_commit.hook import Hook
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
|
||||
ENVIRONMENT_DIR = None
|
||||
get_default_version = helpers.basic_get_default_version
|
||||
health_check = helpers.basic_health_check
|
||||
install_environment = helpers.no_install
|
||||
in_env = helpers.no_env
|
||||
|
||||
|
||||
def run_hook(
|
||||
|
||||
@@ -44,7 +44,8 @@ def _norm_out(b):
|
||||
|
||||
|
||||
def _hook_run(hook, filenames, color):
|
||||
return languages[hook.language].run_hook(hook, filenames, color)
|
||||
with languages[hook.language].in_env(hook.prefix, hook.language_version):
|
||||
return languages[hook.language].run_hook(hook, filenames, color)
|
||||
|
||||
|
||||
def _get_hook_no_install(repo_config, store, hook_id):
|
||||
|
||||
Reference in New Issue
Block a user