okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-clean-3 (321794636): docs and history (cleanup step 3, items 3.0-3.10); release manifest and capability table with checks, history/ with pointers, link check, tools/ split, bench index; owner OK'd the wording 11-082b36e8a02

main
3.8 KiB86 linesraw
1#!/usr/bin/env python3
2"""Index page for bench/results/:  bench/results/README.md
3
4    python tools/bench_results_index.py            write the index
5    python tools/bench_results_index.py --check    fail when the index is out of date (a new result folder must be listed)
6
7One row per result folder: its date (from the folder name), its own title (the first heading of its README or of its first
8Markdown file), and how many files it holds.  The results themselves stay where they are.  Standard library only.
9"""
10import argparse
11import os
12import re
13import sys
14
15sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
16import doc_pages  # noqa: E402
17
18ROOT = doc_pages.ROOT
19RESULTS = os.path.join(ROOT, "bench", "results")
20OUT = os.path.join(RESULTS, "README.md")
21
22
23def title_of(folder):
24    names = sorted(n for n in os.listdir(folder) if n.lower().endswith(".md"))
25    names.sort(key=lambda n: (n.lower() != "readme.md", n))
26    for name in names:
27        try:
28            with open(os.path.join(folder, name), encoding="utf-8", errors="replace") as f:
29                for line in f:
30                    m = re.match(r"^#{1,3}\s+(.*\S)", line)
31                    if m:
32                        t = re.sub(r"[`*]", "", m.group(1)).replace("|", "/")
33                        return t if len(t) <= 150 else t[:147].rstrip() + "..."
34        except OSError:
35            pass
36    return "(no Markdown page)"
37
38
39def count_files(folder):
40    return sum(len(files) for _, _, files in os.walk(folder))
41
42
43def build():
44    entries = sorted(e for e in os.listdir(RESULTS) if e != "README.md")
45    folders = [e for e in entries if os.path.isdir(os.path.join(RESULTS, e))]
46    loose = [e for e in entries if not os.path.isdir(os.path.join(RESULTS, e))]
47    total = sum(count_files(os.path.join(RESULTS, f)) for f in folders) + len(loose)
48    lines = ["# Benchmark results", "",
49             "Raw results and short reports from measurement runs, one folder per run, named by date. They stay here as they were written: each states",
50             "the Strata version, hardware and method of its own day, and none is a statement about a later release. Current numbers are in the",
51             "[docs](../../docs/README.md) and the [README](../../README.md); how to measure is in [BENCHMARK_PROTOCOL.md](../../docs/BENCHMARK_PROTOCOL.md).",
52             "", "This page is built by `python tools/bench_results_index.py` (`--check` fails when a folder is missing from it). "
53             "%d folders and %d loose files, %d files in all." % (len(folders), len(loose), total), ""]
54    month = None
55    for f in folders:
56        m = re.match(r"^(\d{4}-\d{2})-(\d{2})", f)
57        key = m.group(1) if m else "undated"
58        if key != month:
59            month = key
60            lines += ["", "## " + key, "", "| Folder | What | Files |", "| --- | --- | ---: |"]
61        lines.append("| [%s](%s/) | %s | %d |" % (f, f, title_of(os.path.join(RESULTS, f)), count_files(os.path.join(RESULTS, f))))
62    if loose:
63        lines += ["", "## Loose files", "", "| File |", "| --- |"] + ["| [%s](%s) |" % (n, n) for n in loose]
64    return "\n".join(lines) + "\n"
65
66
67def main(argv=None):
68    ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
69    ap.add_argument("--check", action="store_true")
70    args = ap.parse_args(argv)
71    want = build()
72    if args.check:
73        have = open(OUT, encoding="utf-8", newline="").read().replace("\r\n", "\n") if os.path.exists(OUT) else ""
74        if have != want:
75            print("FAIL bench/results/README.md is out of date (run python tools/bench_results_index.py)")
76            return 1
77        print("bench results index: ok")
78        return 0
79    with open(OUT, "w", encoding="utf-8", newline="\n") as f:
80        f.write(want)
81    print("wrote %s" % OUT)
82    return 0
83
84
85if __name__ == "__main__":
86    sys.exit(main())