2025-02-09 21:32:53 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-09 21:22:17 +01:00
|
|
|
import contextlib
|
2025-02-09 21:35:53 +01:00
|
|
|
import os.path
|
2025-02-09 21:22:17 +01:00
|
|
|
from typing import Generator
|
|
|
|
from typing import Sequence
|
|
|
|
|
|
|
|
from pre_commit.envcontext import envcontext
|
|
|
|
from pre_commit.envcontext import PatchesT
|
|
|
|
from pre_commit.envcontext import Var
|
2025-02-09 21:35:53 +01:00
|
|
|
from pre_commit.errors import FatalError
|
2025-02-09 21:22:17 +01:00
|
|
|
from pre_commit.languages import helpers
|
2025-02-09 21:32:53 +01:00
|
|
|
from pre_commit.parse_shebang import find_executable
|
2025-02-09 21:22:17 +01:00
|
|
|
from pre_commit.prefix import Prefix
|
|
|
|
|
|
|
|
ENVIRONMENT_DIR = 'coursier'
|
|
|
|
|
|
|
|
get_default_version = helpers.basic_get_default_version
|
2025-02-09 21:33:32 +01:00
|
|
|
health_check = helpers.basic_health_check
|
2025-02-09 21:35:53 +01:00
|
|
|
run_hook = helpers.basic_run_hook
|
2025-02-09 21:22:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def install_environment(
|
|
|
|
prefix: Prefix,
|
|
|
|
version: str,
|
|
|
|
additional_dependencies: Sequence[str],
|
2025-02-09 21:35:53 +01:00
|
|
|
) -> None:
|
2025-02-09 21:22:17 +01:00
|
|
|
helpers.assert_version_default('coursier', version)
|
|
|
|
|
2025-02-09 21:32:53 +01:00
|
|
|
# Support both possible executable names (either "cs" or "coursier")
|
2025-02-09 21:35:53 +01:00
|
|
|
cs = find_executable('cs') or find_executable('coursier')
|
|
|
|
if cs is None:
|
2025-02-09 21:32:53 +01:00
|
|
|
raise AssertionError(
|
|
|
|
'pre-commit requires system-installed "cs" or "coursier" '
|
|
|
|
'executables in the application search path',
|
|
|
|
)
|
|
|
|
|
2025-02-09 21:35:53 +01:00
|
|
|
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
|
|
|
|
|
|
|
def _install(*opts: str) -> None:
|
|
|
|
assert cs is not None
|
|
|
|
helpers.run_setup_cmd(prefix, (cs, 'fetch', *opts))
|
|
|
|
helpers.run_setup_cmd(prefix, (cs, 'install', '--dir', envdir, *opts))
|
|
|
|
|
|
|
|
with in_env(prefix, version):
|
|
|
|
channel = prefix.path('.pre-commit-channel')
|
|
|
|
if os.path.isdir(channel):
|
|
|
|
for app_descriptor in os.listdir(channel):
|
|
|
|
_, app_file = os.path.split(app_descriptor)
|
|
|
|
app, _ = os.path.splitext(app_file)
|
|
|
|
_install(
|
2025-02-09 21:22:17 +01:00
|
|
|
'--default-channels=false',
|
2025-02-09 21:35:53 +01:00
|
|
|
'--channel', channel,
|
2025-02-09 21:22:17 +01:00
|
|
|
app,
|
2025-02-09 21:35:53 +01:00
|
|
|
)
|
|
|
|
elif not additional_dependencies:
|
|
|
|
raise FatalError(
|
|
|
|
'expected .pre-commit-channel dir or additional_dependencies',
|
2025-02-09 21:22:17 +01:00
|
|
|
)
|
|
|
|
|
2025-02-09 21:35:53 +01:00
|
|
|
if additional_dependencies:
|
|
|
|
_install(*additional_dependencies)
|
2025-02-09 21:22:17 +01:00
|
|
|
|
2025-02-09 21:35:53 +01:00
|
|
|
|
|
|
|
def get_env_patch(target_dir: str) -> PatchesT:
|
2025-02-09 21:22:17 +01:00
|
|
|
return (
|
|
|
|
('PATH', (target_dir, os.pathsep, Var('PATH'))),
|
2025-02-09 21:35:53 +01:00
|
|
|
('COURSIER_CACHE', os.path.join(target_dir, '.cs-cache')),
|
2025-02-09 21:22:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2025-02-09 21:35:53 +01:00
|
|
|
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)):
|
2025-02-09 21:22:17 +01:00
|
|
|
yield
|