|
| 1 | +import contextlib |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from typing import Generator |
| 5 | +from typing import Sequence |
| 6 | +from typing import Tuple |
| 7 | + |
| 8 | +import pre_commit.constants as C |
| 9 | +from pre_commit.envcontext import envcontext |
| 10 | +from pre_commit.envcontext import PatchesT |
| 11 | +from pre_commit.envcontext import Var |
| 12 | +from pre_commit.hook import Hook |
| 13 | +from pre_commit.languages import helpers |
| 14 | +from pre_commit.prefix import Prefix |
| 15 | +from pre_commit.util import clean_path_on_failure |
| 16 | +from pre_commit.util import cmd_output |
| 17 | + |
| 18 | +ENVIRONMENT_DIR = 'lua_env' |
| 19 | +get_default_version = helpers.basic_get_default_version |
| 20 | +healthy = helpers.basic_healthy |
| 21 | + |
| 22 | + |
| 23 | +def _get_lua_version() -> str: # pragma: win32 no cover |
| 24 | + """Get the Lua version used in file paths.""" |
| 25 | + _, stdout, _ = cmd_output('luarocks', 'config', '--lua-ver') |
| 26 | + return stdout.strip() |
| 27 | + |
| 28 | + |
| 29 | +def get_env_patch(d: str) -> PatchesT: # pragma: win32 no cover |
| 30 | + version = _get_lua_version() |
| 31 | + so_ext = 'dll' if sys.platform == 'win32' else 'so' |
| 32 | + return ( |
| 33 | + ('PATH', (os.path.join(d, 'bin'), os.pathsep, Var('PATH'))), |
| 34 | + ( |
| 35 | + 'LUA_PATH', ( |
| 36 | + os.path.join(d, 'share', 'lua', version, '?.lua;'), |
| 37 | + os.path.join(d, 'share', 'lua', version, '?', 'init.lua;;'), |
| 38 | + ), |
| 39 | + ), |
| 40 | + ( |
| 41 | + 'LUA_CPATH', |
| 42 | + (os.path.join(d, 'lib', 'lua', version, f'?.{so_ext};;'),), |
| 43 | + ), |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def _envdir(prefix: Prefix) -> str: # pragma: win32 no cover |
| 48 | + directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT) |
| 49 | + return prefix.path(directory) |
| 50 | + |
| 51 | + |
| 52 | +@contextlib.contextmanager # pragma: win32 no cover |
| 53 | +def in_env(prefix: Prefix) -> Generator[None, None, None]: |
| 54 | + with envcontext(get_env_patch(_envdir(prefix))): |
| 55 | + yield |
| 56 | + |
| 57 | + |
| 58 | +def install_environment( |
| 59 | + prefix: Prefix, |
| 60 | + version: str, |
| 61 | + additional_dependencies: Sequence[str], |
| 62 | +) -> None: # pragma: win32 no cover |
| 63 | + helpers.assert_version_default('lua', version) |
| 64 | + |
| 65 | + envdir = _envdir(prefix) |
| 66 | + with clean_path_on_failure(envdir): |
| 67 | + with in_env(prefix): |
| 68 | + # luarocks doesn't bootstrap a tree prior to installing |
| 69 | + # so ensure the directory exists. |
| 70 | + os.makedirs(envdir, exist_ok=True) |
| 71 | + |
| 72 | + # Older luarocks (e.g., 2.4.2) expect the rockspec as an arg |
| 73 | + for rockspec in prefix.star('.rockspec'): |
| 74 | + make_cmd = ('luarocks', '--tree', envdir, 'make', rockspec) |
| 75 | + helpers.run_setup_cmd(prefix, make_cmd) |
| 76 | + |
| 77 | + # luarocks can't install multiple packages at once |
| 78 | + # so install them individually. |
| 79 | + for dependency in additional_dependencies: |
| 80 | + cmd = ('luarocks', '--tree', envdir, 'install', dependency) |
| 81 | + helpers.run_setup_cmd(prefix, cmd) |
| 82 | + |
| 83 | + |
| 84 | +def run_hook( |
| 85 | + hook: Hook, |
| 86 | + file_args: Sequence[str], |
| 87 | + color: bool, |
| 88 | +) -> Tuple[int, bytes]: # pragma: win32 no cover |
| 89 | + with in_env(hook.prefix): |
| 90 | + return helpers.run_xargs(hook, hook.cmd, file_args, color=color) |
0 commit comments