-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathvalidate_plugins_catalog.py
More file actions
285 lines (228 loc) · 9.22 KB
/
Copy pathvalidate_plugins_catalog.py
File metadata and controls
285 lines (228 loc) · 9.22 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
281
282
283
284
285
#!/usr/bin/env python3
"""
validate_plugins_catalog.py
Automated sync check between PLUGINS.md catalog and actual plugin metadata files.
This script validates that:
1. All plugins in plugins/ have an entry in PLUGINS.md
2. All entries in PLUGINS.md correspond to actual plugins
3. Plugin counts match (safe, intrusive, exploit)
4. Category counts are accurate
Usage:
# Validate the catalog locally
python scripts/validate_plugins_catalog.py
# Use custom paths (useful for CI)
python scripts/validate_plugins_catalog.py --catalog PLUGINS.md --plugins-dir plugins
Exit codes:
0 - Catalog is in sync
1 - Validation failed (drift detected)
"""
import argparse
import json
import re
import sys
from pathlib import Path
from typing import Dict, List, Tuple, Set
def parse_plugins_md(catalog_path: Path) -> Dict[str, Dict[str, str]]:
"""
Parse PLUGINS.md and extract plugin metadata from the index table.
Returns dict mapping plugin ID -> {"name": ..., "category": ..., "safety": ...}
"""
if not catalog_path.exists():
print(f"[ERROR] PLUGINS.md not found at: {catalog_path}", file=sys.stderr)
sys.exit(1)
catalog_text = catalog_path.read_text(encoding="utf-8")
plugins: Dict[str, Dict[str, str]] = {}
# Extract plugin index table using regex
# Pattern: | Plugin Name | `plugin_id` | `category` | `safety` | ... |
table_pattern = r"\|\s*([^\|]+?)\s*\|\s*`([^`]+?)`\s*\|\s*`([^`]+?)`\s*\|\s*`([^`]+?)`"
for match in re.finditer(table_pattern, catalog_text):
name = match.group(1).strip()
plugin_id = match.group(2).strip()
category = match.group(3).strip()
safety = match.group(4).strip()
# Skip header row
if plugin_id == "ID" or name == "Plugin":
continue
plugins[plugin_id] = {
"name": name,
"category": category,
"safety": safety,
}
return plugins
def scan_actual_plugins(plugins_dir: Path) -> Dict[str, Dict[str, str]]:
"""
Scan plugins/ directory and extract plugin metadata from each metadata.json.
Returns dict mapping plugin ID -> {"id": ..., "category": ..., "safety": ...}
"""
if not plugins_dir.exists():
print(f"[ERROR] plugins directory not found at: {plugins_dir}", file=sys.stderr)
sys.exit(1)
plugins: Dict[str, Dict[str, str]] = {}
for plugin_folder in sorted(plugins_dir.iterdir()):
if not plugin_folder.is_dir():
continue
metadata_file = plugin_folder / "metadata.json"
if not metadata_file.exists():
print(
f"[WARNING] No metadata.json in {plugin_folder.name}",
file=sys.stderr
)
continue
try:
metadata = json.loads(metadata_file.read_text(encoding="utf-8"))
plugin_id = metadata.get("id", plugin_folder.name)
category = metadata.get("category", "unknown")
# FIXED: Correctly parse safety level inner object from metadata.json
safety_block = metadata.get("safety", {})
safety = safety_block.get("level", "unknown") if isinstance(safety_block, dict) else "unknown"
plugins[plugin_id] = {
"id": plugin_id,
"category": category,
"safety": safety,
"folder": plugin_folder.name,
}
except json.JSONDecodeError as e:
print(
f"[WARNING] Invalid JSON in {plugin_folder.name}/metadata.json: {e}",
file=sys.stderr
)
continue
return plugins
def extract_counts_from_catalog(catalog_path: Path) -> Dict[str, int]:
"""Extract At a Glance counts from PLUGINS.md."""
counts: Dict[str, int] = {}
catalog_text = catalog_path.read_text(encoding="utf-8")
# Pattern: "- Total plugins: 60"
pattern = r"-\s+(\w+[\w\s]*?):\s+(\d+)"
for match in re.finditer(pattern, catalog_text):
key = match.group(1).strip().lower().replace(" ", "_")
value = int(match.group(2))
counts[key] = value
return counts
def extract_category_counts_from_catalog(catalog_path: Path) -> Dict[str, int]:
"""Extract category counts table from PLUGINS.md."""
counts: Dict[str, int] = {}
catalog_text = catalog_path.read_text(encoding="utf-8")
# Pattern: | `category_name` | count |
pattern = r"\|\s*`([^`]+?)`\s*\|\s*(\d+)\s*\|"
in_category_section = False
for line in catalog_text.split("\n"):
if "Category Summary" in line:
in_category_section = True
continue
if in_category_section and "##" in line:
break
if in_category_section:
match = re.search(pattern, line)
if match:
category = match.group(1).strip()
count = int(match.group(2))
if category != "Category": # Skip header
counts[category] = count
return counts
def validate_catalog(catalog_path: Path, plugins_dir: Path) -> Tuple[bool, List[str]]:
"""
Validate that PLUGINS.md is in sync with actual plugins.
Returns (is_valid, list_of_issues)
"""
issues: List[str] = []
# Parse catalog and scan actual plugins
catalog_plugins = parse_plugins_md(catalog_path)
actual_plugins = scan_actual_plugins(plugins_dir)
# Check 1: All actual plugins are in catalog
missing_in_catalog = set(actual_plugins.keys()) - set(catalog_plugins.keys())
if missing_in_catalog:
issues.append(
f"Missing from PLUGINS.md: {', '.join(sorted(missing_in_catalog))}"
)
# Check 2: All catalog entries correspond to actual plugins
extra_in_catalog = set(catalog_plugins.keys()) - set(actual_plugins.keys())
if extra_in_catalog:
issues.append(
f"In PLUGINS.md but not in plugins/: {', '.join(sorted(extra_in_catalog))}"
)
# Check 3: Total plugin count
catalog_counts = extract_counts_from_catalog(catalog_path)
total_plugins_expected = catalog_counts.get("total_plugins")
if total_plugins_expected is not None:
actual_total = len(actual_plugins)
if actual_total != total_plugins_expected:
issues.append(
f"Total plugins mismatch: catalog says {total_plugins_expected}, "
f"but found {actual_total}"
)
# Check 4: Safety level counts
actual_safety_counts = {}
for plugin in actual_plugins.values():
safety = plugin.get("safety", "unknown")
actual_safety_counts[safety] = actual_safety_counts.get(safety, 0) + 1
for safety_level, count_key in [
("safe", "safe_plugins"),
("intrusive", "intrusive_plugins"),
("exploit", "exploit_plugins"),
]:
expected = catalog_counts.get(count_key)
actual = actual_safety_counts.get(safety_level, 0)
if expected is not None and actual != expected:
issues.append(
f"Safety level '{safety_level}' count mismatch: "
f"catalog says {expected}, but found {actual}"
)
# Check 5: Category counts
catalog_category_counts = extract_category_counts_from_catalog(catalog_path)
actual_category_counts = {}
for plugin in actual_plugins.values():
category = plugin.get("category", "unknown")
actual_category_counts[category] = actual_category_counts.get(category, 0) + 1
for category, expected_count in catalog_category_counts.items():
actual_count = actual_category_counts.get(category, 0)
if actual_count != expected_count:
issues.append(
f"Category '{category}' count mismatch: "
f"catalog says {expected_count}, but found {actual_count}"
)
return (len(issues) == 0, issues)
def main() -> None:
parser = argparse.ArgumentParser(
description="Validate that PLUGINS.md is in sync with plugin metadata files.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python scripts/validate_plugins_catalog.py
python scripts/validate_plugins_catalog.py --catalog ./PLUGINS.md --plugins-dir ./plugins
""",
)
parser.add_argument(
"--catalog",
type=Path,
default=Path("PLUGINS.md"),
help="Path to PLUGINS.md (default: PLUGINS.md)",
)
parser.add_argument(
"--plugins-dir",
type=Path,
default=Path("plugins"),
help="Path to plugins directory (default: plugins)",
)
args = parser.parse_args()
# Ensure paths are absolute for consistent error messages
catalog_path = args.catalog.resolve()
plugins_path = args.plugins_dir.resolve()
print(f"Validating plugin catalog...")
print(f" Catalog: {catalog_path}")
print(f" Plugins: {plugins_path}")
print()
is_valid, issues = validate_catalog(catalog_path, plugins_path)
if is_valid:
print("[✓] Catalog is in sync!")
sys.exit(0)
else:
print("[✗] Catalog validation failed:")
print()
print(f"DEBUG - Actual plugins found in folder: {sorted(list(scan_actual_plugins(plugins_path).keys()))}\n")
for issue in issues:
print(f" • {issue}")
print()
sys.exit(1)
if __name__ == "__main__":
main()