mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-14 17:41:45 +04:00
wip
This commit is contained in:
parent
ea09e8bc68
commit
6ee2b2dfb0
6 changed files with 239 additions and 248 deletions
|
|
@ -1,91 +1,47 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import lang_base
|
||||
from pre_commit.all_languages import languages
|
||||
from pre_commit.clientlib import load_manifest
|
||||
from pre_commit.clientlib import LOCAL
|
||||
from pre_commit.clientlib import META
|
||||
from pre_commit.hook import Hook
|
||||
from pre_commit.lang_base import environment_dir
|
||||
from pre_commit.hook import InstallKey
|
||||
from pre_commit.prefix import Prefix
|
||||
from pre_commit.store import Store
|
||||
from pre_commit.util import clean_path_on_failure
|
||||
from pre_commit.util import rmtree
|
||||
|
||||
|
||||
logger = logging.getLogger('pre_commit')
|
||||
|
||||
|
||||
def _state_filename_v1(venv: str) -> str:
|
||||
return os.path.join(venv, '.install_state_v1')
|
||||
def _state_filename_v5(d: str) -> str:
|
||||
return os.path.join(d, '.pre-commit-state-v5')
|
||||
|
||||
|
||||
def _state_filename_v2(venv: str) -> str:
|
||||
return os.path.join(venv, '.install_state_v2')
|
||||
|
||||
|
||||
def _state(additional_deps: Sequence[str]) -> object:
|
||||
return {'additional_dependencies': additional_deps}
|
||||
|
||||
|
||||
def _read_state(venv: str) -> object | None:
|
||||
filename = _state_filename_v1(venv)
|
||||
if not os.path.exists(filename):
|
||||
return None
|
||||
else:
|
||||
with open(filename) as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def _hook_installed(hook: Hook) -> bool:
|
||||
lang = languages[hook.language]
|
||||
if lang.ENVIRONMENT_DIR is None:
|
||||
return True
|
||||
|
||||
venv = environment_dir(
|
||||
hook.prefix,
|
||||
lang.ENVIRONMENT_DIR,
|
||||
hook.language_version,
|
||||
)
|
||||
return (
|
||||
(
|
||||
os.path.exists(_state_filename_v2(venv)) or
|
||||
_read_state(venv) == _state(hook.additional_dependencies)
|
||||
) and
|
||||
not lang.health_check(hook.prefix, hook.language_version)
|
||||
)
|
||||
|
||||
|
||||
def _hook_install(hook: Hook) -> None:
|
||||
logger.info(f'Installing environment for {hook.src}.')
|
||||
def _hook_install(hook: Hook, store: Store) -> None:
|
||||
logger.info(f'Installing environment for {hook.repo}.')
|
||||
logger.info('Once installed this environment will be reused.')
|
||||
logger.info('This may take a few minutes...')
|
||||
|
||||
lang = languages[hook.language]
|
||||
assert lang.ENVIRONMENT_DIR is not None
|
||||
|
||||
venv = environment_dir(
|
||||
hook.prefix,
|
||||
lang.ENVIRONMENT_DIR,
|
||||
hook.language_version,
|
||||
)
|
||||
clone = store.clone(hook.repo, hook.rev)
|
||||
dest = tempfile.mkdtemp(prefix='i-', dir=store.directory)
|
||||
|
||||
# There's potentially incomplete cleanup from previous runs
|
||||
# Clean it up!
|
||||
if os.path.exists(venv):
|
||||
rmtree(venv)
|
||||
|
||||
with clean_path_on_failure(venv):
|
||||
with clean_path_on_failure(dest):
|
||||
prefix = Prefix(dest)
|
||||
lang.install_environment(
|
||||
hook.prefix, hook.language_version, hook.additional_dependencies,
|
||||
Prefix(clone), prefix,
|
||||
hook.language_version, hook.additional_dependencies,
|
||||
)
|
||||
health_error = lang.health_check(hook.prefix, hook.language_version)
|
||||
health_error = lang.health_check(prefix, hook.language_version)
|
||||
if health_error:
|
||||
raise AssertionError(
|
||||
f'BUG: expected environment for {hook.language} to be healthy '
|
||||
|
|
@ -94,16 +50,12 @@ def _hook_install(hook: Hook) -> None:
|
|||
f'more info:\n\n{health_error}',
|
||||
)
|
||||
|
||||
# TODO: remove v1 state writing, no longer needed after pre-commit 3.0
|
||||
# Write our state to indicate we're installed
|
||||
state_filename = _state_filename_v1(venv)
|
||||
staging = f'{state_filename}staging'
|
||||
with open(staging, 'w') as state_file:
|
||||
state_file.write(json.dumps(_state(hook.additional_dependencies)))
|
||||
# Move the file into place atomically to indicate we've installed
|
||||
os.replace(staging, state_filename)
|
||||
# TODO: need more info?
|
||||
open(_state_filename_v5(dest), 'a+').close()
|
||||
|
||||
open(_state_filename_v2(venv), 'a+').close()
|
||||
|
||||
def _hook_installed(hook: Hook, store: Store) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _hook(
|
||||
|
|
@ -123,7 +75,7 @@ def _hook(
|
|||
if not ret['stages']:
|
||||
ret['stages'] = root_config['default_stages']
|
||||
|
||||
if languages[lang].ENVIRONMENT_DIR is None:
|
||||
if languages[lang].install_environment is lang_base.no_install:
|
||||
if ret['language_version'] != C.DEFAULT:
|
||||
logger.error(
|
||||
f'The hook `{ret["id"]}` specifies `language_version` but is '
|
||||
|
|
@ -131,7 +83,7 @@ def _hook(
|
|||
f'environment. '
|
||||
f'Perhaps you meant to use a specific language?',
|
||||
)
|
||||
exit(1)
|
||||
raise SystemExit(1)
|
||||
if ret['additional_dependencies']:
|
||||
logger.error(
|
||||
f'The hook `{ret["id"]}` specifies `additional_dependencies` '
|
||||
|
|
@ -139,86 +91,64 @@ def _hook(
|
|||
f'environment. '
|
||||
f'Perhaps you meant to use a specific language?',
|
||||
)
|
||||
exit(1)
|
||||
raise SystemExit(1)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def _non_cloned_repository_hooks(
|
||||
repo_config: dict[str, Any],
|
||||
store: Store,
|
||||
root_config: dict[str, Any],
|
||||
) -> tuple[Hook, ...]:
|
||||
def _prefix(language_name: str, deps: Sequence[str]) -> Prefix:
|
||||
language = languages[language_name]
|
||||
# pygrep / script / system / docker_image do not have
|
||||
# environments so they work out of the current directory
|
||||
if language.ENVIRONMENT_DIR is None:
|
||||
return Prefix(os.getcwd())
|
||||
else:
|
||||
return Prefix(store.make_local(deps))
|
||||
|
||||
return tuple(
|
||||
Hook.create(
|
||||
repo_config['repo'],
|
||||
_prefix(hook['language'], hook['additional_dependencies']),
|
||||
_hook(hook, root_config=root_config),
|
||||
)
|
||||
for hook in repo_config['hooks']
|
||||
)
|
||||
|
||||
|
||||
def _cloned_repository_hooks(
|
||||
repo_config: dict[str, Any],
|
||||
store: Store,
|
||||
root_config: dict[str, Any],
|
||||
) -> tuple[Hook, ...]:
|
||||
repo, rev = repo_config['repo'], repo_config['rev']
|
||||
manifest_path = os.path.join(store.clone(repo, rev), C.MANIFEST_FILE)
|
||||
by_id = load_manifest(manifest_path)['hooks']
|
||||
|
||||
for hook in repo_config['hooks']:
|
||||
if hook['id'] not in by_id:
|
||||
logger.error(
|
||||
f'`{hook["id"]}` is not present in repository {repo}. '
|
||||
f'Typo? Perhaps it is introduced in a newer version? '
|
||||
f'Often `pre-commit autoupdate` fixes this.',
|
||||
)
|
||||
exit(1)
|
||||
|
||||
hook_dcts = [
|
||||
_hook(by_id[hook['id']], hook, root_config=root_config)
|
||||
for hook in repo_config['hooks']
|
||||
]
|
||||
return tuple(
|
||||
Hook.create(
|
||||
repo_config['repo'],
|
||||
Prefix(store.clone(repo, rev, hook['additional_dependencies'])),
|
||||
hook,
|
||||
)
|
||||
for hook in hook_dcts
|
||||
)
|
||||
|
||||
|
||||
def _repository_hooks(
|
||||
repo_config: dict[str, Any],
|
||||
store: Store,
|
||||
root_config: dict[str, Any],
|
||||
) -> tuple[Hook, ...]:
|
||||
if repo_config['repo'] in {LOCAL, META}:
|
||||
return _non_cloned_repository_hooks(repo_config, store, root_config)
|
||||
repo = repo_config['repo']
|
||||
if repo == META:
|
||||
return tuple(
|
||||
Hook.create(
|
||||
repo, '',
|
||||
_hook(hook, root_config=root_config),
|
||||
)
|
||||
for hook in repo_config['hooks']
|
||||
)
|
||||
elif repo == LOCAL:
|
||||
return tuple(
|
||||
Hook.create(
|
||||
repo, C.LOCAL_REPO_VERSION,
|
||||
_hook(hook, root_config=root_config),
|
||||
)
|
||||
for hook in repo_config['hooks']
|
||||
)
|
||||
else:
|
||||
return _cloned_repository_hooks(repo_config, store, root_config)
|
||||
rev = repo_config['rev']
|
||||
by_id = store.manifest(repo, rev)['hooks']
|
||||
|
||||
for hook in repo_config['hooks']:
|
||||
if hook['id'] not in by_id:
|
||||
logger.error(
|
||||
f'`{hook["id"]}` is not present in repository {repo}. '
|
||||
f'Typo? Perhaps it is introduced in a newer version? '
|
||||
f'Often `pre-commit autoupdate` fixes this.',
|
||||
)
|
||||
raise SystemExit(1)
|
||||
|
||||
return tuple(
|
||||
Hook.create(
|
||||
repo, rev,
|
||||
_hook(by_id[hook['id']], hook, root_config=root_config),
|
||||
)
|
||||
for hook in repo_config['hooks']
|
||||
)
|
||||
|
||||
|
||||
def install_hook_envs(hooks: Sequence[Hook], store: Store) -> None:
|
||||
def _need_installed() -> list[Hook]:
|
||||
seen: set[tuple[Prefix, str, str, tuple[str, ...]]] = set()
|
||||
seen: set[InstallKey] = set()
|
||||
ret = []
|
||||
for hook in hooks:
|
||||
if hook.install_key not in seen and not _hook_installed(hook):
|
||||
key = hook.install_key
|
||||
if key not in seen and not _hook_installed(hook, store):
|
||||
ret.append(hook)
|
||||
seen.add(hook.install_key)
|
||||
seen.add(key)
|
||||
return ret
|
||||
|
||||
if not _need_installed():
|
||||
|
|
@ -226,7 +156,7 @@ def install_hook_envs(hooks: Sequence[Hook], store: Store) -> None:
|
|||
with store.exclusive_lock():
|
||||
# Another process may have already completed this work
|
||||
for hook in _need_installed():
|
||||
_hook_install(hook)
|
||||
_hook_install(hook, store)
|
||||
|
||||
|
||||
def all_hooks(root_config: dict[str, Any], store: Store) -> tuple[Hook, ...]:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue