blumonilr 5 лет назад
Родитель
Сommit
2ef71e8142
2 измененных файлов с 53 добавлено и 5 удалено
  1. 50 3
      app.py
  2. 3 2
      judge.py

+ 50 - 3
app.py

@@ -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__':

+ 3 - 2
judge.py

@@ -33,8 +33,9 @@ def compile_java(src_path):
 
 
 def compile_python(src_path):
-    if os.system('python -m py_compile %s' % src_path) != 0:
+    if os.system('python3 -m py_compile %s' % src_path) != 0:
         return False
+    os.remove('py_compile')
     return True
 
 
@@ -89,7 +90,7 @@ def judge(src_path, td_path, td_total, time_limit, memory_limit, language):
     elif language == 'python':
         if not compile_python(src_path):
             return {'result': RESULT_STR[7]}
-        args = ['python', 'main.py']
+        args = ['python3', 'main.py']
         src_file = 'main.pyc'
     for i in range(td_total):
         in_path = os.path.join(td_path, '%d.in' % i)