|
| 1 | +import contextlib |
| 2 | +import os |
| 3 | +import shlex |
| 4 | +from typing import Generator |
| 5 | +from typing import Sequence |
| 6 | +from typing import Tuple |
| 7 | + |
| 8 | +from pre_commit.envcontext import envcontext |
| 9 | +from pre_commit.envcontext import PatchesT |
| 10 | +from pre_commit.envcontext import Var |
| 11 | +from pre_commit.hook import Hook |
| 12 | +from pre_commit.languages import helpers |
| 13 | +from pre_commit.prefix import Prefix |
| 14 | +from pre_commit.util import clean_path_on_failure |
| 15 | + |
| 16 | +ENVIRONMENT_DIR = 'perl_env' |
| 17 | +get_default_version = helpers.basic_get_default_version |
| 18 | +healthy = helpers.basic_healthy |
| 19 | + |
| 20 | + |
| 21 | +def _envdir(prefix: Prefix, version: str) -> str: |
| 22 | + directory = helpers.environment_dir(ENVIRONMENT_DIR, version) |
| 23 | + return prefix.path(directory) |
| 24 | + |
| 25 | + |
| 26 | +def get_env_patch(venv: str) -> PatchesT: |
| 27 | + return ( |
| 28 | + ('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))), |
| 29 | + ('PERL5LIB', os.path.join(venv, 'lib', 'perl5')), |
| 30 | + ('PERL_MB_OPT', f'--install_base {shlex.quote(venv)}'), |
| 31 | + ( |
| 32 | + 'PERL_MM_OPT', ( |
| 33 | + f'INSTALL_BASE={shlex.quote(venv)} ' |
| 34 | + f'INSTALLSITEMAN1DIR=none INSTALLSITEMAN3DIR=none' |
| 35 | + ), |
| 36 | + ), |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@contextlib.contextmanager |
| 41 | +def in_env( |
| 42 | + prefix: Prefix, |
| 43 | + language_version: str, |
| 44 | +) -> Generator[None, None, None]: |
| 45 | + with envcontext(get_env_patch(_envdir(prefix, language_version))): |
| 46 | + yield |
| 47 | + |
| 48 | + |
| 49 | +def install_environment( |
| 50 | + prefix: Prefix, version: str, additional_dependencies: Sequence[str], |
| 51 | +) -> None: |
| 52 | + helpers.assert_version_default('perl', version) |
| 53 | + |
| 54 | + with clean_path_on_failure(_envdir(prefix, version)): |
| 55 | + with in_env(prefix, version): |
| 56 | + helpers.run_setup_cmd( |
| 57 | + prefix, ('cpan', '-T', '.', *additional_dependencies), |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def run_hook( |
| 62 | + hook: 'Hook', |
| 63 | + file_args: Sequence[str], |
| 64 | + color: bool, |
| 65 | +) -> Tuple[int, bytes]: |
| 66 | + with in_env(hook.prefix, hook.language_version): |
| 67 | + return helpers.run_xargs(hook, hook.cmd, file_args, color=color) |
0 commit comments