#!/usr/bin/env python3
# retoor <retoor@molodetz.nl>
import os
import re
import subprocess
import sys
class Failure:
def __init__(self, check, detail):
self.check = check
self.detail = detail
def text(self):
return self.check + ": " + self.detail
class Report:
def __init__(self):
self.failures = []
def add(self, check, detail):
self.failures.append(Failure(check, detail))
def passed(self):
return len(self.failures) == 0
def show(self):
for failure in self.failures:
print("FAIL " + failure.text())
class Scanner:
def __init__(self, root):
self.root = root
self.skipped = (
".git", ".venv", "venv", "node_modules", "__pycache__",
".tox", "target", "build", "dist", "third_party",
)
def walk(self):
for base, dirs, names in os.walk(self.root):
dirs[:] = [
name for name in dirs
if name not in self.skipped and not name.startswith(".")
and not name.startswith("build")
]
yield base, sorted(names)
def markdown(self):
found = []
for base, names in self.walk():
for name in names:
if name.endswith(".md"):
found.append(os.path.join(base, name))
return sorted(found)
def directories(self):
found = []
for base, names in self.walk():
found.append(base)
return sorted(found)
def read(self, path):
with open(path, encoding="utf-8") as handle:
return handle.read()
def text(self, path):
try:
return self.read(path)
except (UnicodeDecodeError, OSError):
return None
class Validator:
TEXT_SUFFIXES = (".md", ".py", ".js", ".css", ".html", ".svg", ".txt",
".toml", ".json", ".yaml", ".yml", ".sh")
def __init__(self, root):
self.scanner = Scanner(root)
self.root = root
self.report = Report()
def run(self):
self.ids()
self.changelog()
self.headers()
self.locale()
self.encoding()
self.forbidden()
self.research()
self.agents()
self.tags()
return self.report
def ids(self):
path = os.path.join(self.root, "SPEC.md")
if not os.path.isfile(path):
return
text = self.scanner.text(path)
if text is None:
return
seen = set()
for match in re.findall(r"^- ((?:[A-Z]+-)+[0-9]+):", text, re.M):
if match in seen:
self.report.add("ids", "duplicate definition " + match)
seen.add(match)
def versions(self, path):
text = self.scanner.text(path)
if text is None:
return []
rows = re.findall(r"^\| ([0-9]+)\.([0-9]+)", text, re.M)
return [tuple(int(part) for part in row) for row in rows]
def changelog(self):
path = os.path.join(self.root, "CHANGELOG.md")
if not os.path.isfile(path):
return
found = self.versions(path)
if found != sorted(found):
self.report.add("changelog", "versions do not ascend")
def headers(self):
for path in self.scanner.markdown():
text = self.scanner.text(path)
if text is None:
continue
if "<!-- doc:" not in text:
self.report.add("headers", "missing header " + self.short(path))
def locale(self):
for path in self.scanner.markdown():
text = self.scanner.text(path)
if text is None or "<!-- doc:" not in text:
continue
if not re.search(r"updated:\s*([0-9]{2}-[0-9]{2}-[0-9]{4}|DD-MM-YYYY)", text):
self.report.add("locale", "bad updated date in " + self.short(path))
def encoding(self):
for base, names in self.scanner.walk():
for name in names:
if not name.endswith(self.TEXT_SUFFIXES):
continue
path = os.path.join(base, name)
if self.scanner.text(path) is None:
self.report.add("encoding", "not utf-8 " + self.short(path))
def forbidden(self):
targets = ["SPEC.md", "BUILD-TRACK.md", "docs"]
for target in targets:
path = os.path.join(self.root, target)
if os.path.isfile(path):
self.scan_text(path)
elif os.path.isdir(path):
for base, names in self.scanner.walk():
if not base.startswith(path):
continue
for name in names:
if name.endswith(".md"):
self.scan_text(os.path.join(base, name))
def scan_text(self, path):
text = self.scanner.text(path)
if text is None:
return
if "TBD" in text:
self.report.add("forbidden", "TBD in " + self.short(path))
if "\u2014" in text:
self.report.add("forbidden", "em-dash in " + self.short(path))
def research(self):
base = os.path.join(self.root, "research")
if not os.path.isdir(base):
return
for name in sorted(os.listdir(base)):
if not name.endswith(".md") or name == "AGENTS.md":
continue
if not re.fullmatch(r"[a-z0-9]+(-[a-z0-9]+)*\.md", name):
self.report.add("research", "unslugged " + name)
continue
text = self.scanner.text(os.path.join(base, name))
if text is None:
continue
for section in ("## Queries", "## Sources", "## Synthesis"):
if section not in text:
self.report.add("research", name + " lacks " + section)
def agents(self):
for path in self.scanner.directories():
if not os.path.isfile(os.path.join(path, "AGENTS.md")):
self.report.add("agents", "missing file in " + self.short(path))
def tags(self):
changelog = os.path.join(self.root, "CHANGELOG.md")
if not os.path.isfile(changelog):
return
found = self.versions(changelog)
if not found:
return
latest = found[-1]
wanted = "v" + str(latest[0]) + "." + str(latest[1])
try:
out = subprocess.run(
["git", "-C", self.root, "tag", "--list", "v*",
"--sort=-v:refname"],
capture_output=True, text=True, check=False,
)
except OSError:
return
if out.returncode != 0:
return
tags = [line for line in out.stdout.splitlines() if line]
if tags and not tags[0].startswith(wanted):
self.report.add("tags", "latest tag " + tags[0] + " trails " + wanted)
def short(self, path):
return os.path.relpath(path, self.root)
def main(argv):
root = argv[1] if len(argv) > 1 else os.getcwd()
report = Validator(root).run()
if report.passed():
print("OK")
return 0
report.show()
return 1
if __name__ == "__main__":
sys.exit(main(sys.argv))