public snapshot · areas/examples/raft-nib/ · system@main · commit da2bf624f03d · shared 2026-11-23T17:23:12Z· frozen: this page does not change when the repository does
all files
  1#!/usr/bin/env python3
  2"""The served tree is whole: every page names a stylesheet that exists, every
  3in-site link lands on a file or an id, and nothing is fetched from another
  4host (links out are fine; a page that loads a script, font or image from
  5elsewhere is not).
  6
  7    python3 gen/check_site.py site
  8"""
  9import os
 10import re
 11import sys
 12from html.parser import HTMLParser
 13
 14
 15class Refs(HTMLParser):
 16    def __init__(self):
 17        super().__init__()
 18        self.links, self.loads, self.ids = [], [], set()
 19
 20    def handle_starttag(self, tag, attrs):
 21        a = dict(attrs)
 22        if "id" in a:
 23            self.ids.add(a["id"])
 24        if tag == "a" and "href" in a:
 25            self.links.append(a["href"])
 26        if tag in ("link", "script", "img", "source", "iframe") and (a.get("href") or a.get("src")):
 27            self.loads.append(a.get("href") or a.get("src"))
 28
 29
 30def main(root):
 31    bad = []
 32    pages = [f for f in os.listdir(root) if f.endswith(".html")]
 33    if "index.html" not in pages:
 34        bad.append("no index.html")
 35    for page in pages:
 36        r = Refs()
 37        r.feed(open(os.path.join(root, page), encoding="utf-8").read())
 38        for u in r.loads:
 39            if re.match(r"[a-z]+:|//", u):
 40                bad.append(f"{page}: loads {u} from another host")
 41            elif not os.path.isfile(os.path.join(root, u.split("?")[0])):
 42                bad.append(f"{page}: loads {u}, which is not in the site")
 43        for u in r.links:
 44            if re.match(r"[a-z]+:", u):
 45                continue
 46            path, _, frag = u.partition("#")
 47            if path and not os.path.isfile(os.path.join(root, path)):
 48                bad.append(f"{page}: links to {path}, which is not in the site")
 49            if not path and frag and frag not in r.ids:
 50                bad.append(f"{page}: links to #{frag}, which no element has")
 51    for b in bad:
 52        print("check_site: " + b, file=sys.stderr)
 53    if bad:
 54        sys.exit(1)
 55    print(f"check_site: {len(pages)} page(s), every link and load resolves")
 56
 57
 58if __name__ == "__main__":
 59    main(sys.argv[1] if len(sys.argv) > 1 else "site")