From 0c0d4225caf5f9d53c43a4668269c5036e346589 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Sat, 12 Sep 2015 20:01:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E9=A2=98=E4=B8=AD=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E7=9A=84=E6=AF=94=E8=BE=83=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E7=9A=84=E7=A9=BA=E8=A1=8C=E5=92=8C=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge/judger/client.py | 20 ++++++++++++++++---- problem/views.py | 12 +++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/judge/judger/client.py b/judge/judger/client.py index 899a11f5..77405879 100644 --- a/judge/judger/client.py +++ b/judge/judger/client.py @@ -1,4 +1,5 @@ # coding=utf-8 +import os import json import commands import hashlib @@ -94,7 +95,8 @@ class JudgeClient(object): return error, parse_lrun_output(output) def _compare_output(self, test_case_id): - test_case_md5 = self._test_case_info["test_cases"][str(test_case_id)]["output_md5"] + test_case_config = self._test_case_info["test_cases"][str(test_case_id)] + test_case_md5 = test_case_config["output_md5"] output_path = judger_workspace + str(test_case_id) + ".out" try: @@ -104,6 +106,7 @@ class JudgeClient(object): return False # 计算输出文件的md5 和之前测试用例文件的md5进行比较 + # 现在比较的是完整的文件 md5 = hashlib.md5() while True: data = f.read(2 ** 8) @@ -111,9 +114,18 @@ class JudgeClient(object): break md5.update(data) - # 对比文件是否一致 - # todo 去除最后的空行 - return md5.hexdigest() == test_case_md5 + if md5.hexdigest() == test_case_md5: + return True + else: + # 这时候需要去除用户输出最后的空格和换行 再去比较md5 + # 兼容之前没有striped_output_md5的测试用例 + if "striped_output_md5" not in test_case_config: + return False + f.seek(0) + striped_md5 = hashlib.md5() + # 比较和返回去除空格后的md5比较结果 + striped_md5.update(f.read().rstrip()) + return striped_md5.hexdigest() == test_case_config["striped_output_md5"] def _judge_one(self, test_case_id): # 运行lrun程序 接收返回值 diff --git a/problem/views.py b/problem/views.py index ab15e43d..1e7513d7 100644 --- a/problem/views.py +++ b/problem/views.py @@ -198,16 +198,24 @@ class TestCaseUploadAPIView(APIView): # 计算输出文件的md5 for i in range(len(l) / 2): md5 = hashlib.md5() + striped_md5 = hashlib.md5() f = open(test_case_dir + str(i + 1) + ".out", "r") + # 完整文件的md5 while True: - data = f.read(2 ** 8) + data = f.read() if not data: break md5.update(data) + # 删除标准输出最后的空格和换行 + # 这时只能一次全部读入了,分块读的话,没办法确定文件结尾 + f.seek(0) + striped_md5.update(f.read().rstrip()) + file_info["test_cases"][str(i + 1)] = {"input_name": str(i + 1) + ".in", "output_name": str(i + 1) + ".out", "output_md5": md5.hexdigest(), + "striped_output_md5": striped_md5.hexdigest(), "output_size": os.path.getsize(test_case_dir + str(i + 1) + ".out")} # 写入配置文件 open(test_case_dir + "info", "w").write(json.dumps(file_info)) @@ -239,8 +247,6 @@ def problem_list_page(request, page=1): else: difficulty_order = "difficulty" - - # 按照标签筛选 tag_text = request.GET.get("tag", None) if tag_text: