-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandle_custom_inputs.py
More file actions
executable file
·116 lines (99 loc) · 4.23 KB
/
handle_custom_inputs.py
File metadata and controls
executable file
·116 lines (99 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
from __future__ import annotations
"""
Apply optional container inputs to pre-commit and codespell configs.
"""
import os
import sys
from pathlib import Path
ACTION_REPO_DIR = Path(os.environ.get("ACTION_REPO_DIR", "/action"))
PRE_COMMIT_CONFIG_PATH = ACTION_REPO_DIR / ".pre-commit-config.yaml"
CODESPELL_IGNORE_WORDS_PATH = ACTION_REPO_DIR / "tools/.codespell/ignore-words.txt"
CODESPELL_CONFIG_PATH = ACTION_REPO_DIR / "tools/.codespell/.codespellrc"
def _append_exclude_regex(config_path: Path, exclude_regex: str) -> None:
if not config_path.exists():
print(f"ERROR: pre-commit config missing: {config_path}", file=sys.stderr)
sys.exit(1)
if config_path.stat().st_size == 0:
print(f"ERROR: pre-commit config is empty: {config_path}", file=sys.stderr)
sys.exit(1)
lines = config_path.read_text().splitlines()
out = []
updated = False
for line in lines:
if not updated and line.startswith("exclude: "):
existing = line[len("exclude: ") :].strip().strip("'\"")
combined = f"{existing}|{exclude_regex}" if existing else exclude_regex
out.append(f"exclude: '{combined}'")
updated = True
else:
out.append(line)
if not updated:
print("ERROR: no exclude line found in pre-commit config", file=sys.stderr)
sys.exit(1)
config_path.write_text("\n".join(out) + "\n")
def _append_codespell_ignore_words(ignore_words_path: Path, ignore_words: str) -> None:
if not ignore_words_path.exists():
print(f"ERROR: codespell ignore words file missing: {ignore_words_path}", file=sys.stderr)
sys.exit(1)
words = [w.strip() for w in ignore_words.split(",") if w.strip()]
if not words:
return
with ignore_words_path.open("a", encoding="utf-8") as handle:
for word in words:
handle.write(word + "\n")
def _append_codespell_skip_paths(config_path: Path, skip_paths: str) -> None:
if not config_path.exists():
print(f"ERROR: codespell config missing: {config_path}", file=sys.stderr)
sys.exit(1)
if config_path.stat().st_size == 0:
print(f"ERROR: codespell config is empty: {config_path}", file=sys.stderr)
sys.exit(1)
additions = [p.strip() for p in skip_paths.split(",") if p.strip()]
if not additions:
return
lines = config_path.read_text().splitlines()
out = []
updated = False
for line in lines:
if line.startswith("skip = "):
current = line[len("skip = ") :].strip()
combined = ",".join([p for p in [current, *additions] if p])
out.append(f"skip = {combined}")
updated = True
else:
out.append(line)
if not updated:
print("ERROR: no skip line found in codespell config", file=sys.stderr)
sys.exit(1)
config_path.write_text("\n".join(out) + "\n")
def main() -> int:
if not ACTION_REPO_DIR.exists() or not ACTION_REPO_DIR.is_dir():
print(f"ERROR: action repo dir missing or not a directory: {ACTION_REPO_DIR}", file=sys.stderr)
return 1
exclude_regex = os.environ.get("EXCLUDE_REGEX", "")
codespell_ignore_words = os.environ.get("CODESPELL_IGNORE_WORDS", "")
codespell_skip_paths = os.environ.get("CODESPELL_SKIP_PATHS", "")
if not any([exclude_regex, codespell_ignore_words, codespell_skip_paths]):
return 0
if exclude_regex:
print("Appending to pre-commit exclude regex pattern.")
print(f"Additional exclude regex (raw): {exclude_regex}")
print("Additional exclude regex entries:")
for entry in exclude_regex.split("|"):
if entry:
print(f" - {entry}")
_append_exclude_regex(PRE_COMMIT_CONFIG_PATH, exclude_regex)
if codespell_ignore_words:
print(f"Adding codespell ignore words: {codespell_ignore_words}")
_append_codespell_ignore_words(
CODESPELL_IGNORE_WORDS_PATH, codespell_ignore_words
)
if codespell_skip_paths:
print(f"Adding codespell skip paths: {codespell_skip_paths}")
_append_codespell_skip_paths(
CODESPELL_CONFIG_PATH, codespell_skip_paths
)
return 0
if __name__ == "__main__":
raise SystemExit(main())