adityss commited on
Commit
e82aa27
·
1 Parent(s): eb251de

feat: add OpenEnv submission validator script to check HF Space status and Docker build

Browse files
Files changed (1) hide show
  1. scripts/validate-submission.sh +181 -0
scripts/validate-submission.sh ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ #
3
+ # validate-submission.sh — OpenEnv Submission Validator
4
+ #
5
+ # Checks that your HF Space is live, Docker image builds, and openenv validate passes.
6
+ #
7
+ # Prerequisites:
8
+ # - Docker: https://docs.docker.com/get-docker/
9
+ # - openenv-core: pip install openenv-core
10
+ # - curl (usually pre-installed)
11
+ #
12
+ # Run:
13
+ # ./scripts/validate-submission.sh <ping_url> [repo_dir]
14
+ #
15
+ # Arguments:
16
+ # ping_url Your HuggingFace Space URL (e.g. https://your-space.hf.space)
17
+ # repo_dir Path to your repo (default: current directory)
18
+ #
19
+ # Examples:
20
+ # ./scripts/validate-submission.sh https://my-team.hf.space
21
+ # ./scripts/validate-submission.sh https://my-team.hf.space ./gridmind
22
+ #
23
+
24
+ set -uo pipefail
25
+
26
+ DOCKER_BUILD_TIMEOUT=600
27
+ if [ -t 1 ]; then
28
+ RED='\033[0;31m'
29
+ GREEN='\033[0;32m'
30
+ YELLOW='\033[1;33m'
31
+ BOLD='\033[1m'
32
+ NC='\033[0m'
33
+ else
34
+ RED='' GREEN='' YELLOW='' BOLD='' NC=''
35
+ fi
36
+
37
+ run_with_timeout() {
38
+ local secs="$1"; shift
39
+ if command -v timeout &>/dev/null; then
40
+ timeout "$secs" "$@"
41
+ elif command -v gtimeout &>/dev/null; then
42
+ gtimeout "$secs" "$@"
43
+ else
44
+ "$@" &
45
+ local pid=$!
46
+ ( sleep "$secs" && kill "$pid" 2>/dev/null ) &
47
+ local watcher=$!
48
+ wait "$pid" 2>/dev/null
49
+ local rc=$?
50
+ kill "$watcher" 2>/dev/null
51
+ wait "$watcher" 2>/dev/null
52
+ return $rc
53
+ fi
54
+ }
55
+
56
+ portable_mktemp() {
57
+ local prefix="${1:-validate}"
58
+ mktemp "${TMPDIR:-/tmp}/${prefix}-XXXXXX" 2>/dev/null || mktemp
59
+ }
60
+
61
+ CLEANUP_FILES=()
62
+ cleanup() { rm -f "${CLEANUP_FILES[@]+"${CLEANUP_FILES[@]}"}"; }
63
+ trap cleanup EXIT
64
+
65
+ PING_URL="${1:-}"
66
+ REPO_DIR="${2:-.}"
67
+
68
+ if [ -z "$PING_URL" ]; then
69
+ printf "Usage: %s <ping_url> [repo_dir]\n" "$0"
70
+ printf "\n"
71
+ printf " ping_url Your HuggingFace Space URL (e.g. https://your-space.hf.space)\n"
72
+ printf " repo_dir Path to your repo (default: current directory)\n"
73
+ exit 1
74
+ fi
75
+
76
+ if ! REPO_DIR="$(cd "$REPO_DIR" 2>/dev/null && pwd)"; then
77
+ printf "Error: directory '%s' not found\n" "${2:-.}"
78
+ exit 1
79
+ fi
80
+ PING_URL="${PING_URL%/}"
81
+ export PING_URL
82
+ PASS=0
83
+
84
+ log() { printf "[%s] %b\n" "$(date -u +%H:%M:%S)" "$*"; }
85
+ pass() { log "${GREEN}PASSED${NC} -- $1"; PASS=$((PASS + 1)); }
86
+ fail() { log "${RED}FAILED${NC} -- $1"; }
87
+ hint() { printf " ${YELLOW}Hint:${NC} %b\n" "$1"; }
88
+ stop_at() {
89
+ printf "\n"
90
+ printf "${RED}${BOLD}Validation stopped at %s.${NC} Fix the above before continuing.\n" "$1"
91
+ exit 1
92
+ }
93
+
94
+ printf "\n"
95
+ printf "${BOLD}========================================${NC}\n"
96
+ printf "${BOLD} OpenEnv Submission Validator${NC}\n"
97
+ printf "${BOLD}========================================${NC}\n"
98
+ log "Repo: $REPO_DIR"
99
+ log "Ping URL: $PING_URL"
100
+ printf "\n"
101
+
102
+ log "${BOLD}Step 1/3: Pinging HF Space${NC} ($PING_URL/reset) ..."
103
+
104
+ CURL_OUTPUT=$(portable_mktemp "validate-curl")
105
+ CLEANUP_FILES+=("$CURL_OUTPUT")
106
+ HTTP_CODE=$(curl -s -o "$CURL_OUTPUT" -w "%{http_code}" -X POST \
107
+ -H "Content-Type: application/json" -d '{}' \
108
+ "$PING_URL/reset" --max-time 30 2>"$CURL_OUTPUT" || printf "000")
109
+
110
+ if [ "$HTTP_CODE" = "200" ]; then
111
+ pass "HF Space is live and responds to /reset"
112
+ elif [ "$HTTP_CODE" = "000" ]; then
113
+ fail "HF Space not reachable (connection failed or timed out)"
114
+ hint "Check your network connection and that the Space is running."
115
+ hint "Try: curl -s -o /dev/null -w '%%{http_code}' -X POST $PING_URL/reset"
116
+ stop_at "Step 1"
117
+ else
118
+ fail "HF Space /reset returned HTTP $HTTP_CODE (expected 200)"
119
+ hint "Make sure your Space is running and the URL is correct."
120
+ hint "Try opening $PING_URL in your browser first."
121
+ stop_at "Step 1"
122
+ fi
123
+
124
+ log "${BOLD}Step 2/3: Running docker build${NC} ..."
125
+
126
+ if ! command -v docker &>/dev/null; then
127
+ fail "docker command not found"
128
+ hint "Install Docker: https://docs.docker.com/get-docker/"
129
+ stop_at "Step 2"
130
+ fi
131
+
132
+ if [ -f "$REPO_DIR/Dockerfile" ]; then
133
+ DOCKER_CONTEXT="$REPO_DIR"
134
+ elif [ -f "$REPO_DIR/server/Dockerfile" ]; then
135
+ DOCKER_CONTEXT="$REPO_DIR/server"
136
+ else
137
+ fail "No Dockerfile found in repo root or server/ directory"
138
+ stop_at "Step 2"
139
+ fi
140
+
141
+ log " Found Dockerfile in $DOCKER_CONTEXT"
142
+
143
+ BUILD_OK=false
144
+ BUILD_OUTPUT=$(run_with_timeout "$DOCKER_BUILD_TIMEOUT" docker build "$DOCKER_CONTEXT" 2>&1) && BUILD_OK=true
145
+
146
+ if [ "$BUILD_OK" = true ]; then
147
+ pass "Docker build succeeded"
148
+ else
149
+ fail "Docker build failed (timeout=${DOCKER_BUILD_TIMEOUT}s)"
150
+ printf "%s\n" "$BUILD_OUTPUT" | tail -20
151
+ stop_at "Step 2"
152
+ fi
153
+
154
+ log "${BOLD}Step 3/3: Running openenv validate${NC} ..."
155
+
156
+ if ! command -v openenv &>/dev/null; then
157
+ fail "openenv command not found"
158
+ hint "Install it: pip install openenv-core"
159
+ stop_at "Step 3"
160
+ fi
161
+
162
+ VALIDATE_OK=false
163
+ VALIDATE_OUTPUT=$(cd "$REPO_DIR" && openenv validate 2>&1) && VALIDATE_OK=true
164
+
165
+ if [ "$VALIDATE_OK" = true ]; then
166
+ pass "openenv validate passed"
167
+ [ -n "$VALIDATE_OUTPUT" ] && log " $VALIDATE_OUTPUT"
168
+ else
169
+ fail "openenv validate failed"
170
+ printf "%s\n" "$VALIDATE_OUTPUT"
171
+ stop_at "Step 3"
172
+ fi
173
+
174
+ printf "\n"
175
+ printf "${BOLD}========================================${NC}\n"
176
+ printf "${GREEN}${BOLD} All 3/3 checks passed!${NC}\n"
177
+ printf "${GREEN}${BOLD} Your submission is ready to submit.${NC}\n"
178
+ printf "${BOLD}========================================${NC}\n"
179
+ printf "\n"
180
+
181
+ exit 0