소스 검색

refactor: 新建文件夹并改变工作路径

haha 3 년 전
부모
커밋
98365afc75
2개의 변경된 파일15개의 추가작업 그리고 124개의 파일을 삭제
  1. 12 77
      web/app.py
  2. 3 47
      web/judgement.py

+ 12 - 77
web/app.py

@@ -17,8 +17,17 @@ def hello_world():
     return 'Hello World!'
 
 
-@app.route('/api/judgeTest', methods=['POST'])
-def judge_test():
+'''
+code: string 源代码
+language: string 编程语言{cpp,java,c,python}
+timeLimit: int 时间限制
+memoryLimit: int 内存限制
+inputOutputMapping: hashmap 输入输出对
+'''
+
+
+@app.route('/api/judge', methods=['POST'])
+def judge():
     start = time.time()
     requestData = request.get_data(as_text=True)
     data = json.loads(requestData)
@@ -47,89 +56,15 @@ def judge_test():
     save_test_cases(input_list, output_list, td_total)
     src_file = save_code(language, code)
     rst = judgement.judge(src_file, 'testcases', td_total, time_limit, memory_limit, language)
-
-    # save_test_cases_by_cid(cid, input_list, output_list, td_total)
-    # src_file = save_code_by_cid(cid, language, code)
-    # rst = judgement.judge_by_cid(src_file, 'testcases-'+cid, td_total, time_limit, memory_limit, language,cid)
     os.remove(src_file)
     shutil.rmtree('testcases')
     os.chdir(app_path)
     app.logger.info(os.getcwd())
+    shutil.rmtree(dir_name)
     app.logger.info(time.time() - start)
     return rst
 
 
-'''
-code: string 源代码
-language: string 编程语言{cpp,java,c,python}
-timeLimit: int 时间限制
-memoryLimit: int 内存限制
-inputOutputMapping: hashmap 输入输出对
-'''
-
-
-@app.route('/api/judge', methods=['POST'])
-def judge():
-    start = time.time()
-    requestData = request.get_data(as_text=True)
-    data = json.loads(requestData)
-    code = data['code']
-    language = data['language']
-    cid = str(data['commitId'])
-    time_limit = int(data['timeLimit'])
-    memory_limit = int(data['memoryLimit'])
-    input_list = data['inputList']
-    output_list = data['outputList']
-    if len(input_list) != len(output_list):
-        return {"result_list": [{'result': "System Error"}],
-                'message': 'test data incompleted'}
-    td_total = len(input_list)
-    # save_test_cases(input_list, output_list, td_total)
-    # src_file = save_code(language, code)
-    # rst = judgement.judge(src_file, 'testcases', td_total, time_limit, memory_limit, language)
-
-    save_test_cases_by_cid(cid, input_list, output_list, td_total)
-    src_file = save_code_by_cid(cid, language, code)
-    rst = judgement.judge_by_cid(src_file, 'testcases-'+cid, td_total, time_limit, memory_limit, language,cid)
-    os.remove(src_file)
-    shutil.rmtree('main-'+cid)
-    shutil.rmtree('testcases-'+cid)
-    app.logger.info(time.time() - start)
-    return rst
-
-
-def save_test_cases_by_cid(cid, input_list, output_list, td_total):
-    dir_name = 'testcases-' + cid
-    if os.path.exists(dir_name):
-        shutil.rmtree(dir_name)
-    os.mkdir(dir_name)
-    for i in range(0, td_total):
-        in_file = open(dir_name + '/' + str(i) + '.in', 'w')
-        in_file.write(input_list[i])
-        out_file = open(dir_name + '/' + str(i) + '.out', 'w')
-        out_file.write(output_list[i])
-
-
-def save_code_by_cid(cid, language, code):
-    dir_name = 'main-' + cid
-    if os.path.exists(dir_name):
-        shutil.rmtree(dir_name)
-    os.mkdir(dir_name)
-
-    src_file = None
-    if language == 'c':
-        src_file = open(dir_name+'/'+'main.c', 'w')
-    elif language == 'cpp':
-        src_file = open(dir_name+'/'+'main.cpp', 'w')
-    elif language == 'java':
-        src_file = open(dir_name+'/'+'Main.java', 'w')
-    elif language == 'python':
-        src_file = open(dir_name+'/'+'main.py', 'w')
-    app.logger.info(src_file.name)
-    src_file.write(code)
-    return src_file.name
-
-
 def save_test_cases(input_list, output_list, td_total):
     if os.path.exists('testcases'):
         shutil.rmtree('testcases')

+ 3 - 47
web/judgement.py

@@ -14,8 +14,8 @@ RESULT_STR = [
 ]
 
 
-def compile_c(src_path, out_path):
-    if os.system('gcc %s -o %s' % (src_path, out_path)) != 0:
+def compile_c(src_path):
+    if os.system('gcc %s -o main' % src_path) != 0:
         return False
     return True
 
@@ -69,57 +69,13 @@ 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,'main'):
+        if not compile_c(src_path):
             return {'result_list': [{'result': RESULT_STR[7]}]}
         args = ['./main']
         src_file = 'main'