Skip to content

Commit 51cd1a0

Browse files
committed
 Access .pre-commit-hooks.yaml as artifact
Previously, the path lookup logic was unstable due to a flawed heuristic which meant guessing the artifact path incorrectly in some cases. This patch improves that by employing a reliable approach of putting the artifact into a predictable location within the installed import package. It then uses a standard `importlib.resources` module to retrive it from the always-existing place. The only possibly unobvious quirk is that during development, whenever this file changes in the Git repository, the `pip install` command must be executed again since this is what refreshes the file contents in the installed location.
1 parent c03e3fe commit 51cd1a0

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

hatch.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[build.targets.sdist]
22
include = [
3+
'.pre-commit-hooks.yaml',
34
'src/',
45
]
56

@@ -8,6 +9,9 @@ packages = [
89
'src/pre_commit_terraform/',
910
]
1011

12+
[build.targets.wheel.force-include]
13+
'.pre-commit-hooks.yaml' = 'pre_commit_terraform/_artifacts/.pre-commit-hooks.yaml'
14+
1115
[metadata.hooks.vcs.urls]
1216
'Source Archive' = 'https://github.com/antonbabenko/pre-commit-terraform/archive/{commit_hash}.tar.gz'
1317
'GitHub: repo' = 'https://github.com/antonbabenko/pre-commit-terraform'

src/pre_commit_terraform/common.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import shutil
1414
import subprocess
1515
from collections.abc import Sequence
16+
from importlib.resources import files as access_artifacts_of
1617
from pathlib import Path
1718
from typing import Callable
1819

@@ -305,15 +306,21 @@ def is_hook_run_on_whole_repo(hook_id: str, file_paths: list[str]) -> bool:
305306
"""
306307
logger.debug('Hook ID: %s', hook_id)
307308

308-
# Get the directory containing `.pre-commit-hooks.yaml` file
309-
git_repo_root = Path(__file__).resolve().parents[5]
310-
hook_config_path = os.path.join(git_repo_root, '.pre-commit-hooks.yaml')
309+
# Get the directory containing the packaged `.pre-commit-hooks.yaml` copy
310+
artifacts_root_path = (
311+
access_artifacts_of('pre_commit_terraform')
312+
/ '_artifacts'
313+
)
314+
pre_commit_hooks_yaml_path = artifacts_root_path / '.pre-commit-hooks.yaml'
315+
pre_commit_hooks_yaml_path.read_text(encoding='utf-8')
311316

312-
logger.debug('Hook config path: %s', hook_config_path)
317+
logger.debug('Hook config path: %s', pre_commit_hooks_yaml_path)
313318

314319
# Read the .pre-commit-hooks.yaml file
315-
with open(hook_config_path, 'r', encoding='utf-8') as pre_commit_hooks_yaml:
316-
hooks_config = yaml.safe_load(pre_commit_hooks_yaml)
320+
pre_commit_hooks_yaml_txt = pre_commit_hooks_yaml_path.read_text(
321+
encoding='utf-8',
322+
)
323+
hooks_config = yaml.safe_load(pre_commit_hooks_yaml_txt)
317324

318325
# Get the included and excluded file patterns for the given hook_id
319326
for hook in hooks_config:

0 commit comments

Comments
 (0)