Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tools/bench/watch_edit.sh

3.3 KiB, 1 run, executable

created by r1870400018:68738, 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# The edit-to-view figures of `austenite watch` (F4): one warm compile after one letter is appended to the
3# document. The watch runs with the viewer off and its PDF in a scratch directory; after the first PDF the
4# script appends a letter, waits for the second status line, and reports from it the warm compile's
5# seconds, its passes and whether it ran warm, and the save-to-view time: the wall time from just before
6# the append to the moment the new PDF is seen in place, which holds the poll and the swap as well as the
7# compile. Both ends are read from the shell's own clock ($EPOCHREALTIME) and never from file times, which
8# on a loaded host are stamped from a coarse clock that can lag by more than a second.
9# The document is restored on exit, byte for byte and with its time.
10#
11# Usage: watch_edit.sh <austenite-binary> <doc.typ> [extra austenite arguments, e.g. --set k=v]...
12# Environment: SCRATCH (default: a new directory under ~/.cache/austenite/claude-rc-2/spd), WAIT_S (per
13# wait, default 240).
14set -uo pipefail
15
16BIN="${1:?usage: watch_edit.sh <austenite-binary> <doc.typ> [extra arguments]...}"
17DOC="${2:?usage: watch_edit.sh <austenite-binary> <doc.typ> [extra arguments]...}"
18shift 2
19WAIT_S="${WAIT_S:-240}"
20DOC="$(readlink -f "$DOC")"
21
22OWN=0
23if [ -z "${SCRATCH:-}" ]; then
24 mkdir -p "$HOME/.cache/austenite/claude-rc-2/spd"
25 SCRATCH="$(mktemp -d "$HOME/.cache/austenite/claude-rc-2/spd/watch_edit.XXXXXX")"
26 OWN=1
27fi
28mkdir -p "$SCRATCH"
29BACK="$SCRATCH/doc.orig"
30OUT="$SCRATCH/out.pdf"
31LOG="$SCRATCH/watch.log"
32cp -p "$DOC" "$BACK"
33PID=""
34
35cleanup() {
36 if [ -n "$PID" ]; then
37 kill "$PID" 2>/dev/null
38 wait "$PID" 2>/dev/null
39 fi
40 cp -p "$BACK" "$DOC"
41 if [ "$OWN" = 1 ]; then
42 rm -rf "${SCRATCH:?}"
43 fi
44}
45trap cleanup EXIT
46
47# Waits until the log holds $1 status lines (those that end a compile), or WAIT_S passes.
48status_lines() { grep -c 'page(s)' "$LOG" 2>/dev/null || true; }
49wait_for() {
50 local want="$1" t=0
51 while [ "$(status_lines)" -lt "$want" ]; do
52 if ! kill -0 "$PID" 2>/dev/null; then
53 echo "watch_edit: the watch ended before status line $want" >&2
54 tail -5 "$LOG" >&2
55 return 1
56 fi
57 if [ "$t" -ge $((WAIT_S * 20)) ]; then
58 echo "watch_edit: no status line $want within ${WAIT_S}s" >&2
59 return 1
60 fi
61 sleep 0.05
62 t=$((t + 1))
63 done
64}
65
66"$BIN" watch "$DOC" --set view.open=false --set "output=$OUT" "$@" > "$LOG" 2>&1 &
67PID=$!
68wait_for 1 || exit 1
69# Appended after a pause, so that the document's time differs from the build's reads. The first PDF is
70# kept as the reference for "a new PDF is in place": a later file time than it is the swap.
71sleep 1
72cp -p "$OUT" "$SCRATCH/first.pdf"
73SAVED=$EPOCHREALTIME
74printf 'x' >> "$DOC"
75t=0
76until [[ "$OUT" -nt "$SCRATCH/first.pdf" ]]; do
77 if [ "$t" -ge $((WAIT_S * 200)) ]; then
78 echo "watch_edit: no new PDF within ${WAIT_S}s" >&2
79 exit 1
80 fi
81 sleep 0.005
82 t=$((t + 1))
83done
84SEEN=$EPOCHREALTIME
85wait_for 2 || exit 1
86sleep 0.2
87
88LINE="$(grep 'page(s)' "$LOG" | sed -n 2p)"
89VIEW="$(awk -v s="$SAVED" -v e="$SEEN" 'BEGIN { printf "%.3f", e - s }')"
90SECS="$(echo "$LINE" | sed -n 's/.* page(s), \([0-9.]*\)s,.*/\1/p')"
91PASSES="$(echo "$LINE" | sed -n 's/.*, \([0-9]*\) pass.*/\1/p')"
92MODE="$(echo "$LINE" | sed -n 's/.*s, \(warm\|cold\), .*/\1/p')"
93echo "mode=$MODE compile_s=$SECS passes=$PASSES save_to_view_s=$VIEW"
94echo "line: $LINE"