okami.mom/strata

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

Niko1221Merge f-split-linuxui (83aa59fdc) into f-split-int2b6a38b328

main
5.0 KiB85 linesraw
1#!/usr/bin/env bash
2# Write a tester's README with the checksums and sizes of the files it names, computed from the files themselves.
3#
4#   apps/desktop/scripts/make-tester-readme.sh <template> <out.txt> --appimage <file name> --files-dir <dir> [--files-dir <dir> ...]
5#
6# Why: a README that carries a hand-pasted sha256 goes stale the moment the file is rebuilt (the Ubuntu 26.04 tester compared the file
7# with the old build's hash). The template holds placeholders instead, and this script fills them from the real files:
8#   @APPIMAGE@            the AppImage's file name
9#   @APP_VERSION@         the app version, read from apps/desktop/src-tauri/Cargo.toml
10#   @SHA256:<name>@       sha256 of the file <name> found in one of the --files-dir folders
11#   @SIZE_GB:<name>@      its size in GB with one decimal
12#   @INCLUDE:<file>@      (alone on a line) the contents of <file> from the template's folder, with the same placeholders
13# It refuses to write the README when a named file is missing or a placeholder is left over, so a stale or empty hash cannot ship.
14# The same check is done by `--verify <readme> --files-dir <dir>`: every "<64 hex>  <name>" or "sha256 <64 hex>" pair that names a file
15# present in the folders must match that file.
16set -euo pipefail
17
18usage() { echo "usage: $0 <template> <out.txt> --appimage <name> --files-dir <dir> [--files-dir <dir> ...]" >&2; echo "       $0 --verify <readme> --files-dir <dir> [...]" >&2; exit 2; }
19
20VERIFY=""; TEMPLATE=""; OUT=""; APPIMAGE=""; DIRS=()
21if [ "${1:-}" = "--verify" ]; then VERIFY="${2:?readme}"; shift 2; else TEMPLATE="${1:?template}"; OUT="${2:?out}"; shift 2 || usage; fi
22while [ $# -gt 0 ]; do
23  case "$1" in
24    --appimage) APPIMAGE="$2"; shift 2 ;;
25    --files-dir) DIRS+=("$2"); shift 2 ;;
26    *) usage ;;
27  esac
28done
29[ ${#DIRS[@]} -gt 0 ] || usage
30
31find_file() { local d; for d in "${DIRS[@]}"; do [ -f "$d/$1" ] && { echo "$d/$1"; return 0; }; done; return 1; }
32sha_of() { sha256sum "$1" | awk '{print $1}'; }
33size_gb() { awk -v b="$(stat -c %s "$1" 2>/dev/null || stat -f %z "$1")" 'BEGIN{printf "%.1f", b/1000000000}'; }
34
35if [ -n "$VERIFY" ]; then
36  # every checksum in the README must be the checksum of a file that is here, and a line that names a file and carries a checksum must carry that file's checksum
37  hashes=""; bad=0; seen=0
38  for d in "${DIRS[@]}"; do for f in "$d"/*; do [ -f "$f" ] && grep -qF "$(basename "$f")" "$VERIFY" && hashes="$hashes $(sha_of "$f")"; done; done   # only files the README names are hashed (a folder may hold big unrelated files)
39  readme_hashes="$(grep -Eo '[0-9a-f]{64}' "$VERIFY" | sort -u || true)"
40  for h in $readme_hashes; do
41    seen=$((seen + 1))
42    case " $hashes " in *" $h "*) ;; *) echo "STALE: the README lists $h, which is the checksum of no file in: ${DIRS[*]}" >&2; bad=$((bad + 1)) ;; esac
43  done
44  for d in "${DIRS[@]}"; do for f in "$d"/*; do
45    [ -f "$f" ] || continue
46    grep -qF "$(basename "$f")" "$VERIFY" || continue
47    # a line that names this file AND carries a checksum must carry this file's checksum (a sentence that merely mentions the file is fine)
48    line_hash="$(grep -F "$(basename "$f")" "$VERIFY" | grep -Eo '[0-9a-f]{64}' | head -1 || true)"
49    if [ -n "$line_hash" ] && [ "$line_hash" != "$(sha_of "$f")" ]; then
50      echo "STALE: $(basename "$f") is $(sha_of "$f"), the README says $line_hash" >&2; bad=$((bad + 1))
51    fi
52  done; done
53  echo "$seen checksum(s) in the README checked, $bad stale"
54  [ "$bad" = 0 ] && [ "$seen" -gt 0 ]
55  exit
56fi
57
58[ -n "$APPIMAGE" ] || usage
59TDIR="$(cd "$(dirname "$TEMPLATE")" && pwd)"
60# the app version has one source (the same file Tauri and the shell use)
61APP_VERSION="$(grep -m1 '^version = ' "$TDIR/../src-tauri/Cargo.toml" | cut -d'"' -f2)"
62[ -n "$APP_VERSION" ] || { echo "make-tester-readme: no version in $TDIR/../src-tauri/Cargo.toml" >&2; exit 1; }
63TMP="$(mktemp)"; trap 'rm -f "$TMP" "$TMP.2"' EXIT
64# includes first (one level), then the placeholders
65while IFS= read -r line || [ -n "$line" ]; do
66  if [[ "$line" =~ ^@INCLUDE:([^@]+)@$ ]]; then tr -d '\r' < "$TDIR/${BASH_REMATCH[1]}"; else printf '%s\n' "$line"; fi
67done < <(tr -d '\r' < "$TEMPLATE") | sed "s|@APPIMAGE@|$APPIMAGE|g; s|@APP_VERSION@|$APP_VERSION|g" > "$TMP"
68
69# @SHA256:name@ and @SIZE_GB:name@
70while IFS= read -r token; do
71  kind="${token#@}"; kind="${kind%%:*}"; name="${token#@*:}"; name="${name%@}"
72  f="$(find_file "$name")" || { echo "make-tester-readme: no file named $name in: ${DIRS[*]} (a README must not carry a hash it did not compute)" >&2; exit 1; }
73  case "$kind" in
74    SHA256) value="$(sha_of "$f")" ;;
75    SIZE_GB) value="$(size_gb "$f")" ;;
76    *) echo "make-tester-readme: unknown placeholder $token" >&2; exit 1 ;;
77  esac
78  # replace every occurrence of this exact token (plain text, no pattern characters)
79  text="$(cat "$TMP"; printf x)"; text="${text%x}"
80  printf '%s' "${text//"$token"/$value}" > "$TMP"
81done < <(grep -Eo '@(SHA256|SIZE_GB):[^@ ]+@' "$TMP" | sort -u)
82
83if grep -Eq '@[A-Z0-9_]+[:@]' "$TMP"; then echo "make-tester-readme: placeholders left over:" >&2; grep -En '@[A-Z0-9_]+[:@]' "$TMP" >&2; exit 1; fi
84cp "$TMP" "$OUT"
85echo "wrote $OUT"