#!/usr/bin/env python3 # retoor 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 "