forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_provenance.py
More file actions
275 lines (221 loc) 路 8.01 KB
/
Copy pathadd_provenance.py
File metadata and controls
275 lines (221 loc) 路 8.01 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
#!/usr/bin/env python3
"""Add provenance metadata to lessons (Issue #1219).
Scans lesson markdown files and adds provenance section to those missing it.
Usage:
python3 scripts/add_provenance.py --dry-run
python3 scripts/add_provenance.py --apply
python3 scripts/add_provenance.py --domain ops --dry-run
Provenance format:
provenance:
source: "internal" | "external" | "community"
contributor: "Node Name"
merged_at: "YYYY-MM-DD"
evidence: "post-publication" | "pr-merged" | "issue-resolved"
"""
from __future__ import annotations
import argparse
import json
import re
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
REPO_ROOT = Path(__file__).resolve().parent.parent
LESSONS_DIR = REPO_ROOT / "lessons"
@dataclass
class LessonInfo:
"""Lesson metadata."""
path: Path
title: str
domain: str
has_provenance: bool
frontmatter: dict
def parse_frontmatter(content: str) -> tuple[dict, str]:
"""Parse YAML-like frontmatter from markdown content."""
if not content.startswith("---"):
return {}, content
# Find end of frontmatter
end_idx = content.find("---", 3)
if end_idx == -1:
return {}, content
frontmatter_str = content[3:end_idx].strip()
body = content[end_idx + 3:].strip()
# Simple key-value parser (not full YAML)
frontmatter = {}
current_key = None
current_value = []
for line in frontmatter_str.split("\n"):
if line.startswith(" ") and current_key:
# Continuation of previous value
current_value.append(line.strip())
elif ":" in line and not line.startswith(" "):
# New key-value pair
if current_key:
frontmatter[current_key] = "\n".join(current_value).strip()
key, _, value = line.partition(":")
current_key = key.strip()
current_value = [value.strip()] if value.strip() else []
elif line.strip().startswith("- "):
# List item
if current_key:
current_value.append(line.strip())
if current_key:
frontmatter[current_key] = "\n".join(current_value).strip()
return frontmatter, body
def has_provenance_section(content: str) -> bool:
"""Check if content already has provenance section."""
return "provenance:" in content or "provenance:" in content.lower()
def extract_domain_from_path(path: Path) -> str:
"""Extract domain from file path."""
# lessons/contrib/file.md -> contrib
# lessons/en/file.md -> en (language, skip)
parts = path.relative_to(LESSONS_DIR).parts
if len(parts) > 1:
domain = parts[0]
if domain in ("en", "hi", "id", "ru", "tr"):
return "unknown" # Language directories
return domain
return "unknown"
def guess_contributor(frontmatter: dict, path: Path) -> str:
"""Guess contributor from metadata."""
# Check for explicit contributor
if "contributor" in frontmatter:
return frontmatter["contributor"]
# Check title for node names
title = frontmatter.get("title", "")
node_patterns = [
r"Misaka\d+",
r"Node\d+",
r"Agent\d+",
]
for pattern in node_patterns:
match = re.search(pattern, title)
if match:
return match.group(0)
# Default based on domain
domain = extract_domain_from_path(path)
if domain == "contrib":
return "Community"
elif domain == "core":
return "MisakaNet Core"
else:
return "Unknown"
def guess_source(domain: str) -> str:
"""Guess source from domain."""
if domain == "contrib":
return "community"
elif domain in ("core", "devops"):
return "internal"
else:
return "external"
def add_provenance(content: str, contributor: str, source: str) -> str:
"""Add provenance section to content."""
# Find insertion point (after frontmatter)
if not content.startswith("---"):
# No frontmatter, add at beginning
provenance = f"""---
provenance:
source: "{source}"
contributor: "{contributor}"
merged_at: "2026-08-23"
evidence: "post-publication"
---
"""
return provenance + content
# Find end of frontmatter
end_idx = content.find("---", 3)
if end_idx == -1:
return content
# Insert provenance before closing ---
before = content[:end_idx]
after = content[end_idx:]
provenance = f"""provenance:
source: "{source}"
contributor: "{contributor}"
merged_at: "2026-08-23"
evidence: "post-publication"
"""
return before + provenance + after
def scan_lessons(domain_filter: Optional[str] = None) -> list[LessonInfo]:
"""Scan for lessons without provenance."""
lessons = []
for md_file in LESSONS_DIR.rglob("*.md"):
# Skip templates, archive, etc.
if md_file.parent.name in ("_archive", "templates", "draft", "drafts"):
continue
if md_file.name in ("index.md", "TEMPLATE.md", "LESSON_QUALITY_SCORING.md"):
continue
try:
content = md_file.read_text(encoding="utf-8")
except Exception:
continue
frontmatter, body = parse_frontmatter(content)
domain = extract_domain_from_path(md_file)
# Apply domain filter
if domain_filter and domain != domain_filter:
continue
lessons.append(LessonInfo(
path=md_file,
title=frontmatter.get("title", md_file.stem),
domain=domain,
has_provenance=has_provenance_section(content),
frontmatter=frontmatter,
))
return lessons
def main():
parser = argparse.ArgumentParser(description="Add provenance to lessons")
parser.add_argument("--dry-run", action="store_true", help="Show what would be changed")
parser.add_argument("--apply", action="store_true", help="Apply changes")
parser.add_argument("--domain", "-d", help="Filter by domain")
parser.add_argument("--json", "-j", action="store_true", help="Output as JSON")
args = parser.parse_args()
if not args.dry_run and not args.apply:
parser.error("Specify --dry-run or --apply")
# Scan lessons
lessons = scan_lessons(args.domain)
# Filter those without provenance
missing = [l for l in lessons if not l.has_provenance]
has_prov = [l for l in lessons if l.has_provenance]
# Output summary
if args.json:
result = {
"total_scanned": len(lessons),
"has_provenance": len(has_prov),
"missing_provenance": len(missing),
"lessons": [
{
"path": str(l.path.relative_to(REPO_ROOT)),
"title": l.title,
"domain": l.domain,
}
for l in missing
],
}
print(json.dumps(result, indent=2))
else:
print(f"Scanned {len(lessons)} lessons")
print(f" Has provenance: {len(has_prov)}")
print(f" Missing provenance: {len(missing)}")
if missing:
print(f"\nLessons to update:")
for l in missing[:20]: # Show first 20
print(f" - {l.path.relative_to(REPO_ROOT)} ({l.domain})")
if len(missing) > 20:
print(f" ... and {len(missing) - 20} more")
# Apply changes
if args.apply and missing:
print(f"\nAdding provenance to {len(missing)} lessons...")
updated = 0
for lesson in missing:
try:
content = lesson.path.read_text(encoding="utf-8")
contributor = guess_contributor(lesson.frontmatter, lesson.path)
source = guess_source(lesson.domain)
new_content = add_provenance(content, contributor, source)
lesson.path.write_text(new_content, encoding="utf-8")
updated += 1
except Exception as e:
print(f" Error updating {lesson.path}: {e}", file=sys.stderr)
print(f"Updated {updated} lessons")
if __name__ == "__main__":
main()