-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit.py
More file actions
164 lines (145 loc) · 6 KB
/
pre-commit.py
File metadata and controls
164 lines (145 loc) · 6 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
pre-commit.py — Run before every git commit/push.
1. Updates "Last update DD.MM.YYYY." in index.html with today's date.
2. Deletes any individual file (not folder) over 100 MB in the project root.
3. Counts total commentary entries across all JSON files in data/commentaries/.
4. Calculates how many of the 35,527 Bible verses are covered.
5. Calculates average commentary entries per Bible verse (multi-verse entries
are expanded so each verse in the range is counted individually).
Updates all three statistics in README.md.
6. Clears addition-process staging files.
"""
import json
import os
import re
from datetime import date
from pathlib import Path
ROOT = Path(__file__).parent
# ---------------------------------------------------------------------------
# Task 1 — Update date in index.html
# ---------------------------------------------------------------------------
index_html = ROOT / "index.html"
html = index_html.read_text(encoding="utf-8")
today_str = date.today().strftime("%d.%m.%Y")
new_html, n = re.subn(
r"Last update \d{2}\.\d{2}\.\d{4}\.",
f"Last update {today_str}.",
html,
)
if n:
index_html.write_text(new_html, encoding="utf-8")
print(f"[1] index.html date updated to {today_str}.")
else:
print("[1] WARNING: 'Last update' pattern not found in index.html.")
# ---------------------------------------------------------------------------
# Task 2 — Delete files over 100 MB (never delete folders)
# ---------------------------------------------------------------------------
LIMIT = 100 * 1024 * 1024 # 100 MB in bytes
deleted = []
for dirpath, dirnames, filenames in os.walk(ROOT):
# Skip .git to avoid corrupting the repository
dirnames[:] = [d for d in dirnames if d != ".git"]
for fname in filenames:
fpath = Path(dirpath) / fname
try:
size = fpath.stat().st_size
except OSError:
continue
if size > LIMIT:
print(f"[2] Deleting oversized file ({size / 1024 / 1024:.1f} MB): {fpath.relative_to(ROOT)}")
fpath.unlink()
deleted.append(fpath)
if not deleted:
print("[2] No files over 100 MB found.")
# ---------------------------------------------------------------------------
# Tasks 3 & 4 — Count commentary entries and unique verse coverage
# ---------------------------------------------------------------------------
COMMENTARIES_DIR = ROOT / "data" / "commentaries"
TOTAL_BIBLE_VERSES = 35_527
EXCLUDED_NAMES = {"meta.json", "index.json", "thetypographer.json"}
total_entries = 0
total_verse_entry_count = 0 # expanded: multi-verse entries count once per verse
covered_verses: set[tuple[str, str, int]] = set()
for json_file in COMMENTARIES_DIR.rglob("*.json"):
if json_file.name in EXCLUDED_NAMES:
continue
try:
data = json.loads(json_file.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError) as e:
print(f"[3/4] WARNING: Could not parse {json_file.relative_to(ROOT)}: {e}")
continue
verses = data.get("verses", [])
if not isinstance(verses, list):
continue
total_entries += len(verses)
# Derive (book, chapter) from the file path: .../[book]/[chapter].json
book = json_file.parent.name
chapter = json_file.stem # filename without .json
for entry in verses:
v = entry.get("v")
if v is None:
continue
v_str = str(v)
if "-" in v_str:
try:
start, end = v_str.split("-", 1)
span = int(end) - int(start) + 1
total_verse_entry_count += span
for verse_num in range(int(start), int(end) + 1):
covered_verses.add((book, chapter, verse_num))
except ValueError:
pass
else:
try:
covered_verses.add((book, chapter, int(v_str)))
total_verse_entry_count += 1
except ValueError:
pass
unique_covered = len(covered_verses)
pct = (unique_covered / TOTAL_BIBLE_VERSES) * 100
avg_entries = total_verse_entry_count / TOTAL_BIBLE_VERSES
print(f"[3] Total commentary entries: {total_entries}")
print(f"[4] Unique verses covered: {unique_covered} / {TOTAL_BIBLE_VERSES} ({pct:05.2f} %)")
print(f"[5] Average commentary entries per Bible verse: {avg_entries:05.2f}")
# ---------------------------------------------------------------------------
# Update README.md
# ---------------------------------------------------------------------------
readme = ROOT / "README.md"
readme_text = readme.read_text(encoding="utf-8")
readme_text, n1 = re.subn(
r"- \d+ commentary entries in total[^\n]+",
f"- {total_entries} commentary entries in total to annotate the Bible text.",
readme_text,
)
readme_text, n2 = re.subn(
r"- [\d.]+ % of the Bible text[^\n]+",
f"- {pct:05.2f} % of the Bible text covered with commentary.",
readme_text,
)
readme_text, n3 = re.subn(
r"- Ø [\d.]+ commentary entries per Bible verse[^\n]+",
f"- Ø {avg_entries:05.2f} commentary entries per Bible verse.",
readme_text,
)
if n1 and n2 and n3:
readme.write_text(readme_text, encoding="utf-8")
print("[3/4/5] README.md statistics updated.")
else:
if not n1:
print("[3] WARNING: Commentary entries line not found in README.md.")
if not n2:
print("[4] WARNING: Bible coverage line not found in README.md.")
if not n3:
print("[5] WARNING: Average entries per verse line not found in README.md.")
# ---------------------------------------------------------------------------
# Task 6 — Clear addition-process staging files
# ---------------------------------------------------------------------------
PLACEHOLDER = "Seek and you will find. (Matthew 7:7)"
ADDITION_DIR = ROOT / "data" / "addition-process"
for fname in ("proofreading.json", "commentary-unadded.txt"):
fpath = ADDITION_DIR / fname
if fpath.exists():
fpath.write_text(PLACEHOLDER, encoding="utf-8")
print(f"[6] Cleared {fname}.")
else:
print(f"[6] WARNING: {fname} not found.")