Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tools/gate/devtrees.sh

6.5 KiB, 1 run, executable

created by r1870400018:66915, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#!/usr/bin/env bash
2# Engine survey of one tree: each main compiled by typst 0.15.1, by Austenite's curated reader (what the
3# dev loop's `--watch` runs today) and by Austenite's evaluator (`--eval`, what it would run), with the
4# exit status, the page count and the diagnostic counts of each, and nothing of the tree. Generic: it takes
5# the tree as arguments and knows no book.
6#
7# devtrees.sh <tree-root> <main>...
8#
9# <tree-root> the tree's root, which is `--root` and the parent of `assets/fonts`
10# <main> an entry file, relative to <tree-root> or absolute; each is compiled from its own
11# directory under its bare file name, as `./dev` does
12#
13# Environment: AUSTENITE_BIN (the austenite binary under test; required), TYPST_BIN (default
14# /home/jason/bin/typst, which must be 0.15.1), DEVTREES_INDEX (digits, default 0: the tree's number in the
15# line a caller prints for each tree), DEVTREES_TIMEOUT (seconds an engine may run, default 1800), GATE_CAP
16# (the memory cap of each engine, default 3G). Every engine runs under the cap with the account's own fonts
17# hidden (`gate_lib.sh`).
18#
19# Engines, per main:
20# typst typst compile --root R --font-path R/assets/fonts <main> <pdf>
21# curated austenite <main> <dir>
22# eval austenite --eval --strict --diag-summary --root R --font-path R/assets/fonts <main> <dir>
23# eval-lax the same without --strict, run only after a strict run that failed: the dev loop is not
24# strict, so what it would build and in how many pages is the datum
25#
26# [bio] by construction. Every PDF lives in a directory made under ~/.cache/austenite/devtrees and removed
27# on exit; the stdout of every engine is dropped; the stderr of every engine goes only through an awk that
28# keeps two counts (typst: its `error:` and `warning:` lines; curated: its `: error:`, `: warning:` and
29# `Error:` lines; eval: the sums of its `diag-summary` lines, which carry no text of the document); the page
30# count is read from the PDF by pdfinfo and is a number or `none`; and the whole of stdout and stderr of
31# this script passes a final whitelist, so the only lines that can appear are these, with N, M, E, X and Y
32# numbers (E is the exit status, 124 when the engine timed out) and P a number or `none`:
33#
34# tree N main M typst exit E pages P error X warning Y
35# tree N main M curated exit E pages P error X warning Y
36# tree N main M eval exit E pages P error X warning Y
37# tree N main M eval-lax exit E pages P error X warning Y
38# tree N missing-root
39# tree N main M missing-main
40# devtrees: typst is not 0.15.1
41# devtrees: no austenite binary
42# devtrees: usage
43# devtrees: no scratch
44set -u
45
46HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
47# shellcheck source=gate_lib.sh
48. "$HERE/gate_lib.sh"
49
50# A page count: a number or `none`.
51pages_of() {
52 local n
53 n="$(pdfinfo "$1" 2>/dev/null | awk '/^Pages:/ { print $2 }')"
54 case "$n" in
55 ''|*[!0-9]*) echo none;;
56 *) echo "$n";;
57 esac
58}
59
60# The counters, each reading an engine's stderr on stdin and printing `<errors> <warnings>`.
61count_typst() {
62 awk '
63 /^error: / { e++ }
64 /^warning: / { w++ }
65 END { printf "%d %d\n", e + 0, w + 0 }'
66}
67
68count_curated() {
69 awk '
70 /: error: / { e++; next }
71 /: warning: / { w++; next }
72 /^Error: / { e++ }
73 END { printf "%d %d\n", e + 0, w + 0 }'
74}
75
76count_eval() {
77 awk '
78 /^diag-summary error [a-z-]+ [^ ]+ [0-9]+$/ { e += $NF; next }
79 /^diag-summary warning [a-z-]+ [^ ]+ [0-9]+$/ { w += $NF }
80 END { printf "%d %d\n", e + 0, w + 0 }'
81}
82
83# engine <label> <counter> <pdf> <command> <args>...: one engine from the main's directory, then its line.
84# The status of the engine is left in RC.
85engine() {
86 local label="$1" counter="$2" pdf="$3" x=0 y=0
87 shift 3
88 rm -rf "${W:?}/o"
89 mkdir -p "$W/o"
90 ( cd "$DIR" && gate_hidden timeout -k 10 "$TO" "$@" ) 2>&1 >/dev/null | "count_$counter" > "$W/counts"
91 RC="${PIPESTATUS[0]}"
92 read -r x y < "$W/counts"
93 case "$x" in ''|*[!0-9]*) x=0;; esac
94 case "$y" in ''|*[!0-9]*) y=0;; esac
95 echo "tree $IDX main $MI $label exit $RC pages $(pages_of "$pdf") error $x warning $y"
96 rm -rf "${W:?}/o"
97}
98
99main() {
100 local base="$HOME/.cache/austenite/devtrees"
101 IDX="${DEVTREES_INDEX:-0}"
102 TO="${DEVTREES_TIMEOUT:-1800}"
103 case "$IDX$TO" in *[!0-9]*) echo "devtrees: usage"; return 2;; esac
104 if [ $# -lt 2 ] || [ -z "$IDX" ] || [ -z "$TO" ]; then
105 echo "devtrees: usage"
106 return 2
107 fi
108 local typst="${TYPST_BIN:-/home/jason/bin/typst}" aus="${AUSTENITE_BIN:-}"
109 if ! timeout 60 "$typst" --version 2>/dev/null | awk '/^typst 0\.15\.1( |$)/ { ok = 1 } END { exit !ok }'; then
110 echo "devtrees: typst is not 0.15.1"
111 return 2
112 fi
113 if [ -z "$aus" ] || [ ! -f "$aus" ] || [ ! -x "$aus" ]; then
114 echo "devtrees: no austenite binary"
115 return 2
116 fi
117 aus="$(cd "$(dirname "$aus")" && pwd)/$(basename "$aus")"
118 local root
119 root="$(cd "$1" 2>/dev/null && pwd)" || { echo "tree $IDX missing-root"; return 1; }
120 shift
121 mkdir -p "$base" || { echo "devtrees: no scratch"; return 2; }
122 # Global, not local: the trap runs after this function has returned.
123 W="$(mktemp -d "$base/run.XXXXXX")" || { echo "devtrees: no scratch"; return 2; }
124 trap 'rm -rf "${W:?}"' EXIT
125 GATE_SCRATCH="$W"
126
127 local font="$root/assets/fonts" m path strict
128 MI=0
129 for m in "$@"; do
130 MI=$((MI + 1))
131 case "$m" in
132 /*) path="$m";;
133 *) path="$root/$m";;
134 esac
135 if [ ! -f "$path" ]; then
136 echo "tree $IDX main $MI missing-main"
137 continue
138 fi
139 DIR="$(dirname "$path")"
140 m="$(basename "$path")"
141 engine typst typst "$W/o/typst.pdf" "$typst" compile --root "$root" --font-path "$font" "$m" "$W/o/typst.pdf"
142 engine curated curated "$W/o/curated/document.pdf" "$aus" "$m" "$W/o/curated"
143 engine eval eval "$W/o/eval/document.pdf" "$aus" --eval --strict --diag-summary --root "$root" --font-path "$font" "$m" "$W/o/eval"
144 strict="$RC"
145 if [ "$strict" != 0 ]; then
146 engine eval-lax eval "$W/o/eval/document.pdf" "$aus" --eval --diag-summary --root "$root" --font-path "$font" "$m" "$W/o/eval"
147 fi
148 done
149 return 0
150}
151
152# The final whitelist, over everything main and its children wrote to either stream: the message of a tool
153# that failed, a shell's note of a process it lost, anything but these shapes is dropped.
154WHITELIST='
155 /^tree [0-9]+ main [0-9]+ (typst|curated|eval|eval-lax) exit [0-9]+ pages (none|[0-9]+) error [0-9]+ warning [0-9]+$/ { print; next }
156 /^tree [0-9]+ missing-root$/ { print; next }
157 /^tree [0-9]+ main [0-9]+ missing-main$/ { print; next }
158 /^devtrees: (typst is not 0\.15\.1|no austenite binary|usage|no scratch)$/ { print; next }'
159main "$@" 2>&1 | awk "$WHITELIST"
160exit "${PIPESTATUS[0]}"