forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB052d1d3Tahinli2 months ago3627 commits

gen-fixture-library.sh394 lines, 18.4 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# gen-fixture-library.sh — GENERATE scripts/fixture-library.tsv from source.
3#
4# The tsv is committed output, never hand-typed. Regenerate with:
5# scripts/gen-fixture-library.sh && git diff --exit-code scripts/fixture-library.tsv
6# so drift between "what the tests reach for" and "the manifest" is a
7# one-command diff. `scripts/verify-fixture-library.sh` runs that same diff as
8# part of its exit status.
9#
10# COLUMNS (tab separated)
11# 1 path repo-relative; `fixtures/...` is relative to $EC_FIXTURES
12# 2 required-by file:line of every reference (one row per reference)
13# 3 provenance the script/recipe that reproduces the bytes, or
14# `none (prose provenance only)` — a FINDING, not a
15# placeholder: a pinned fixture claim must cite the
16# originating encoder program and the artifact sha256, or the
17# pin is unreproducible (the IVF-container drift incident is
18# why that rule exists)
19# 4 status ok | missing | empty-dir | absent-pin
20# 5 sha256 of the artefact, when it is a regular file under 4 MiB
21#
22# ENUMERATION SHAPES the scan is derived from:
23# CARGO_MANIFEST_DIR env!("CARGO_MANIFEST_DIR") / concat!(env!(...))
24# concat!(env!( a fixture root assembled from the manifest dir
25# include_bytes! include_bytes!/include_str! argument literals
26# "fixtures/ any string literal naming the fixture tree
27# ../../fixtures crate-relative literals (CWD is the crate dir)
28# Path::new( Path::new("...") argument literals
29# fs::read fs::read / read_dir / File::open argument literals
30#
31# library_fixture:: a path passed to the ec-av1 library-fixture resolver
32# (`library_fixture::require("video/…", …)`) is
33# LIBRARY-RELATIVE, so it anchors at the fixture root
34# the preflight validates (EC_FIXTURES first) rather
35# than at any crate directory. Without this row shape
36# the RESOLVE pass silently lost every clip row the
37# moment a gate stopped naming `../../fixtures`
38# literally -- the preflight would stop checking the
39# exact paths whose absence this lane is about.
40# TWO PASSES:
41# 1. fixture-root / fixture-path literals -> the row is the literal itself.
42# 2. bare media-file literals ("aac-adts-5.1-44100.aac", "entry-tx3g.mp4")
43# joined onto each fixture directory literal seen in the SAME file, then
44# onto the standard top-level library directories. Only joins that
45# resolve are emitted; unresolved names are counted in the header as the
46# declared blind spot.
47#
48# BLIND SPOT (honest: this is a preflight, not a proof). The scan is literal-
49# and grep-derived, so it cannot see a path assembled at runtime from a value
50# that is not a literal in the same file -- a vector name read from
51# scripts/vectors.sha256, a row from fixtures/real-library-manifest.tsv, a
52# format! interpolation, a name in a table. For those, the parent DIRECTORY is
53# still enumerated, and the verifier requires every listed directory to exist
54# AND be non-empty -- that is what catches the "three hosts whose fixture
55# libraries differed" class. A single missing member of a runtime-named corpus
56# shows up as a regeneration diff, not as a resolve failure.
57
58set -uo pipefail
59
60ROOT=$(cd -- "$(dirname -- "$0")/.." && pwd)
61OUT=${1:-$ROOT/scripts/fixture-library.tsv}
62FIXTURES=${EC_FIXTURES:-$ROOT/fixtures}
63
64MEDIA_RE='\.(mp4|m4a|m4s|m4v|mkv|ivf|obu|flac|opus|ogg|oga|webm|265|264|h264|h265|hevc|y4m|yuv|wav|aac|ts|bin|ttml|srt|vtt|ass|json)'
65
66# Pins that are KNOWN ABSENT and must not vanish from the manifest. Each is
67# recorded as an `absent-pin` row naming the gate that wants it, so the gap is
68# visible instead of silently skipped.
69# Provenance CLASS per row, kept separate from the GENERATOR script so a
70# recovered witness is never confused with a re-encode:
71# recovered-original the historical bytes, copied from a runner library; a
72# re-encode is a DIFFERENT stream and would not hash the
73# same, so column 7 is the self-validation
74# captured committed bytes whose only provenance is a gate comment
75# (a FINDING: no encoder recipe is recorded)
76# regenerated a script in column 4 reproduces the bytes
77# absent-pin referenced by code, not present; named so the gap is a row
78# host-corpus a developer media file, not part of the library
79RECOVERED_PINS="golden4-pin.obu golden6-mismatch.obu golden7-forwarding-mismatch.obu lr-sgr-r7.obu"
80
81pin_class() { # $1 = manifest path -> class
82 local path=$1 base
83 base=${path##*/}
84 case " $RECOVERED_PINS " in
85 *" $base "*)
86 case $path in
87 crates/*/fixtures/*) echo "recovered-original" ;;
88 *) echo "regenerated" ;; # a same-named generated artefact
89 esac
90 return 0 ;;
91 esac
92 case $path in
93 fixtures/ll444-lossless-key.obu | fixtures/golden3-pin.obu | fixtures/sbpart-pin.obu)
94 echo "absent-pin" ;;
95 fixtures/*.obu | crates/*/fixtures/*.obu) echo "captured" ;;
96 *) echo "regenerated" ;;
97 esac
98}
99
100# --- 1. collect the source files that can name a fixture -----------------
101mapfile -t FILES < <(
102 grep -rl --include='*.rs' \
103 -e 'fixtures' -e 'include_bytes!' -e 'include_str!' \
104 "$ROOT"/crates/*/tests "$ROOT"/crates/*/src 2>/dev/null | sort
105)
106
107is_fixture_literal() { # $1 = literal without quotes
108 local lit=$1
109 case $lit in
110 *[[:space:]]* | *'{'* | *'}'* | *'$'* | *'%'* | *'*'* | *'?'* | \
111 *'('* | *')'* | *','* | *'='* | *';'* | *':'*) return 1 ;;
112 *.sh | *.py | *.md | *.tsv) return 1 ;; # a script name, not a path
113 *'...'*) return 1 ;; # an ellipsis placeholder, not a path
114 /home/* | /Users/* | /tmp/*) return 1 ;; # developer path, not the library
115 esac
116 [[ $lit == *fixtures* || $lit == */vectors/* ]] || return 1
117 return 0
118}
119
120# lane-av1clipprobe: a literal that starts with one of the fixture library's
121# own top-level directories is LIBRARY-RELATIVE, not crate-relative: it is what
122# the ec-av1 library-fixture resolver takes (`require("video/…")`), and the
123# other crates' `../../fixtures/<dir>` + `.join("video/…")` pairs resolve to the
124# same manifest path. So it maps to `fixtures/<lit>` -- the same row, whether
125# the consumer spells the root out or hands the relative part to the resolver.
126library_relative() { # $1 = literal without quotes
127 case $1 in
128 audio/* | video/* | bitstreams/* | stills/* | subs/* | realworld/* | \
129 vp8/* | vp9/* | hbd-r5/* | vectors/*) return 0 ;;
130 esac
131 return 1
132}
133
134# Provenance: which script reproduces these bytes. An empty answer is a
135# finding -- committed pins whose only provenance is prose in a gate comment.
136generator_for() { # $1 = repo-relative path
137 case $1 in
138 fixtures/vectors/*) echo "scripts/fetch-vectors.sh" ;;
139 fixtures/bitstreams/*) echo "scripts/gen-bitstream-fixtures.sh" ;;
140 fixtures/stills/*) echo "scripts/gen-still-fixtures.sh" ;;
141 fixtures/subs/*) echo "scripts/gen-subtitle-fixtures.sh" ;;
142 fixtures/real-library-manifest.tsv) echo "scripts/scan-real-library.sh" ;;
143 fixtures/vp8/*) echo "scripts/gen_vp8_fixtures.sh" ;;
144 fixtures/*.obu) echo "none (prose provenance only)" ;;
145 crates/*/fixtures/*.obu) echo "none (prose provenance only)" ;;
146 fixtures/audio/mp3* | fixtures/audio/*mp3*) echo "scripts/gen-mp3-fixtures.sh" ;;
147 *) echo "scripts/gen-fixtures.sh" ;;
148 esac
149}
150
151abs_path() { # $1 = manifest path -> absolute path
152 case $1 in
153 fixtures) printf '%s\n' "$FIXTURES" ;;
154 fixtures/*) printf '%s\n' "$FIXTURES/${1#fixtures/}" ;;
155 *) printf '%s\n' "$ROOT/$1" ;;
156 esac
157}
158
159# Resolve a source literal to a manifest path. Cargo runs tests with the crate
160# directory as CWD, so `../../fixtures` and `CARGO_MANIFEST_DIR` both anchor at
161# the crate; a bare `fixtures/...` is accepted from either anchor.
162resolve() { # $1 = literal, $2 = crate dir; prints manifest path
163 local lit=$1 crate=$2 base cand
164 for base in "$crate" "$ROOT" "$ROOT/crates"; do
165 # An anchor only counts if the resolved path is INSIDE this tree. A
166 # `../../fixtures/...` literal tried against $ROOT or $ROOT/crates walks
167 # above the repo -- on a runner host it matched a directory belonging to
168 # something else entirely and produced rows like
169 # `home/remote-user/fixtures/audio/x.mka`, which no other host can ever
170 # match. Containment is the test, not existence.
171 [ -e "$base/$lit" ] || continue
172 cand=$(realpath -m -- "$base/$lit")
173 [[ $cand == "$ROOT"/* ]] || continue
174 printf '%s\n' "${cand#"$ROOT"/}"
175 return 0
176 done
177 # Unresolved: normalise the LITERAL, not the resolved path. In a tree
178 # WITHOUT a root `fixtures/` (every linked worktree, every runner host that
179 # stages its library elsewhere) no anchor matches, and realpath -m walks
180 # above the repo -- on a host whose parent directory holds a same-named
181 # directory it hands back `/home/<user>/fixtures/audio`, whose leading
182 # slash, if merely stripped, becomes the row
183 # `home/<user>/fixtures/audio`: a machine-specific path that no other host
184 # can ever match, and a silent DRIFT on every host but the one that made
185 # it. Dropping the literal's own `../` and `./` prefixes keeps the row
186 # manifest-relative -- `fixtures/audio` -- so the verifier resolves it
187 # through EC_FIXTURES and every host agrees on the row.
188 local out=$lit
189 while [[ $out == /* ]]; do out=${out#/}; done
190 while [[ $out == ../* ]]; do out=${out#../}; done
191 while [[ $out == ./ ]]; do out=${out#./}; done
192 printf '%s\n' "$out"
193}
194
195row() { # $1 path, $2 required-by, $3 class (optional), $4 status override
196 local path=$1 required_by=$2 class=${3:-} status_override=${4:-}
197 local abs status sum
198 abs=$(abs_path "$path")
199 if [ -n "$status_override" ]; then
200 status=$status_override
201 elif [ ! -e "$abs" ]; then
202 status=missing
203 elif [ -d "$abs" ] && [ -z "$(ls -A -- "$abs" 2>/dev/null)" ]; then
204 status=empty-dir
205 else
206 status=ok
207 fi
208 local prov tracked
209 if [ -z "$class" ]; then class=$(pin_class "$path"); fi
210 case $class in
211 captured | recovered-original) prov='-' ;; # no recipe: a FINDING
212 *) prov=$(generator_for "$path") ;;
213 esac
214 case $path in
215 crates/*/fixtures/*)
216 if [ -e "$ROOT/.git" ]; then
217 if git -C "$ROOT" ls-files --error-unmatch -- "$path" >/dev/null 2>&1; then
218 tracked=yes
219 else
220 tracked=no
221 fi
222 else
223 tracked='no-git'
224 fi ;;
225 *) tracked='-' ;;
226 esac
227 if [ -f "$abs" ] && [ "$(stat -c %s -- "$abs")" -lt 4194304 ]; then
228 sum=$(sha256sum -- "$abs" | cut -d' ' -f1)
229 else
230 sum='-'
231 fi
232 printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
233 "$path" "$required_by" "$class" "$prov" "$status" "$tracked" "$sum"
234}
235
236tmp=$(mktemp)
237trap 'rm -f "$tmp"' EXIT
238unnamed=0
239seen_absent=()
240
241for f in "${FILES[@]}"; do
242 crate=$(dirname "$(dirname "$f")")
243 rel=${f#"$ROOT"/}
244 file_dirs=()
245
246 # Pass 1 -- fixture-root / fixture-path literals.
247 while IFS= read -r hit; do
248 lineno=${hit%%:*}
249 lit=${hit#*:}
250 lit=${lit#\"}
251 lit=${lit%\"}
252 is_fixture_literal "$lit" || continue
253 path=$(resolve "$lit" "$crate")
254 case $(pin_class "$path") in
255 absent-pin)
256 row "$path" "$rel:$lineno" absent-pin absent-pin >>"$tmp"
257 continue ;;
258 esac
259 row "$path" "$rel:$lineno" >>"$tmp"
260 [ -d "$(abs_path "$path")" ] && file_dirs+=("$path")
261 done < <(grep -noE '"[^"]*"' "$f")
262
263 # Pass 1b -- the ec-av1 library-fixture RESOLVER's literals (lane-
264 # av1clipprobe). `library_fixture::require("video/…", …)` takes a path
265 # RELATIVE TO THE FIXTURE ROOT, so it is not a crate-relative literal and
266 # Pass 1 cannot classify it -- and a clip gate that had stopped naming
267 # `../../fixtures` literally would have dropped its row from this manifest
268 # entirely, i.e. the preflight would have stopped RESOLVING the very paths
269 # whose silent absence this lane is about. Scoped to lines that actually
270 # call the resolver: a bare `audio/…` literal in another crate keeps
271 # whatever classification it had, so this adds no rows outside ec-av1.
272 while IFS= read -r ln; do
273 lineno=${ln%%:*}
274 while IFS= read -r q; do
275 lit=${q#\"}
276 lit=${lit%\"}
277 library_relative "$lit" || continue
278 path="fixtures/$lit"
279 case $(pin_class "$path") in
280 absent-pin)
281 row "$path" "$rel:$lineno" absent-pin absent-pin >>"$tmp"
282 continue ;;
283 esac
284 row "$path" "$rel:$lineno" >>"$tmp"
285 [ -d "$(abs_path "$path")" ] && file_dirs+=("$path")
286 done < <(printf '%s\n' "${ln#*:}" | grep -oE '"[^"]*"')
287 done < <(grep -nE 'library_fixture::(require|require_at|path)\(' "$f")
288 # A resolver call names a FILE inside a library directory; the DIRECTORY row
289 # is what makes RESOLVE check the corpus is present AND non-empty, and it
290 # used to come from the `../../fixtures/video` literal this lane removed.
291 # One row per directory per file, so the manifest does not grow a
292 # duplicate per call site.
293 while IFS= read -r ln; do
294 d=""
295 while IFS= read -r q; do
296 lit=${q#\"}
297 lit=${lit%\"}
298 library_relative "$lit" || continue
299 d="fixtures/${lit%%/*}"
300 done < <(printf '%s\n' "${ln#*:}" | grep -oE '"[^"]*"')
301 [ -n "$d" ] || continue
302 case " ${resolver_dirs:-} " in *" $d "*) continue ;; esac
303 resolver_dirs="${resolver_dirs:-} $d"
304 case $(pin_class "$d") in
305 absent-pin) continue ;;
306 esac
307 row "$d" "$rel:${ln%%:*}" >>"$tmp"
308 done < <(grep -nE 'library_fixture::(require|require_at|path)\(' "$f")
309
310 # Pass 2 -- bare media names joined onto this file's fixture directories,
311 # then onto the standard top-level library directories (a file may name
312 # its corpus directory in a helper module rather than in this file).
313 for d in audio video bitstreams subs stills realworld vp8 vp9 hbd-r5; do
314 [ -d "$FIXTURES/$d" ] && file_dirs+=("fixtures/$d")
315 done
316 ((${#file_dirs[@]})) || continue
317 while IFS= read -r hit; do
318 lineno=${hit%%:*}
319 name=${hit#*:}
320 name=${name#\"}
321 name=${name%\"}
322 for d in "${file_dirs[@]}"; do
323 if [ -e "$(abs_path "$d/$name")" ]; then
324 row "$d/$name" "$rel:$lineno" >>"$tmp"
325 continue 2
326 fi
327 done
328 unnamed=$((unnamed + 1))
329 done < <(grep -noE '"[^"/]*'"$MEDIA_RE"'"' "$f")
330done
331
332# Pass 3 -- committed pins that NO source literal reaches. A committed
333# fixture nothing references is either dead weight or a reference built at
334# runtime from a name the grep cannot see; either way it must be a row, not a
335# gap. Scanned over the crate-local committed pin directories.
336orphans=0
337while IFS= read -r pin; do
338 [ -n "$pin" ] || continue
339 if ! grep -qP "^\Q$pin\E\t" "$tmp"; then
340 row "$pin" "(committed pin: NO source reference found)" >>"$tmp"
341 orphans=$((orphans + 1))
342 fi
343done < <(find "$ROOT"/crates/*/fixtures -maxdepth 1 -type f 2>/dev/null |
344 sed "s#^$ROOT/##" | sort)
345
346# A known-absent pin is annotated where a source literal actually REACHES for
347# it (pass 1, above: `absent_pin` supplies the provenance and the `absent-pin`
348# status). Nothing is injected here: a pin no code references any more is not
349# a gap in the library, it is a deleted reference, and inventing a row for it
350# would keep a stale claim alive. A pin that IS referenced and IS absent still
351# gets a row, because pass 1 emits one for every literal.
352
353# Which tests consume the fetched vector sets, and does anything need the
354# .tar.gz blobs? The fleet sync excluded them deliberately, so a test that
355# wanted a blob would be red on three hosts for a reason nobody wrote down.
356vector_consumers=$(awk -F'\t' '$1 ~ /^fixtures\/vectors\// {split($2,a,":"); print a[1]}' "$tmp" | sort -u | tr '\n' ' ')
357blob_users=$(grep -rl --include='*.rs' '\.tar\.gz' "$ROOT"/crates 2>/dev/null | tr '\n' ' ')
358
359{
360 cat <<EOF
361# GENERATED by scripts/gen-fixture-library.sh -- do not hand-edit.
362# Regenerate: scripts/gen-fixture-library.sh
363# Verify: scripts/verify-fixture-library.sh
364# path <TAB> required-by file:line <TAB> provenance <TAB> status <TAB> sha256
365#
366# Enumeration is grep-derived. It cannot see a fixture name that is not a
367# string literal in the same file (manifest rows, vectors.sha256 names,
368# format! interpolation); such names are only covered through their parent
369# directory, which the verifier requires to exist AND be non-empty.
370# $unnamed media-name literal(s) in this tree resolved under no enumerated
371# fixture directory and are therefore not listed individually.
372#
373# FIXTURE ROOT: worktree-relative, deliberately NOT an absolute path. A
374# generated file in the repo must not carry the authoring machine's home
375# directory: this repo scans commit MESSAGES for host paths, and a generated
376# artefact is exactly how one slips past that check. The path is resolved
377# through EC_FIXTURES at run time by verify-fixture-library.sh.
378#
379# HOST-STATE LINES (per-host by nature, excluded from the drift comparison,
380# which diffs the ROWS only): the unresolved-literal count above, and the
381# status/tracked/sha256 columns below.
382#
383# VECTORS consumers (fixtures/vectors/*): ${vector_consumers:-none}
384# VECTORS .tar.gz blob referenced by: ${blob_users:-nothing}
385# The three fleet hosts carry the EXTRACTED vector sets (~470 files) and the
386# sync deliberately excluded the .tar.gz blobs; nothing in crates/ reads a
387# blob, so the extracted directories are the whole requirement.
388# Unreferenced committed pins carried as rows: $orphans
389EOF
390 sort -u "$tmp"
391} >"$OUT"
392
393rows=$(grep -cv '^#' "$OUT")
394echo "gen-fixture-library: $rows rows -> ${OUT#"$ROOT"/} ($unnamed unnamed media literals, $orphans unreferenced committed pins)" >&2