Explorar el Código

feat: 按照commitId命名文件

haha hace 3 años
padre
commit
e143630720
Se han modificado 2 ficheros con 49 adiciones y 4 borrados
  1. 2 1
      web/app.py
  2. 47 3
      web/judgement.py

+ 2 - 1
web/app.py

@@ -48,7 +48,8 @@ def judge():
     src_file = save_code_by_cid(cid, language, code)
     rst = judgement.judge(src_file, 'testcases-'+cid, td_total, time_limit, memory_limit, language)
     os.remove(src_file)
-    shutil.rmtree('testcases')
+    shutil.rmtree('main-'+cid)
+    shutil.rmtree('testcases-'+cid)
     app.logger.info(time.time() - start)
     return rst
 

+ 47 - 3
web/judgement.py

@@ -14,8 +14,8 @@ RESULT_STR = [
 ]
 
 
-def compile_c(src_path):
-    if os.system('gcc %s -o main' % src_path) != 0:
+def compile_c(src_path, out_path):
+    if os.system('gcc %s -o %s' % (src_path, out_path)) != 0:
         return False
     return True
 
@@ -69,13 +69,57 @@ def run(args, in_path, out_path, time_limit, memory_limit):
     return rst
 
 
+def judge_by_cid(src_path, td_path, td_total, time_limit, memory_limit, language, cid):
+    main_dir = 'main-'+cid
+
+    args = []
+    src_file = ''
+    rst_list = []
+    if language == 'c':
+        if not compile_c(src_path, main_dir+'/main'):
+            return {'result_list': [{'result': RESULT_STR[7]}]}
+        args = ['./'+main_dir+'/main']
+        src_file = main_dir+'/main'
+    elif language == 'cpp':
+        if not compile_cpp(src_path):
+            return {'result_list': [{'result': RESULT_STR[7]}]}
+        args = ['./main']
+        src_file = 'main'
+    elif language == 'java':
+        if not compile_java(src_path):
+            return {'result_list': [{'result': RESULT_STR[7]}]}
+        args = ['java', 'Main']
+        src_file = 'Main.class'
+        time_limit *= 3
+    elif language == 'python':
+        if not compile_python(src_path):
+            return {'result_list': [{'result': RESULT_STR[7]}]}
+        args = ['python3', 'main.py']
+        src_file = 'main.pyc'
+    # 遍历每一个测试用例
+    for i in range(td_total):
+        in_path = os.path.join(td_path, '%d.in' % i)
+        out_path = os.path.join(td_path, '%d.out' % i)
+        if os.path.isfile(in_path) and os.path.isfile(out_path):
+            rst = run(args, in_path, out_path, time_limit, memory_limit)
+            rst['result'] = RESULT_STR[rst['result']]
+            rst_list.append(rst)
+        else:
+            os.remove(src_file)
+            rst_list.append({'result': RESULT_STR[8]})
+            return {'result_list': rst_list,
+                    'message': 'test data:%d incompleted' % i}
+    if os.path.exists(src_file):
+        os.remove(src_file)
+    return {'result_list': rst_list}
+
 # 源代码文件,测试用例目录,测试用例个数,时间限制ms,内存限制KB,语言
 def judge(src_path, td_path, td_total, time_limit, memory_limit, language):
     args = []
     src_file = ''
     rst_list = []
     if language == 'c':
-        if not compile_c(src_path):
+        if not compile_c(src_path,''):
             return {'result_list': [{'result': RESULT_STR[7]}]}
         args = ['./main']
         src_file = 'main'