introduce install state v2 to replace v1

the v1 state is unnecessary since new repos are created for new additional_dependencies
This commit is contained in:
Anthony Sottile
2023-01-23 19:24:10 -05:00
parent 2c39545d24
commit bff5e0e738
3 changed files with 34 additions and 9 deletions

View File

@@ -23,16 +23,20 @@ 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_v2(venv: str) -> str:
return os.path.join(venv, '.install_state_v2')
def _state(additional_deps: Sequence[str]) -> object:
return {'additional_dependencies': sorted(additional_deps)}
def _state_filename(venv: str) -> str:
return os.path.join(venv, f'.install_state_v{C.INSTALLED_STATE_VERSION}')
def _read_state(venv: str) -> object | None:
filename = _state_filename(venv)
filename = _state_filename_v1(venv)
if not os.path.exists(filename):
return None
else:
@@ -51,7 +55,10 @@ def _hook_installed(hook: Hook) -> bool:
hook.language_version,
)
return (
_read_state(venv) == _state(hook.additional_dependencies) and
(
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)
)
@@ -87,14 +94,18 @@ def _hook_install(hook: Hook) -> None:
f'your environment\n\n'
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(venv)
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)
open(_state_filename_v2(venv), 'a+').close()
def _hook(
*hook_dicts: dict[str, Any],