varshu23 commited on
Commit
4875aaa
·
1 Parent(s): bd7abac

first commit

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