From cef3386f44a57b2563b1a678cddc5201e1040f46 Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Sat, 5 Sep 2015 18:36:50 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=AF=94=E8=B5=9B?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E9=A1=B5=E9=9D=A2=E8=BF=94=E5=9B=9E=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=E5=88=97=E8=A1=A8=E7=9A=84=E6=8C=89=E9=92=AE[CI=20SKI?= =?UTF-8?q?P]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/src/oj/contest/contest_problem.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/template/src/oj/contest/contest_problem.html b/template/src/oj/contest/contest_problem.html index f5b3cb1..4c16fb3 100644 --- a/template/src/oj/contest/contest_problem.html +++ b/template/src/oj/contest/contest_problem.html @@ -8,6 +8,9 @@
  • 我的提交
  • +
  • 返回题目列表 +
  • {{ contest_problem.title }}

    From ac4277ccd5c69c30a9a44efeee6f4eb70fcb0a80 Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Sat, 5 Sep 2015 18:51:51 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8Durl=E9=94=99=E8=AF=AF,?= =?UTF-8?q?=E5=9B=A0=E4=B8=BA=E6=9C=AC=E5=9C=B0=E6=B2=A1=E6=9C=89=E8=B7=91?= =?UTF-8?q?,=E4=BC=A0=E5=88=B0=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=B8=8A?= =?UTF-8?q?=E6=89=8D=E7=9F=A5=E9=81=93=E7=9A=84[CI=20SKIP]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/src/oj/contest/contest_problem.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/src/oj/contest/contest_problem.html b/template/src/oj/contest/contest_problem.html index 4c16fb3..8f55b10 100644 --- a/template/src/oj/contest/contest_problem.html +++ b/template/src/oj/contest/contest_problem.html @@ -9,7 +9,7 @@ href="/contest/{{ contest_problem.contest.id }}/problem/{{ contest_problem.id }}/submissions/">我的提交
  • 返回题目列表 + href="/contest/{{ contest_problem.contest.id }}/problems/">返回题目列表
  • {{ contest_problem.title }}

    From cbd9e448c51890b2307100507eaf141121a2746e Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Sat, 5 Sep 2015 19:55:51 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=AF=94?= =?UTF-8?q?=E8=B5=9B=E9=A2=98=E7=9B=AE=E5=88=97=E8=A1=A8=E4=B8=AD=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E9=A2=98=E7=9B=AE=E7=8A=B6=E6=80=81=E7=9A=84=E6=A0=87?= =?UTF-8?q?=E8=AF=86(AC/=E6=AD=A3=E5=9C=A8=E8=BF=9B=E8=A1=8C/=E6=B2=A1?= =?UTF-8?q?=E5=BC=80=E5=A7=8B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/views.py | 32 ++++++++++++------- .../src/oj/contest/contest_problems_list.html | 17 +++++++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/contest/views.py b/contest/views.py index a117672..ca93b67 100644 --- a/contest/views.py +++ b/contest/views.py @@ -323,11 +323,24 @@ def contest_problems_list_page(request, contest_id): 比赛所有题目的列表页 """ try: - contest_problems = ContestProblem.objects.filter(contest=Contest.objects.get(id=contest_id)).order_by("sort_index") + contest = Contest.objects.get(id=contest_id) except Contest.DoesNotExist: - return error_page(request, u"比赛题目不存在") + return error_page(request, u"比赛不存在") + + contest_problems = ContestProblem.objects.filter(contest=contest).order_by("sort_index") + submissions = ContestSubmission.objects.filter(user=request.user, contest=contest) + state = {} + for item in submissions: + state[item.problem_id] = item.ac + for item in contest_problems: + if item.id in state: + item.ac = state[item.id].ac + else: + item.ac = 0 + # 右侧的公告列表 announcements = Announcement.objects.filter(is_global=True, visible=True).order_by("-create_time") + return render(request, "oj/contest/contest_problems_list.html", {"contest_problems": contest_problems, "announcements": announcements, "contest": {"id": contest_id}}) @@ -348,7 +361,7 @@ def contest_list_page(request, page=1): # 筛选我能参加的比赛 join = request.GET.get("join", None) if join: - contests = contests.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())).\ + contests = contests.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())). \ filter(end_time__gt=datetime.datetime.now(), start_time__lt=datetime.datetime.now()) paginator = Paginator(contests, 20) @@ -379,7 +392,6 @@ def contest_list_page(request, page=1): "join": join}) - def _cmp(x, y): if x["total_ac"] > y["total_ac"]: return 1 @@ -396,7 +408,8 @@ def _cmp(x, y): def contest_rank_page(request, contest_id): contest = Contest.objects.get(id=contest_id) contest_problems = ContestProblem.objects.filter(contest=contest).order_by("sort_index") - result = ContestSubmission.objects.filter(contest=contest).values("user_id").annotate(total_submit=Sum("total_submission_number")) + result = ContestSubmission.objects.filter(contest=contest).values("user_id").annotate( + total_submit=Sum("total_submission_number")) for i in range(0, len(result)): # 这个人所有的提交 submissions = ContestSubmission.objects.filter(user_id=result[i]["user_id"], contest_id=contest_id) @@ -407,11 +420,6 @@ def contest_rank_page(request, contest_id): result[i]["user"] = User.objects.get(id=result[i]["user_id"]) result[i]["total_time"] = submissions.filter(ac=True).aggregate(total_time=Sum("total_time"))["total_time"] - return render(request, "oj/contest/contest_rank.html", - {"contest": contest, "contest_problems": contest_problems, "result": sorted(result, cmp=_cmp, reverse=True)}) - - - - - + {"contest": contest, "contest_problems": contest_problems, + "result": sorted(result, cmp=_cmp, reverse=True)}) diff --git a/template/src/oj/contest/contest_problems_list.html b/template/src/oj/contest/contest_problems_list.html index 0289592..aae2a2b 100644 --- a/template/src/oj/contest/contest_problems_list.html +++ b/template/src/oj/contest/contest_problems_list.html @@ -40,7 +40,22 @@ {% for item in contest_problems %} - + + + + + {{ item.sort_index }} From 81bd998d8dba0ddd921400034220ce24c7eec9a3 Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Sat, 5 Sep 2015 20:07:31 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=9A=E5=88=9A?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/views.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/contest/views.py b/contest/views.py index ca93b67..2542198 100644 --- a/contest/views.py +++ b/contest/views.py @@ -258,10 +258,10 @@ class ContestPasswordVerifyAPIView(APIView): try: contest = Contest.objects.get(id=data["contest_id"], contest_type=2) except Contest.DoesNotExist: - return error_response(u"密码错误") + return error_response(u"比赛不存在") if data["password"] != contest.password: - return error_response(u" 密码错误") + return error_response(u"密码错误") else: if "contests" not in request.session: request.session["contests"] = [] @@ -279,10 +279,7 @@ def contest_page(request, contest_id): """ 单个比赛的详情页 """ - try: - contest = Contest.objects.get(id=contest_id) - except Contest.DoesNotExist: - return error_page(request, u"比赛不存在") + contest = Contest.objects.get(id=contest_id) return render(request, "oj/contest/contest_index.html", {"contest": contest}) @@ -292,10 +289,7 @@ def contest_problem_page(request, contest_id, contest_problem_id): """ 单个比赛题目的详情页 """ - try: - contest = Contest.objects.get(id=contest_id) - except Contest.DoesNotExist: - return error_page(request, u"比赛不存在") + contest = Contest.objects.get(id=contest_id) try: contest_problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except ContestProblem.DoesNotExist: @@ -334,7 +328,7 @@ def contest_problems_list_page(request, contest_id): state[item.problem_id] = item.ac for item in contest_problems: if item.id in state: - item.ac = state[item.id].ac + item.ac = state[item.id] else: item.ac = 0 From 1f50cd075182f1626698d3399187e91972549685 Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Sat, 5 Sep 2015 20:17:15 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AD=A3=E5=9C=A8?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=9B=BE=E6=A0=87=E4=B8=8D=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98,=E4=BF=AE=E6=94=B9view=E9=87=8Ccont?= =?UTF-8?q?est=5Fproblem=E7=8A=B6=E6=80=81=E7=9A=84=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D,=E4=BD=BF=E6=9B=B4=E7=A1=AE=E5=88=87,=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0dealing-falg=E7=9A=84css?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/views.py | 7 +++++-- static/src/css/oj.css | 4 ++++ template/src/oj/contest/contest_problems_list.html | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/contest/views.py b/contest/views.py index 2542198..eaef55b 100644 --- a/contest/views.py +++ b/contest/views.py @@ -328,9 +328,12 @@ def contest_problems_list_page(request, contest_id): state[item.problem_id] = item.ac for item in contest_problems: if item.id in state: - item.ac = state[item.id] + if state[item.id]: + item.state = 1 + else: + item.state = 2 else: - item.ac = 0 + item.state = 0 # 右侧的公告列表 announcements = Announcement.objects.filter(is_global=True, visible=True).order_by("-create_time") diff --git a/static/src/css/oj.css b/static/src/css/oj.css index e099835..9e9af55 100644 --- a/static/src/css/oj.css +++ b/static/src/css/oj.css @@ -73,6 +73,10 @@ li.list-group-item { color: green; } +.dealing-flag { + color: yellow; +} + .CodeMirror{ min-height: 250px; _height:250px; diff --git a/template/src/oj/contest/contest_problems_list.html b/template/src/oj/contest/contest_problems_list.html index aae2a2b..c7bdec0 100644 --- a/template/src/oj/contest/contest_problems_list.html +++ b/template/src/oj/contest/contest_problems_list.html @@ -43,11 +43,11 @@ Date: Sat, 5 Sep 2015 20:24:11 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=AF=B9=E6=AF=94=E8=B5=9B=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=E5=88=97=E8=A1=A8=E9=87=8C=E7=8A=B6=E6=80=81=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E7=9A=84=E6=A0=B7=E5=BC=8F=E7=9A=84=E5=B0=8F=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/src/css/oj.css | 2 +- template/src/oj/contest/contest_problems_list.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/static/src/css/oj.css b/static/src/css/oj.css index 9e9af55..a5dc892 100644 --- a/static/src/css/oj.css +++ b/static/src/css/oj.css @@ -74,7 +74,7 @@ li.list-group-item { } .dealing-flag { - color: yellow; + color: #FF9933; } .CodeMirror{ diff --git a/template/src/oj/contest/contest_problems_list.html b/template/src/oj/contest/contest_problems_list.html index c7bdec0..5118d3e 100644 --- a/template/src/oj/contest/contest_problems_list.html +++ b/template/src/oj/contest/contest_problems_list.html @@ -47,7 +47,7 @@ {% ifequal item.state 1%} glyphicon-ok ac-flag {% else %} - glyphicon-arrow-right dealing-flag + glyphicon-minus dealing-flag {% endifequal %} {% endif %} From 4d12914e8e21026b19026495b4a8d067d305be2a Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Sat, 5 Sep 2015 20:30:21 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=AA=E8=BE=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/src/oj/contest/contest_problem.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/src/oj/contest/contest_problem.html b/template/src/oj/contest/contest_problem.html index 8f55b10..4cf7e3b 100644 --- a/template/src/oj/contest/contest_problem.html +++ b/template/src/oj/contest/contest_problem.html @@ -9,7 +9,7 @@ href="/contest/{{ contest_problem.contest.id }}/problem/{{ contest_problem.id }}/submissions/">我的提交
  • 返回题目列表 + href="/contest/{{ contest_problem.contest.id }}/problems/">返回
  • {{ contest_problem.title }}

    From 49643c661af28a7e1f8fa32d3189246ead3b25a0 Mon Sep 17 00:00:00 2001 From: "sxw@401" Date: Sat, 5 Sep 2015 21:09:02 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=BD=E6=9D=BE?= =?UTF-8?q?=E5=88=A4=E9=A2=98=E7=9A=84client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge/judger/loose_client.py | 163 +++++++++++++++++++++++++++++++++++ judge/judger/run.py | 9 +- 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 judge/judger/loose_client.py diff --git a/judge/judger/loose_client.py b/judge/judger/loose_client.py new file mode 100644 index 0000000..e3d2540 --- /dev/null +++ b/judge/judger/loose_client.py @@ -0,0 +1,163 @@ +# coding=utf-8 +import json +import commands +import hashlib +from multiprocessing import Pool + +from settings import max_running_number, lrun_gid, lrun_uid, judger_workspace +from language import languages +from result import result +from compiler import compile_ +from judge_exceptions import JudgeClientError, CompileError +from utils import parse_lrun_output + + +# 下面这个函数作为代理访问实例变量,否则Python2会报错,是Python2的已知问题 +# http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-when-using-pythons-multiprocessing-pool-ma/7309686 +def _run(instance, test_case_id): + return instance._judge_one(test_case_id) + + +class JudgeClient(object): + def __init__(self, language_code, exe_path, max_cpu_time, + max_real_time, max_memory, test_case_dir): + """ + :param language_code: 语言编号 + :param exe_path: 可执行文件路径 + :param max_cpu_time: 最大cpu时间,单位ms + :param max_real_time: 最大执行时间,单位ms + :param max_memory: 最大内存,单位MB + :param test_case_dir: 测试用例文件夹路径 + :return:返回结果list + """ + self._language = languages[language_code] + self._exe_path = exe_path + self._max_cpu_time = max_cpu_time + self._max_real_time = max_real_time + self._max_memory = max_memory + self._test_case_dir = test_case_dir + # 进程池 + self._pool = Pool(processes=max_running_number) + + def _generate_command(self, test_case_id): + """ + 设置相关运行限制 进制访问网络 如果启用tmpfs 就把代码输出写入tmpfs,否则写入硬盘 + """ + # todo 系统调用白名单 chroot等参数 + command = "lrun" + \ + " --max-cpu-time " + str(self._max_cpu_time / 1000.0) + \ + " --max-real-time " + str(self._max_real_time / 1000.0 * 2) + \ + " --max-memory " + str(self._max_memory * 1000 * 1000) + \ + " --network false" + \ + " --uid " + str(lrun_uid) + \ + " --gid " + str(lrun_gid) + + execute_command = self._language["execute_command"].format(exe_path=self._exe_path) + + command += (" " + + execute_command + + # 0就是stdin + " 0<" + self._test_case_dir + str(test_case_id) + ".in" + + # 1就是stdout + " 1>" + judger_workspace + str(test_case_id) + ".out" + + # 3是stderr,包含lrun的输出和程序的异常输出 + " 3>&2") + return command + + def _parse_lrun_output(self, output): + # 要注意的是 lrun把结果输出到了stderr,所以有些情况下lrun的输出可能与程序的一些错误输出的混合的,要先分离一下 + error = None + # 倒序找到MEMORY的位置 + output_start = output.rfind("MEMORY") + if output_start == -1: + raise JudgeClientError("Lrun result parse error") + # 如果不是0,说明lrun输出前面有输出,也就是程序的stderr有内容 + if output_start != 0: + error = output[0:output_start] + # 分离出lrun的输出 + output = output[output_start:] + + return error, parse_lrun_output(output) + + def _compare_output(self, test_case_id): + + output_path = judger_workspace + str(test_case_id) + ".out" + + try: + f = open(output_path, "r") + except IOError: + # 文件不存在等引发的异常 返回结果错误 + return False + try: + std = open(self._test_case_dir+test_case_id+".out", "r") + except IOError: + # 文件不存在等引发的异常 返回结果错误 + return False + lines=std.readline() + line_conut = len(lines) + for i in range(0, line_conut-2): + if lines[i] + + + + + + + def _judge_one(self, test_case_id): + # 运行lrun程序 接收返回值 + command = self._generate_command(test_case_id) + status_code, output = commands.getstatusoutput(command) + if status_code: + raise JudgeClientError(output) + error, run_result = self._parse_lrun_output(output) + + run_result["test_case_id"] = test_case_id + + # 如果返回值非0 或者信号量不是0 或者程序的stderr有输出 代表非正常结束 + if run_result["exit_code"] or run_result["term_sig"] or run_result["siginaled"] or error: + run_result["result"] = result["runtime_error"] + return run_result + + # 代表内存或者时间超过限制了 + if run_result["exceed"]: + if run_result["exceed"] == "memory": + run_result["result"] = result["memory_limit_exceeded"] + elif run_result["exceed"] in ["cpu_time", "real_time"]: + run_result["result"] = result["time_limit_exceeded"] + else: + raise JudgeClientError("Error exceeded type: " + run_result["exceed"]) + return run_result + + # 下面就是代码正常运行了 需要判断代码的输出是否正确 + if self._compare_output(test_case_id): + run_result["result"] = result["accepted"] + else: + run_result["result"] = result["wrong_answer"] + + return run_result + + def run(self): + # 添加到任务队列 + _results = [] + results = [] + for i in range(self._test_case_info["test_case_number"]): + _results.append(self._pool.apply_async(_run, (self, i + 1))) + self._pool.close() + self._pool.join() + for item in _results: + # 注意多进程中的异常只有在get()的时候才会被引发 + # http://stackoverflow.com/questions/22094852/how-to-catch-exceptions-in-workers-in-multiprocessing + try: + results.append(item.get()) + except Exception as e: + # todo logging + print e + results.append({"result": result["system_error"]}) + return results + + def __getstate__(self): + # 不同的pool之间进行pickle的时候要排除自己,否则报错 + # http://stackoverflow.com/questions/25382455/python-notimplementederror-pool-objects-cannot-be-passed-between-processes + self_dict = self.__dict__.copy() + del self_dict['_pool'] + return self_dict diff --git a/judge/judger/run.py b/judge/judger/run.py index 6ecc2d3..08e01c8 100644 --- a/judge/judger/run.py +++ b/judge/judger/run.py @@ -2,8 +2,15 @@ import sys import json import MySQLdb +import os + +# 判断判题模式 +judge_model = os.environ.get("judge_model", "default") +if judge_model == "default": + from client import JudgeClient +elif judge_model == "loose": + from loose_client import JudgeClient -from client import JudgeClient from language import languages from compiler import compile_ from result import result