-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcheck_project_rules.py
More file actions
280 lines (243 loc) · 9.43 KB
/
Copy pathcheck_project_rules.py
File metadata and controls
280 lines (243 loc) · 9.43 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python3
"""Lightweight Markdown project checks for book repositories."""
from __future__ import annotations
import ast
import re
import sys
import textwrap
from datetime import date, timedelta
from pathlib import Path
from urllib.parse import unquote, urlparse
ROOT = Path(__file__).resolve().parent
SKIP_DIRS = {
".agent",
".git",
".mdpress",
"_book",
"_site",
"dist",
"node_modules",
}
LINK_RE = re.compile(r"(!?)\[[^\]]*\]\(([^)\s]+(?:\s+\"[^\"]*\")?)\)")
FENCE_RE = re.compile(r"^\s*(`{3,}|~{3,})")
VOLATILE_META_RE = re.compile(
r"`verified_at`:\s*(\d{4}-\d{2}-\d{2})\s*·\s*"
r"`expires_at`:\s*(\d{4}-\d{2}-\d{2})\s*·\s*"
r"`ttl_days`:\s*(\d+)"
)
VOLATILE_STATUS_RE = re.compile(
r"<!--\s*volatile-status:\s*id=([^\s]+)\s+status=([^\s]+)\s*-->"
)
ANGLE_PLACEHOLDER_RE = re.compile(r"<([A-Za-z_][A-Za-z0-9_.-]*)>")
def iter_markdown_files(root: Path = ROOT) -> list[Path]:
files: list[Path] = []
for path in root.rglob("*.md"):
if any(part in SKIP_DIRS for part in path.relative_to(root).parts):
continue
files.append(path)
return sorted(files)
def display_path(path: Path, root: Path = ROOT) -> str:
try:
return str(path.relative_to(root))
except ValueError:
return str(path)
def strip_fenced_blocks(text: str) -> str:
output: list[str] = []
in_fence = False
fence_marker = ""
fence_len = 0
for line in text.splitlines():
match = FENCE_RE.match(line)
if match:
marker = match.group(1)
char = marker[0]
length = len(marker)
if not in_fence:
in_fence = True
fence_marker = char
fence_len = length
elif char == fence_marker and length >= fence_len:
in_fence = False
output.append("")
continue
output.append("" if in_fence else line)
return "\n".join(output)
def check_fences(path: Path, text: str) -> list[str]:
issues: list[str] = []
stack: list[tuple[str, int, int]] = []
for line_no, line in enumerate(text.splitlines(), 1):
match = FENCE_RE.match(line)
if not match:
continue
marker = match.group(1)
char = marker[0]
length = len(marker)
if not stack:
stack.append((char, length, line_no))
continue
open_char, open_len, _ = stack[-1]
if char == open_char and length >= open_len:
stack.pop()
else:
stack.append((char, length, line_no))
for _, _, line_no in stack:
issues.append(f"{path.relative_to(ROOT)}:{line_no}: unclosed fenced code block")
return issues
def is_local_target(target: str) -> bool:
parsed = urlparse(target)
return not parsed.scheme and not parsed.netloc and not target.startswith("#")
def normalize_target(raw_target: str) -> str:
target = raw_target.strip()
if " " in target and target.count('"') >= 2:
target = target.split(" ", 1)[0]
return unquote(target.split("#", 1)[0])
def check_links(path: Path, text: str) -> list[str]:
issues: list[str] = []
body = strip_fenced_blocks(text)
for match in LINK_RE.finditer(body):
raw_target = match.group(2).strip()
target = normalize_target(raw_target)
if not target or not is_local_target(raw_target):
continue
target_path = (path.parent / target).resolve()
try:
target_path.relative_to(ROOT)
except ValueError:
continue
if not target_path.exists():
line_no = body[: match.start()].count("\n") + 1
issues.append(
f"{path.relative_to(ROOT)}:{line_no}: missing local link target: {raw_target}"
)
return issues
def check_summary_links() -> list[str]:
summary = ROOT / "SUMMARY.md"
if not summary.exists():
return []
return check_links(summary, summary.read_text(encoding="utf-8", errors="ignore"))
def check_volatile_facts(
path: Path = ROOT / "12_appendix" / "12.5_volatile_facts.md",
today: date | None = None,
) -> list[str]:
"""Fail closed when the fast-changing-facts ledger is stale or ambiguous."""
issues: list[str] = []
name = display_path(path)
if not path.is_file():
return [f"{name}: volatile facts ledger is missing"]
text = path.read_text(encoding="utf-8", errors="ignore")
metadata = VOLATILE_META_RE.search(text)
if metadata is None:
issues.append(f"{name}: volatile facts metadata is missing")
else:
try:
verified_at = date.fromisoformat(metadata.group(1))
expires_at = date.fromisoformat(metadata.group(2))
except ValueError as exc:
issues.append(f"{name}: invalid volatile facts date: {exc}")
else:
ttl_days = int(metadata.group(3))
current_date = today or date.today()
if expires_at <= verified_at:
issues.append(
f"{name}: volatile facts expires_at must be after verified_at"
)
if ttl_days != 30 or expires_at != verified_at + timedelta(days=30):
issues.append(
f"{name}: volatile facts TTL must describe exactly 30 days"
)
if verified_at > current_date:
issues.append(
f"{name}: volatile facts verified_at is in the future "
f"({verified_at.isoformat()})"
)
if current_date > expires_at:
issues.append(
f"{name}: volatile facts ledger expired on {expires_at.isoformat()}"
)
statuses = VOLATILE_STATUS_RE.findall(text)
if not statuses:
issues.append(f"{name}: volatile facts status metadata is missing")
for fact_id, status in statuses:
if status == "open-conflict":
issues.append(f"{name}: {fact_id} has an unresolved conflict")
elif status not in {"current", "resolved-conflict"}:
issues.append(f"{name}: {fact_id} has unknown status {status!r}")
return issues
def check_python_fences(root: Path = ROOT) -> list[str]:
"""Parse every runnable Python fence and reject template placeholders."""
issues: list[str] = []
for path in iter_markdown_files(root):
lines = path.read_text(encoding="utf-8", errors="ignore").splitlines()
index = 0
while index < len(lines):
opening = FENCE_RE.match(lines[index])
if opening is None:
index += 1
continue
marker = opening.group(1)
info = lines[index][opening.end() :].strip().split()
start_line = index + 1
index += 1
body: list[str] = []
while index < len(lines):
closing = FENCE_RE.match(lines[index])
if (
closing is not None
and closing.group(1)[0] == marker[0]
and len(closing.group(1)) >= len(marker)
):
break
body.append(lines[index])
index += 1
index += 1
if not info or info[0].lower() not in {"python", "py"}:
continue
flags = {item.lower() for item in info[1:]}
if "non-runnable" in flags:
previous = start_line - 2
while previous >= 0 and not lines[previous].strip():
previous -= 1
introduction = lines[previous] if previous >= 0 else ""
if "伪代码" not in introduction or "不可直接运行" not in introduction:
issues.append(
f"{display_path(path, root)}:{start_line}: non-runnable Python "
"must be introduced as pseudocode that cannot run directly"
)
continue
code = textwrap.dedent("\n".join(body))
for match in ANGLE_PLACEHOLDER_RE.finditer(code):
token = match.group(1)
if re.search(rf"</{re.escape(token)}\s*>", code):
continue
line_no = start_line + code[: match.start()].count("\n") + 1
issues.append(
f"{display_path(path, root)}:{line_no}: angle-bracket placeholder "
f"<{token}> is not valid runnable Python configuration"
)
try:
ast.parse(code, filename=str(path))
except SyntaxError as exc:
line_no = start_line + (exc.lineno or 1)
issues.append(
f"{display_path(path, root)}:{line_no}: invalid Python syntax: "
f"{exc.msg}"
)
return issues
def main() -> int:
issues: list[str] = []
files = iter_markdown_files()
for path in files:
text = path.read_text(encoding="utf-8", errors="ignore")
issues.extend(check_fences(path, text))
issues.extend(check_links(path, text))
issues.extend(check_summary_links())
issues.extend(check_volatile_facts())
issues.extend(check_python_fences())
if issues:
print("\n".join(sorted(set(issues))))
print(f"\n{len(set(issues))} issue(s) found across {len(files)} Markdown files.")
return 1
print(f"All {len(files)} Markdown files passed project checks.")
return 0
if __name__ == "__main__":
sys.exit(main())