CoolFace
Apppublic

training-monkey/dataoncallenv

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
validate-submission.sh163 linesDownload Raw Back to root
1#!/usr/bin/env bash2#3# validate-submission.sh — OpenEnv Submission Validator4 5set -uo pipefail6 7DOCKER_BUILD_TIMEOUT=6008if [ -t 1 ]; then9  RED='\033[0;31m'10  GREEN='\033[0;32m'11  YELLOW='\033[1;33m'12  BOLD='\033[1m'13  NC='\033[0m'14else15  RED='' GREEN='' YELLOW='' BOLD='' NC=''16fi17 18run_with_timeout() {19  local secs="$1"; shift20  if command -v timeout &>/dev/null; then21    timeout "$secs" "$@"22  elif command -v gtimeout &>/dev/null; then23    gtimeout "$secs" "$@"24  else25    "$@" &26    local pid=$!27    ( sleep "$secs" && kill "$pid" 2>/dev/null ) &28    local watcher=$!29    wait "$pid" 2>/dev/null30    local rc=$?31    kill "$watcher" 2>/dev/null32    wait "$watcher" 2>/dev/null33    return $rc34  fi35}36 37portable_mktemp() {38  local prefix="${1:-validate}"39  mktemp "${TMPDIR:-/tmp}/${prefix}-XXXXXX" 2>/dev/null || mktemp40}41 42CLEANUP_FILES=()43cleanup() { rm -f "${CLEANUP_FILES[@]+"${CLEANUP_FILES[@]}"}"; }44trap cleanup EXIT45 46PING_URL="${1:-}"47REPO_DIR="${2:-.}"48 49if [ -z "$PING_URL" ]; then50  printf "Usage: %s <ping_url> [repo_dir]\n" "$0"51  printf "\n"52  printf "  ping_url   Your HuggingFace Space URL (e.g. https://your-space.hf.space)\n"53  printf "  repo_dir   Path to your repo (default: current directory)\n"54  exit 155fi56 57if ! REPO_DIR="$(cd "$REPO_DIR" 2>/dev/null && pwd)"; then58  printf "Error: directory '%s' not found\n" "${2:-.}"59  exit 160fi61PING_URL="${PING_URL%/}"62export PING_URL63PASS=064 65log()  { printf "[%s] %b\n" "$(date -u +%H:%M:%S)" "$*"; }66pass() { log "${GREEN}PASSED${NC} -- $1"; PASS=$((PASS + 1)); }67fail() { log "${RED}FAILED${NC} -- $1"; }68hint() { printf "  ${YELLOW}Hint:${NC} %b\n" "$1"; }69stop_at() {70  printf "\n"71  printf "${RED}${BOLD}Validation stopped at %s.${NC} Fix the above before continuing.\n" "$1"72  exit 173}74 75printf "\n"76printf "${BOLD}========================================${NC}\n"77printf "${BOLD}  OpenEnv Submission Validator${NC}\n"78printf "${BOLD}========================================${NC}\n"79log "Repo:     $REPO_DIR"80log "Ping URL: $PING_URL"81printf "\n"82 83log "${BOLD}Step 1/3: Pinging HF Space${NC} ($PING_URL/reset) ..."84 85CURL_OUTPUT=$(portable_mktemp "validate-curl")86CLEANUP_FILES+=("$CURL_OUTPUT")87HTTP_CODE=$(curl -s -o "$CURL_OUTPUT" -w "%{http_code}" -X POST \88  -H "Content-Type: application/json" -d '{}' \89  "$PING_URL/reset" --max-time 30 2>"$CURL_OUTPUT" || printf "000")90 91if [ "$HTTP_CODE" = "200" ]; then92  pass "HF Space is live and responds to /reset"93elif [ "$HTTP_CODE" = "000" ]; then94  fail "HF Space not reachable (connection failed or timed out)"95  hint "Check your network connection and that the Space is running."96  hint "Try: curl -s -o /dev/null -w '%%{http_code}' -X POST $PING_URL/reset"97  stop_at "Step 1"98else99  fail "HF Space /reset returned HTTP $HTTP_CODE (expected 200)"100  hint "Make sure your Space is running and the URL is correct."101  hint "Try opening $PING_URL in your browser first."102  stop_at "Step 1"103fi104 105log "${BOLD}Step 2/3: Running docker build${NC} ..."106 107if ! command -v docker &>/dev/null; then108  fail "docker command not found"109  hint "Install Docker: https://docs.docker.com/get-docker/"110  stop_at "Step 2"111fi112 113if [ -f "$REPO_DIR/Dockerfile" ]; then114  DOCKER_CONTEXT="$REPO_DIR"115elif [ -f "$REPO_DIR/server/Dockerfile" ]; then116  DOCKER_CONTEXT="$REPO_DIR/server"117else118  fail "No Dockerfile found in repo root or server/ directory"119  stop_at "Step 2"120fi121 122log "  Found Dockerfile in $DOCKER_CONTEXT"123 124BUILD_OK=false125BUILD_OUTPUT=$(run_with_timeout "$DOCKER_BUILD_TIMEOUT" docker build "$DOCKER_CONTEXT" 2>&1) && BUILD_OK=true126 127if [ "$BUILD_OK" = true ]; then128  pass "Docker build succeeded"129else130  fail "Docker build failed (timeout=${DOCKER_BUILD_TIMEOUT}s)"131  printf "%s\n" "$BUILD_OUTPUT" | tail -20132  stop_at "Step 2"133fi134 135log "${BOLD}Step 3/3: Running openenv validate${NC} ..."136 137if ! command -v openenv &>/dev/null; then138  fail "openenv command not found"139  hint "Install it: pip install openenv-core"140  stop_at "Step 3"141fi142 143VALIDATE_OK=false144VALIDATE_OUTPUT=$(cd "$REPO_DIR" && openenv validate 2>&1) && VALIDATE_OK=true145 146if [ "$VALIDATE_OK" = true ]; then147  pass "openenv validate passed"148  [ -n "$VALIDATE_OUTPUT" ] && log "  $VALIDATE_OUTPUT"149else150  fail "openenv validate failed"151  printf "%s\n" "$VALIDATE_OUTPUT"152  stop_at "Step 3"153fi154 155printf "\n"156printf "${BOLD}========================================${NC}\n"157printf "${GREEN}${BOLD}  All 3/3 checks passed!${NC}\n"158printf "${GREEN}${BOLD}  Your submission is ready to submit.${NC}\n"159printf "${BOLD}========================================${NC}\n"160printf "\n"161 162exit 0163