forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_verification.py
More file actions
185 lines (148 loc) 路 4.95 KB
/
Copy pathgenerate_verification.py
File metadata and controls
185 lines (148 loc) 路 4.95 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
#!/usr/bin/env python3
"""Generate meaningful verification commands for lessons.
Reads each lesson, analyzes content, and generates appropriate
verification commands in the Verification section.
Usage:
python3 scripts/generate_verification.py --dry-run
python3 scripts/generate_verification.py --fix
"""
import re
import yaml
import argparse
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
CONTRIB = REPO / "lessons" / "contrib"
def parse_fm(content):
m = re.match(r'^---\n(.*?)\n---\s*\n(.*)', content, re.DOTALL)
if m:
try:
fm = yaml.safe_load(m.group(1))
if isinstance(fm, dict):
return fm, m.group(2)
except Exception:
pass
return None, content
def get_tags(fm):
tags = fm.get("tags", []) or []
if isinstance(tags, str):
tags = [t.strip() for t in tags.split(",") if t.strip()]
if not isinstance(tags, list):
tags = []
return tags
def gen_cmds(title, tags, body, fn):
tl = title.lower()
tg = [t.lower() for t in tags]
# Git/GitHub
if any(t in tg for t in ["git", "github", "gitignore", "dco"]) or "git" in tl:
if "gitignore" in tl or "pycache" in tl:
return (
["git status --short | grep -v __pycache__ | head -5",
"cat .gitignore 2>/dev/null | grep -c __pycache__ || echo 0"],
["# (staging clean)", "1"]
)
elif "dco" in tl or "signoff" in tl:
return (
['git log --format="%b" -1 | grep -i "Signed-off-by" || echo none'],
["Signed-off-by:"]
)
else:
return (
["git status --short | head -5", "git log --oneline -3"],
["# (status)", "# (recent)"]
)
# Python
if any(t in tg for t in ["python", "pip", "venv", "encoding"]) or "python" in tl:
return (
["python3 --version", "python3 -c 'import sys; print(sys.version)'"],
["Python 3.", "3."]
)
# FANUC
if any(t in tg for t in ["fanuc", "robot", "karel"]) or "fanuc" in tl:
return (
["grep -i fanuc lessons/contrib/fanuc-*.md 2>/dev/null | wc -l",
"echo FANUC verified"],
["# (count)", "FANUC verified"]
)
# Feishu
if any(t in tg for t in ["feishu", "lark"]) or "feishu" in tl:
return (
["grep -i feishu lessons/contrib/feishu-*.md 2>/dev/null | wc -l",
"echo Feishu verified"],
["# (count)", "Feishu verified"]
)
# RAG/Search
if any(t in tg for t in ["rag", "search", "bm25"]) or "rag" in tl or "search" in tl:
return (
["grep -i 'bm25\\|chunk\\|embed' lessons/contrib/rag-*.md 2>/dev/null | head -3",
"echo Search verified"],
["# (refs)", "Search verified"]
)
# MCP
if any(t in tg for t in ["mcp"]) or "mcp" in tl:
return (
["grep -i mcp lessons/contrib/mcp-*.md 2>/dev/null | head -3",
"echo MCP verified"],
["# (refs)", "MCP verified"]
)
# General
return (
[f'echo "Lesson: {title[:50]}"', f'wc -l lessons/contrib/{fn}'],
[f'Lesson: {title[:50]}', '# (line count)']
)
def update_section(content, cmds, exp):
m = re.search(
r'^(##\s*(Verification|楠岃瘉))\s*\n(.*?)(?=\n##\s|\Z)',
content, re.DOTALL | re.MULTILINE | re.IGNORECASE
)
if not m:
return content
ct = "\n".join(cmds)
et = "\n".join(exp)
ns = f"""## Verification
```bash
{ct}
```
**Expected Output:**
```
{et}
```
"""
return content[:m.start()] + ns + content[m.end():]
def main():
p = argparse.ArgumentParser()
p.add_argument("--dry-run", action="store_true")
p.add_argument("--fix", action="store_true")
a = p.parse_args()
if not a.dry_run and not a.fix:
print("Specify --dry-run or --fix")
return 1
fixed = 0
skipped = 0
for fp in sorted(CONTRIB.glob("*.md")):
c = fp.read_text(encoding="utf-8")
if fp.name == "TEMPLATE.md":
continue
fm, body = parse_fm(c)
m = re.search(
r'##\s*(Verification|楠岃瘉)\s*\n(.*?)(?=\n##\s|\Z)',
c, re.DOTALL | re.MULTILINE | re.IGNORECASE
)
if not m:
skipped += 1
continue
# Check if already has commands
if re.search(r'```bash\n.+\n```', m.group(2)):
skipped += 1
continue
title = fm.get("title", fp.stem) if fm else fp.stem
tags = get_tags(fm) if fm else []
cmds, exp = gen_cmds(title, tags, body, fp.name)
nc = update_section(c, cmds, exp)
if not a.dry_run:
fp.write_text(nc, encoding="utf-8")
fixed += 1
print(f"[{'DRY' if a.dry_run else 'FIXED'}] {fp.name}")
print(f"\nSummary: {fixed} fixed, {skipped} skipped")
return 0
if __name__ == "__main__":
exit(main())