Cotton code
37 lines · 2.1 KB · ShellRaw
#!/usr/bin/env bash# Cotton Albums' face model into the app's bundle, by its pinned SHA-256 (apps/cotton/App/Flavours/albums/# face-model.json). Run by the CottonAlbums targets as their last script phase (project.yml `scripts`).## The model is never in the repository (coordinator, 27 Nov). It is read from $COTTON_MODELS_DIR (default# ~/.cache/cottondb/models); when it is not there and the pin names a `url`, it is fetched from the file store into# that folder first. A missing file or a wrong hash FAILS THE BUILD: there is no fallback model, so no build can# ship without HyperFace, or with another model, by accident.set -euo pipefailhere="$(cd "$(dirname "$0")" && pwd)"pin="$here/../apps/cotton/App/Flavours/albums/face-model.json"read_pin() { /usr/bin/python3 -c "import json,sys; v=json.load(open(sys.argv[1])).get(sys.argv[2]); print('' if v is None else v)" "$pin" "$1"; }file="$(read_pin file)"want="$(read_pin sha256)"url="$(read_pin url)"dir="${COTTON_MODELS_DIR:-$HOME/.cache/cottondb/models}"archive="$dir/$file"fail() { echo "error: Cotton Albums face model: $*" >&2; exit 1; }if [ ! -f "$archive" ]; then  [ -n "$url" ] || fail "$archive is absent and face-model.json names no url yet. Put the archive (sha256 $want) there, or set COTTON_MODELS_DIR."  mkdir -p "$dir"  curl -fsSL --retry 3 -o "$archive.part" "$url" || fail "could not fetch $url"  mv "$archive.part" "$archive"figot="$(shasum -a 256 "$archive" | cut -d' ' -f1)"[ "$got" = "$want" ] || fail "$archive has sha256 $got; face-model.json pins $want. Delete it and fetch again."[ -n "${TARGET_BUILD_DIR:-}" ] && [ -n "${UNLOCALIZED_RESOURCES_FOLDER_PATH:-}" ] || fail "run by Xcode only (TARGET_BUILD_DIR is unset)"out="$TARGET_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/FaceModels"model="${file%.tar}"# Only this model's own folder is replaced, never anything else in the bundle.[ -d "$out/$model" ] && find "$out/$model" -deletemkdir -p "$out"tar -xf "$archive" -C "$out"[ -f "$out/$model/coremldata.bin" ] || fail "the archive did not hold $model"echo "Cotton Albums face model: $model ($got) in the bundle"