#!/usr/bin/env bash # Shared helpers for the pipeline scripts (source it; do not run it). # options: --some-name VALUE sets SOME_NAME; switches listed in $SWITCHES (e.g. "skip-training") set the # variable to 1; --extra ... passes every following argument through unchanged (array EXTRA). # PYTHON and DEVICE can also be set in the environment (defaults: python, cuda). set -euo pipefail PYTHON="${PYTHON:-python}" DEVICE="${DEVICE:-cuda}" export PYTORCH_ALLOC_CONF="${PYTORCH_ALLOC_CONF:-expandable_segments:True}" EXTRA=() parse_args() { while [[ $# -gt 0 ]]; do case "$1" in -h|--help) sed -n '2,/^set -euo/p' "$0" | sed '$d'; exit 0 ;; --extra) shift; EXTRA=("$@"); break ;; --*) local key="${1#--}" var var="$(printf '%s' "$key" | tr 'a-z-' 'A-Z_')" if [[ " ${SWITCHES:-} " == *" $key "* ]]; then printf -v "$var" '%s' 1; shift else [[ $# -ge 2 ]] || { echo "missing value for $1" >&2; exit 2; } printf -v "$var" '%s' "$2"; shift 2 fi ;; *) echo "unknown argument: $1 (see --help)" >&2; exit 2 ;; esac done } log() { printf '\n\033[36m=== %s ===\033[0m\n' "$*"; } ok() { printf '\n\033[32m%s\033[0m\n' "$*"; } # run "Label" script.py args... (runs $PYTHON, stops the pipeline on failure) run() { local label="$1"; shift log "$label" echo "$PYTHON $*" "$PYTHON" "$@" || { echo "$label failed" >&2; exit 1; } } # need file... : stop unless every path exists need() { local f for f in "$@"; do [[ -e "$f" ]] || { echo "Missing $f" >&2; exit 1; }; done } # run_step "Label" ./other_script.sh args... (child pipeline script) run_step() { local label="$1"; shift log "$label" "$@" || { echo "$label failed" >&2; exit 1; } } # file size in bytes (GNU stat on Linux, BSD stat on macOS); 0 if missing fsize() { [[ -e "$1" ]] || { echo 0; return; }; stat -c %s "$1" 2>/dev/null || stat -f %z "$1"; }