|
|
@@ -1,4 +1,7 @@
|
|
|
-from flask import Flask
|
|
|
+import os
|
|
|
+import shutil
|
|
|
+
|
|
|
+from flask import Flask, request, json
|
|
|
import judge
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
@@ -8,9 +11,53 @@ def hello_world():
|
|
|
return 'Hello World!'
|
|
|
|
|
|
|
|
|
-@app.route('/api/judge', methods = ['POST'])
|
|
|
+'''
|
|
|
+code: string 源代码
|
|
|
+language: string 编程语言{cpp,java,c,python}
|
|
|
+timeLimit: int 时间限制
|
|
|
+memoryLimit: int 内存限制
|
|
|
+inputOutputMapping: hashmap 输入输出对
|
|
|
+'''
|
|
|
+
|
|
|
+
|
|
|
+@app.route('/api/judge', methods=['POST'])
|
|
|
def judge():
|
|
|
- return {'result': 'System Error'}
|
|
|
+ data = json.loads(request.get_data(as_text=True))
|
|
|
+ code = data['code']
|
|
|
+ language = data['language']
|
|
|
+ time_limit = int(data['timeLimit'])
|
|
|
+ memory_limit = int(data['memoryLimit'])
|
|
|
+ input_output_map = data['inputOutputMapping']
|
|
|
+ td_total = len(input_output_map)
|
|
|
+ save_test_cases(input_output_map, td_total)
|
|
|
+ src_file = save_code(language, code)
|
|
|
+ rst = judge.judge(src_file, 'testcases', td_total, time_limit, memory_limit, language)
|
|
|
+ os.remove(src_file)
|
|
|
+ shutil.rmtree('testcases')
|
|
|
+ return rst
|
|
|
+
|
|
|
+
|
|
|
+def save_test_cases(input_output_map, td_total):
|
|
|
+ os.mkdir('testcases')
|
|
|
+ for i in range(1, td_total+1):
|
|
|
+ in_file = open('testcases/' + str(i) + '.in', 'w')
|
|
|
+ in_file.write(input_output_map.keys[i-1])
|
|
|
+ out_file = open('testcases/' + str(i) + '.out', 'w')
|
|
|
+ out_file.write(input_output_map.values[i-1])
|
|
|
+
|
|
|
+
|
|
|
+def save_code(language, code):
|
|
|
+ src_file = None
|
|
|
+ if language == 'c':
|
|
|
+ src_file = open('main.c', 'w')
|
|
|
+ elif language == 'cpp':
|
|
|
+ src_file = open('main.cpp', 'w')
|
|
|
+ elif language == 'java':
|
|
|
+ src_file = open('Main.java', 'w')
|
|
|
+ elif language == 'python':
|
|
|
+ src_file = open('main.py', 'w')
|
|
|
+ src_file.write(code)
|
|
|
+ return src_file
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|