-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
425 lines (332 loc) · 12.3 KB
/
validate.py
File metadata and controls
425 lines (332 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python3
"""
Pre-submission validation script for Code Reviewer Environment.
Run this before submitting to ensure all requirements are met.
"""
import os
import sys
import subprocess
import json
import shutil
def print_header(text):
print("\n" + "=" * 60)
print(f" {text}")
print("=" * 60)
def print_success(text):
print(f" [OK] {text}")
def print_error(text):
print(f" [FAIL] {text}")
def print_warning(text):
print(f" [WARN] {text}")
def check_file_exists(filepath, description):
"""Check if a file exists."""
if os.path.exists(filepath):
print_success(f"{description}: {filepath}")
return True
else:
print_error(f"Missing {description}: {filepath}")
return False
def check_required_files():
"""Check all required files exist."""
print_header("Checking Required Files")
required_files = [
("openenv.yaml", "OpenEnv specification"),
("Dockerfile", "Docker configuration"),
("requirements.txt", "Python dependencies"),
("README.md", "Documentation"),
("inference.py", "Baseline inference script"),
("models.py", "Pydantic models"),
("environment.py", "Environment implementation"),
("tasks.py", "Task definitions"),
("server.py", "WebSocket server"),
]
all_exist = True
for filepath, description in required_files:
if not check_file_exists(filepath, description):
all_exist = False
return all_exist
def check_openenv_yaml():
"""Validate openenv.yaml structure."""
print_header("Validating openenv.yaml")
try:
import yaml
with open("openenv.yaml", "r", encoding="utf-8") as f:
config = yaml.safe_load(f)
required_keys = ["name", "version", "description", "interface", "endpoints", "models", "config"]
for key in required_keys:
if key in config:
print_success(f"Has '{key}'")
else:
print_error(f"Missing '{key}'")
return False
# Check tasks
tasks = config.get("config", {}).get("available_tasks", [])
if len(tasks) >= 3:
print_success(f"Has {len(tasks)} tasks: {tasks}")
else:
print_error(f"Need at least 3 tasks, found {len(tasks)}")
return False
return True
except Exception as e:
print_error(f"Error parsing openenv.yaml: {e}")
return False
def check_dockerfile():
"""Validate Dockerfile."""
print_header("Validating Dockerfile")
try:
with open("Dockerfile", "r", encoding="utf-8") as f:
content = f.read()
required_elements = [
("FROM", "Base image"),
("WORKDIR", "Working directory"),
("requirements.txt", "Requirements installation"),
("EXPOSE", "Port exposure"),
("CMD", "Run command"),
]
for element, description in required_elements:
if element in content:
print_success(f"Has {description}")
else:
print_error(f"Missing {description}")
return False
return True
except Exception as e:
print_error(f"Error checking Dockerfile: {e}")
return False
def check_inference_script():
"""Validate inference.py structure."""
print_header("Validating inference.py")
try:
with open("inference.py", "r", encoding="utf-8") as f:
content = f.read()
# Check logging format
required_patterns = [
("[START]", "START log format"),
("[STEP]", "STEP log format"),
("[END]", "END log format"),
("log_start", "log_start function"),
("log_step", "log_step function"),
("log_end", "log_end function"),
("API_BASE_URL", "API_BASE_URL variable"),
("MODEL_NAME", "MODEL_NAME variable"),
("HF_TOKEN", "HF_TOKEN variable"),
("from openai import OpenAI", "OpenAI import"),
]
for pattern, description in required_patterns:
if pattern in content:
print_success(f"Has {description}")
else:
print_error(f"Missing {description}")
return False
return True
except Exception as e:
print_error(f"Error checking inference.py: {e}")
return False
def check_models():
"""Validate Pydantic models."""
print_header("Validating Pydantic Models")
try:
sys.path.insert(0, os.getcwd())
from models import (
CodeReviewerObservation,
CodeReviewerAction,
CodeReviewerReward,
CodeIssue,
)
print_success("All models import successfully")
# Test model instantiation
from models import CodeSnippet, IssueType, Severity
snippet = CodeSnippet(language="python", code="print('test')")
print_success("CodeSnippet instantiates")
issue = CodeIssue(
line_number=1,
issue_type=IssueType.SYNTAX_ERROR,
severity=Severity.HIGH,
description="Test",
)
print_success("CodeIssue instantiates")
action = CodeReviewerAction(action_type="test", issue=issue)
print_success("CodeReviewerAction instantiates")
return True
except Exception as e:
print_error(f"Model validation failed: {e}")
return False
def check_environment():
"""Validate environment implementation."""
print_header("Validating Environment")
try:
sys.path.insert(0, os.getcwd())
from environment import CodeReviewerEnv
from models import CodeReviewerAction
# Test reset
env = CodeReviewerEnv("syntax_check")
obs = env.reset()
print_success("Environment reset works")
# Test step
action = CodeReviewerAction(action_type="submit_review")
obs, reward, done, info = env.step(action)
print_success("Environment step works")
# Test state
state = env.state()
print_success("Environment state() works")
return True
except Exception as e:
print_error(f"Environment validation failed: {e}")
import traceback
traceback.print_exc()
return False
def check_tasks():
"""Validate task definitions."""
print_header("Validating Tasks")
try:
sys.path.insert(0, os.getcwd())
from tasks import TASKS
task_count = len(TASKS)
if task_count >= 3:
print_success(f"Has {task_count} tasks")
else:
print_error(f"Need at least 3 tasks, found {task_count}")
return False
# Check difficulties
difficulties = [task.difficulty for task in TASKS.values()]
if "easy" in difficulties:
print_success("Has easy task")
else:
print_error("Missing easy task")
return False
if "medium" in difficulties:
print_success("Has medium task")
else:
print_error("Missing medium task")
return False
if "hard" in difficulties:
print_success("Has hard task")
else:
print_error("Missing hard task")
return False
# Check graders
for task_name, task in TASKS.items():
if len(task.expected_issues) > 0:
print_success(f"'{task_name}' has {len(task.expected_issues)} expected issues")
else:
print_error(f"'{task_name}' has no expected issues")
return False
return True
except Exception as e:
print_error(f"Task validation failed: {e}")
import traceback
traceback.print_exc()
return False
def check_readme():
"""Validate README.md."""
print_header("Validating README.md")
try:
with open("README.md", "r", encoding="utf-8") as f:
content = f.read()
required_sections = [
("## Overview", "Overview section"),
("## Quick Start", "Quick Start section"),
("## Tasks", "Tasks section"),
("## Observation Space", "Observation space documentation"),
("## Action Space", "Action space documentation"),
("## Environment API", "API documentation"),
("## Deployment", "Deployment instructions"),
("## Baseline Scores", "Baseline scores section"),
]
for pattern, description in required_sections:
if pattern in content:
print_success(f"Has {description}")
else:
print_warning(f"Missing {description}")
return True
except Exception as e:
print_error(f"README validation failed: {e}")
return False
def test_docker_build():
"""Test Docker build (optional, may be slow)."""
print_header("Testing Docker Build (Optional)")
if shutil.which("docker") is None:
print_warning("Docker not found, skipping build test")
return True
try:
daemon_check = subprocess.run(
["docker", "info"],
capture_output=True,
text=True,
timeout=30,
)
if daemon_check.returncode != 0:
print_warning("Docker daemon not available, skipping build test")
return True
except Exception as e:
print_warning(f"Could not query Docker daemon, skipping build test: {e}")
return True
print("Attempting Docker build (this may take a few minutes)...")
try:
result = subprocess.run(
["docker", "build", "-t", "code-reviewer-test", "."],
capture_output=True,
text=True,
timeout=300, # 5 minute timeout
)
if result.returncode == 0:
print_success("Docker build succeeded")
return True
else:
print_error("Docker build failed")
print(result.stderr[-500:]) # Last 500 chars
return False
except subprocess.TimeoutExpired:
print_warning("Docker build timed out (may still work on HF Spaces)")
return True
except Exception as e:
print_warning(f"Could not test Docker build: {e}")
return True
def main():
"""Run all validation checks."""
print("\n" + "=" * 60)
print(" CODE REVIEWER ENVIRONMENT - PRE-SUBMISSION VALIDATION")
print("=" * 60)
checks = [
("Required Files", check_required_files),
("openenv.yaml", check_openenv_yaml),
("Dockerfile", check_dockerfile),
("inference.py", check_inference_script),
("Pydantic Models", check_models),
("Environment", check_environment),
("Tasks", check_tasks),
("README.md", check_readme),
]
results = []
for name, check_func in checks:
try:
passed = check_func()
results.append((name, passed))
except Exception as e:
print_error(f"Check '{name}' crashed: {e}")
results.append((name, False))
# Optional Docker test
docker_passed = test_docker_build()
results.append(("Docker Build", docker_passed))
# Summary
print_header("VALIDATION SUMMARY")
passed_count = sum(1 for _, p in results if p)
total_count = len(results)
for name, passed in results:
status = "[PASS]" if passed else "[FAIL]"
print(f" {status}: {name}")
print(f"\n Result: {passed_count}/{total_count} checks passed")
if passed_count == total_count:
print("\n [OK] ALL CHECKS PASSED! Ready to submit!")
print("\n Next steps:")
print(" 1. Push to GitHub")
print(" 2. Deploy to Hugging Face Spaces")
print(" 3. Run inference.py to verify baseline scores")
print(" 4. Submit your entry!")
return 0
else:
print("\n [WARN] SOME CHECKS FAILED")
print(" Please fix the issues above before submitting.")
return 1
if __name__ == "__main__":
sys.exit(main())