33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
def validate(path):
|
|
obj = json.loads(path.read_text(encoding="utf-8"))
|
|
if "products" in obj or "matchStrings" in obj:
|
|
raise ValueError("Unexpected page envelope in record")
|
|
expected = "cpe" if "/cpes/" in "/" + path.as_posix() else "matchString"
|
|
field = "cpeNameId" if expected == "cpe" else "matchCriteriaId"
|
|
if not isinstance(obj.get(expected), dict) or not obj[expected].get(field):
|
|
raise ValueError(f"Invalid record {path}")
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("--changed", action="store_true")
|
|
args = p.parse_args()
|
|
if args.changed:
|
|
result = subprocess.run(["git", "status", "--porcelain", "--untracked-files=all", "--", "data"], capture_output=True, text=True, check=True)
|
|
files = [Path(line[3:]) for line in result.stdout.splitlines() if len(line) > 3 and line[:2] != " D" and line[:2] != "D "]
|
|
else:
|
|
files = list(Path("data").rglob("*.json"))
|
|
count = 0
|
|
for path in files:
|
|
if not path.is_file() or path.suffix != ".json": continue
|
|
validate(path)
|
|
count += 1
|
|
print(f"VALID: {count} CPE / match records", flush=True)
|
|
|
|
if __name__ == "__main__": main()
|