|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import sys |
| 5 | +import subprocess |
| 6 | +import tempfile |
| 7 | +from pathlib import Path |
| 8 | +import argparse |
| 9 | + |
| 10 | +def run_hazel_grader(student_json_str, hazel_path): |
| 11 | + """ |
| 12 | + Run the Hazel grader on student JSON |
| 13 | + Uses: node -r ./src/web/www/polyfill_worker.js _build/default/src/web/gradingReport.bc.js <input> <output> |
| 14 | + """ |
| 15 | + |
| 16 | + # Find the required Hazel files |
| 17 | + polyfill_file = hazel_path / "src/web/www/polyfill_worker.js" |
| 18 | + grading_file = hazel_path / "_build/default/src/web/gradingReport.bc.js" |
| 19 | + |
| 20 | + if not polyfill_file.exists(): |
| 21 | + raise FileNotFoundError(f"Hazel polyfill not found: {polyfill_file}") |
| 22 | + if not grading_file.exists(): |
| 23 | + raise FileNotFoundError(f"Hazel gradingReport.bc.js not found: {grading_file}") |
| 24 | + |
| 25 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 26 | + input_file = Path(tmpdir) / "input.json" |
| 27 | + output_file = Path(tmpdir) / "output.json" |
| 28 | + |
| 29 | + # Write input JSON |
| 30 | + with open(input_file, 'w') as f: |
| 31 | + decoded = json.loads(student_json_str) |
| 32 | + json.dump(decoded, f, indent=2) |
| 33 | + |
| 34 | + # Run Hazel grader |
| 35 | + cmd = [ |
| 36 | + "node", |
| 37 | + "-r", str(polyfill_file), |
| 38 | + str(grading_file), |
| 39 | + str(input_file), |
| 40 | + str(output_file) |
| 41 | + ] |
| 42 | + |
| 43 | + try: |
| 44 | + result = subprocess.run( |
| 45 | + cmd, |
| 46 | + cwd=hazel_path / "_build/default/src/web/www/", |
| 47 | + capture_output=True, |
| 48 | + text=True, |
| 49 | + check=True |
| 50 | + ) |
| 51 | + |
| 52 | + # Read output |
| 53 | + if output_file.exists(): |
| 54 | + with open(output_file, 'r') as f: |
| 55 | + return f.read().strip() |
| 56 | + else: |
| 57 | + return result.stdout.strip() |
| 58 | + |
| 59 | + except subprocess.CalledProcessError as e: |
| 60 | + print(f"[error] Hazel grader failed: {e.stderr}", file=sys.stderr) |
| 61 | + raise |
| 62 | + |
| 63 | + |
| 64 | +def hazel_transform(value, hazel_path): |
| 65 | + """Transform function for Hazel grading""" |
| 66 | + try: |
| 67 | + return run_hazel_grader(value, hazel_path) |
| 68 | + except Exception as e: |
| 69 | + print(f"[error] Hazel processing failed: {e}", file=sys.stderr) |
| 70 | + return "Hazel grader error" |
| 71 | + |
| 72 | + |
| 73 | +def main(): |
| 74 | + parser = argparse.ArgumentParser( |
| 75 | + description="Process a single exercise submission through the Hazel grader", |
| 76 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 77 | + |
| 78 | + parser.add_argument("submission_file", help="Path to submission JSON") |
| 79 | + parser.add_argument("hazel_dir", help="Path to Hazel project directory") |
| 80 | + parser.add_argument("--output", "-o", help="Output file (default: stdout)") |
| 81 | + |
| 82 | + args = parser.parse_args() |
| 83 | + |
| 84 | + submission_path = Path(args.submission_file).resolve() |
| 85 | + hazel_path = Path(args.hazel_dir).resolve() |
| 86 | + |
| 87 | + if not submission_path.exists(): |
| 88 | + print(f"Error: Submission file {submission_path} not found", file=sys.stderr) |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + if not hazel_path.exists(): |
| 92 | + print(f"Error: Hazel directory {hazel_path} not found", file=sys.stderr) |
| 93 | + sys.exit(1) |
| 94 | + |
| 95 | + try: |
| 96 | + with open(submission_path, 'r') as f: |
| 97 | + submission = f.read() |
| 98 | + result = hazel_transform(submission, hazel_path) |
| 99 | + |
| 100 | + # Output result |
| 101 | + json_output = json.dumps(result, indent=2, sort_keys=True) |
| 102 | + |
| 103 | + if args.output: |
| 104 | + with open(args.output, 'w') as f: |
| 105 | + f.write(json_output) |
| 106 | + print(f"Results written to {args.output}", file=sys.stderr) |
| 107 | + else: |
| 108 | + print(json_output) |
| 109 | + |
| 110 | + except Exception as e: |
| 111 | + print(f"Error: {e}", file=sys.stderr) |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments