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
| import os import subprocess import uuid import json from flask import Flask, request, jsonify, send_file from pathlib import Path
app = Flask(__name__)
SUBMISSIONS_PATH = Path("./submissions") PROBLEMS_PATH = Path("./problems")
SUBMISSIONS_PATH.mkdir(parents=True, exist_ok=True)
CODE_TEMPLATE = """ import sys import math import collections import queue import heapq import bisect
def audit_checker(event,args): if not event in ["import","time.sleep","builtins.input","builtins.input/result"]: raise RuntimeError
sys.addaudithook(audit_checker)
"""
class OJTimeLimitExceed(Exception): pass
class OJRuntimeError(Exception): pass
@app.route("/") def index(): return send_file("static/index.html")
@app.route("/source") def source(): return send_file("server.py")
@app.route("/api/problems") def list_problems(): problems_dir = PROBLEMS_PATH problems = [] for problem in problems_dir.iterdir(): problem_config_file = problem / "problem.json" if not problem_config_file.exists(): continue
problem_config = json.load(problem_config_file.open("r")) problem = { "problem_id": problem.name, "name": problem_config["name"], "description": problem_config["description"], } problems.append(problem)
problems = sorted(problems, key=lambda x: x["problem_id"])
problems = {"problems": problems} return jsonify(problems), 200
@app.route("/api/submit", methods=["POST"]) def submit_code(): try: data = request.get_json() code = data.get("code") problem_id = data.get("problem_id")
if code is None or problem_id is None: return ( jsonify({"status": "ER", "message": "Missing 'code' or 'problem_id'"}), 400, )
problem_id = str(int(problem_id)) problem_dir = PROBLEMS_PATH / problem_id if not problem_dir.exists(): return ( jsonify( {"status": "ER", "message": f"Problem ID {problem_id} not found!"} ), 404, )
code_filename = SUBMISSIONS_PATH / f"submission_{uuid.uuid4()}.py" with open(code_filename, "w") as code_file: code = CODE_TEMPLATE + code code_file.write(code)
result = judge(code_filename, problem_dir)
code_filename.unlink()
return jsonify(result)
except Exception as e: return jsonify({"status": "ER", "message": str(e)}), 500
def judge(code_filename, problem_dir): test_files = sorted(problem_dir.glob("*.input")) total_tests = len(test_files) passed_tests = 0
try: for test_file in test_files: input_file = test_file expected_output_file = problem_dir / f"{test_file.stem}.output"
if not expected_output_file.exists(): continue
case_passed = run_code(code_filename, input_file, expected_output_file)
if case_passed: passed_tests += 1
if passed_tests == total_tests: return {"status": "AC", "message": f"Accepted"} else: return { "status": "WA", "message": f"Wrang Answer: pass({passed_tests}/{total_tests})", } except OJRuntimeError as e: return {"status": "RE", "message": f"Runtime Error: ret={e.args[0]}"} except OJTimeLimitExceed: return {"status": "TLE", "message": "Time Limit Exceed"}
def run_code(code_filename, input_file, expected_output_file): with open(input_file, "r") as infile, open( expected_output_file, "r" ) as expected_output: expected_output_content = expected_output.read().strip()
process = subprocess.Popen( ["python3", code_filename], stdin=infile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, )
try: stdout, stderr = process.communicate(timeout=5) except subprocess.TimeoutExpired: process.kill() raise OJTimeLimitExceed
if process.returncode != 0: raise OJRuntimeError(process.returncode)
if stdout.strip() == expected_output_content: return True else: return False
if __name__ == "__main__": app.run(host="0.0.0.0", port=5000)
|