-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_variables.py
More file actions
executable file
·69 lines (52 loc) · 1.84 KB
/
check_variables.py
File metadata and controls
executable file
·69 lines (52 loc) · 1.84 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
#!/usr/bin/env python3
"""Check for unsubstituted cookiecutter variables in generated projects."""
import sys
import re
from pathlib import Path
def check_project(project_dir: str) -> tuple[bool, list[str]]:
"""Check a project for unsubstituted variables."""
project_path = Path(project_dir)
errors = []
# Pattern to match unsubstituted cookiecutter variables
pattern = re.compile(r"\{\{cookiecutter\.[^}]+\}\}")
for filepath in project_path.rglob("*"):
# Skip certain directories
if any(
skip in filepath.parts
for skip in ["venv", ".git", "__pycache__", ".pytest_cache"]
):
continue
# Only check files
if not filepath.is_file():
continue
# Skip binary files and certain extensions
if filepath.suffix in [".pyc", ".egg-info", ".so", ".whl"]:
continue
try:
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
content = f.read()
matches = pattern.findall(content)
if matches:
errors.append(f"{filepath.relative_to(project_path)}: {matches}")
except Exception:
pass
return len(errors) == 0, errors
def main():
projects = ["python-project-example", "custom-test-project"]
all_clean = True
for project in projects:
clean, errors = check_project(project)
if clean:
print(f"PASS: No unsubstituted variables in {project}")
else:
print(f"FAIL: Found unsubstituted variables in {project}:")
for err in errors:
print(f" {err}")
all_clean = False
if all_clean:
print("\nNo unsubstituted cookiecutter variables found")
return 0
else:
sys.exit(1)
if __name__ == "__main__":
sys.exit(main())