From d7532ad44642fafffc397fd7f13b4841b747d691 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Mon, 4 Apr 2016 16:43:43 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Special=20Judge?= =?UTF-8?q?=E7=9A=84=E5=9F=BA=E7=A1=80=E9=85=8D=E7=BD=AE=E5=92=8C=E5=88=9B?= =?UTF-8?q?=E5=BB=BASpecial=20Judge=E9=A2=98=E7=9B=AE=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/migrations/0014_auto_20160404_1509.py | 30 +++++++++++++++++++ contest/migrations/0015_auto_20160404_1641.py | 20 +++++++++++++ contest/serializers.py | 6 ++++ problem/migrations/0012_auto_20160404_1509.py | 30 +++++++++++++++++++ problem/migrations/0013_auto_20160404_1641.py | 20 +++++++++++++ problem/models.py | 4 +++ problem/serizalizers.py | 6 ++++ problem/views.py | 3 ++ static/src/js/app/admin/problem/addProblem.js | 14 +++++++-- static/src/js/components/spj.js | 30 +++++++++++++++++++ static/src/js/components/testCaseUploader.js | 2 +- static/src/js/config.js | 1 + template/src/admin/problem/add_problem.html | 2 +- 13 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 contest/migrations/0014_auto_20160404_1509.py create mode 100644 contest/migrations/0015_auto_20160404_1641.py create mode 100644 problem/migrations/0012_auto_20160404_1509.py create mode 100644 problem/migrations/0013_auto_20160404_1641.py create mode 100644 static/src/js/components/spj.js diff --git a/contest/migrations/0014_auto_20160404_1509.py b/contest/migrations/0014_auto_20160404_1509.py new file mode 100644 index 00000000..fc78c36a --- /dev/null +++ b/contest/migrations/0014_auto_20160404_1509.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-04-04 07:09 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0013_auto_20151017_1511'), + ] + + operations = [ + migrations.AddField( + model_name='contestproblem', + name='spj', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='contestproblem', + name='spj_code', + field=models.TextField(blank=True, null=True), + ), + migrations.AddField( + model_name='contestproblem', + name='spj_code_language', + field=models.IntegerField(blank=True, null=True), + ), + ] diff --git a/contest/migrations/0015_auto_20160404_1641.py b/contest/migrations/0015_auto_20160404_1641.py new file mode 100644 index 00000000..5b59ef2b --- /dev/null +++ b/contest/migrations/0015_auto_20160404_1641.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-04-04 08:41 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0014_auto_20160404_1509'), + ] + + operations = [ + migrations.RenameField( + model_name='contestproblem', + old_name='spj_code_language', + new_name='spj_language', + ), + ] diff --git a/contest/serializers.py b/contest/serializers.py index 0ee0def5..317eb76e 100644 --- a/contest/serializers.py +++ b/contest/serializers.py @@ -73,6 +73,9 @@ class CreateContestProblemSerializer(serializers.Serializer): test_case_id = serializers.CharField(max_length=40) time_limit = serializers.IntegerField() memory_limit = serializers.IntegerField() + spj = serializers.BooleanField() + spj_language = serializers.IntegerField(required=False, default=None) + spj_code = serializers.CharField(max_length=10000, required=False, default=None) hint = serializers.CharField(max_length=3000, allow_blank=True) score = serializers.IntegerField(required=False, default=0) sort_index = serializers.CharField(max_length=30) @@ -101,6 +104,9 @@ class EditContestProblemSerializer(serializers.Serializer): test_case_id = serializers.CharField(max_length=40) time_limit = serializers.IntegerField() memory_limit = serializers.IntegerField() + spj = serializers.BooleanField() + spj_language = serializers.IntegerField(required=False, default=None) + spj_code = serializers.CharField(max_length=10000, required=False, default=None) samples = ContestProblemSampleSerializer() hint = serializers.CharField(max_length=3000, allow_blank=True) visible = serializers.BooleanField() diff --git a/problem/migrations/0012_auto_20160404_1509.py b/problem/migrations/0012_auto_20160404_1509.py new file mode 100644 index 00000000..260f8bfb --- /dev/null +++ b/problem/migrations/0012_auto_20160404_1509.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-04-04 07:09 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('problem', '0011_auto_20151017_1227'), + ] + + operations = [ + migrations.AddField( + model_name='problem', + name='spj', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='problem', + name='spj_code', + field=models.TextField(blank=True, null=True), + ), + migrations.AddField( + model_name='problem', + name='spj_code_language', + field=models.IntegerField(blank=True, null=True), + ), + ] diff --git a/problem/migrations/0013_auto_20160404_1641.py b/problem/migrations/0013_auto_20160404_1641.py new file mode 100644 index 00000000..23f9e508 --- /dev/null +++ b/problem/migrations/0013_auto_20160404_1641.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-04-04 08:41 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('problem', '0012_auto_20160404_1509'), + ] + + operations = [ + migrations.RenameField( + model_name='problem', + old_name='spj_code_language', + new_name='spj_language', + ), + ] diff --git a/problem/models.py b/problem/models.py index 5944ca6d..a0491b6c 100644 --- a/problem/models.py +++ b/problem/models.py @@ -38,6 +38,10 @@ class AbstractProblem(models.Model): time_limit = models.IntegerField() # 内存限制 单位是MB memory_limit = models.IntegerField() + # special judge + spj = models.BooleanField(default=False) + spj_language = models.IntegerField(blank=True, null=True) + spj_code = models.TextField(blank=True, null=True) # 是否可见 false的话相当于删除 visible = models.BooleanField(default=True) # 总共提交数量 diff --git a/problem/serizalizers.py b/problem/serizalizers.py index 1b0e9557..1682d3b5 100644 --- a/problem/serizalizers.py +++ b/problem/serizalizers.py @@ -27,6 +27,9 @@ class CreateProblemSerializer(serializers.Serializer): test_case_id = serializers.CharField(max_length=40) time_limit = serializers.IntegerField(min_value=1, max_value=10000) memory_limit = serializers.IntegerField(min_value=16) + spj = serializers.BooleanField() + spj_language = serializers.IntegerField(required=False, default=None) + spj_code = serializers.CharField(max_length=10000, required=False, default=None) difficulty = serializers.IntegerField() tags = serializers.ListField(child=serializers.CharField(max_length=10)) hint = serializers.CharField(max_length=3000, allow_blank=True) @@ -73,6 +76,9 @@ class EditProblemSerializer(serializers.Serializer): source = serializers.CharField(max_length=100) time_limit = serializers.IntegerField(min_value=1) memory_limit = serializers.IntegerField(min_value=1) + spj = serializers.BooleanField() + spj_language = serializers.IntegerField(required=False, default=None) + spj_code = serializers.CharField(max_length=10000, required=False, default=None) difficulty = serializers.IntegerField() tags = serializers.ListField(child=serializers.CharField(max_length=20)) samples = ProblemSampleSerializer() diff --git a/problem/views.py b/problem/views.py index 27f8c705..2ee65648 100644 --- a/problem/views.py +++ b/problem/views.py @@ -91,6 +91,9 @@ class ProblemAdminAPIView(APIView): samples=json.dumps(data["samples"]), time_limit=data["time_limit"], memory_limit=data["memory_limit"], + spj=data["spj"], + spj_language=data["spj_language"], + spj_code=data["spj_code"], difficulty=data["difficulty"], created_by=request.user, hint=data["hint"], diff --git a/static/src/js/app/admin/problem/addProblem.js b/static/src/js/app/admin/problem/addProblem.js index 0e8605eb..e15c116c 100644 --- a/static/src/js/app/admin/problem/addProblem.js +++ b/static/src/js/app/admin/problem/addProblem.js @@ -1,5 +1,5 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", - "csrfToken", "tagEditor", "validator", "jqueryUI", "editorComponent", "testCaseUploader"], + "csrfToken", "tagEditor", "validator", "jqueryUI", "editorComponent", "testCaseUploader", "spj"], function ($, avalon, editor, uploader, bsAlert, csrfTokenHeader) { avalon.ready(function () { @@ -37,6 +37,11 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", bsAlert("请至少添加一个标签,这将有利于用户发现你的题目!"); return false; } + var spjVM = avalon.vmodels.spjConfig; + if (spjVM.spj && spjVM.spjCode == ""){ + bsAlert("请填写Special Judge的代码"); + return false; + } var ajaxData = { id: avalon.vmodels.admin.problemId, title: vm.title, @@ -51,8 +56,13 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", tags: tags, input_description: vm.inputDescription, output_description: vm.outputDescription, - difficulty: vm.difficulty + difficulty: vm.difficulty, + spj: spjVM.spj }; + if (spjVM.spj) { + ajaxData.spj_language = spjVM.spjLanguage; + ajaxData.spj_code = spjVM.spjCode; + } for (var i = 0; i < vm.samples.$model.length; i++) { ajaxData.samples.push({ diff --git a/static/src/js/components/spj.js b/static/src/js/components/spj.js new file mode 100644 index 00000000..07dc1bc8 --- /dev/null +++ b/static/src/js/components/spj.js @@ -0,0 +1,30 @@ +define("spj", ["avalon"], function (avalon) { + avalon.component("ms:spj", { + $template: '
'+ + ''+ + '
'+ + '
'+ + '
'+ + ''+ + '
'+ + ''+ + '
'+ + '
'+ + '
'+ + ''+ + ''+ + '
', + spj: false, + spjLanguage: 1, + spjCode: "", + $ready: function (vm, el) { + el.msRetain = true; + } + }) +}); diff --git a/static/src/js/components/testCaseUploader.js b/static/src/js/components/testCaseUploader.js index 8a41cc35..00ab8f08 100644 --- a/static/src/js/components/testCaseUploader.js +++ b/static/src/js/components/testCaseUploader.js @@ -7,7 +7,7 @@ define("testCaseUploader", ["avalon", "uploader", "bsAlert", "jquery"], function '请将所有测试用例打包在一个文件中上传,' + '所有文件要在压缩包的根目录,' + '且输入输出文件名要以从1开始连续数字标识要对应例如:' + - '
1.in 1.out 2.in 2.out ' + + '
1.in 1.out 2.in 2.out(普通题目)或者1.in 2.in 3.in(Special Judge) ' + '

上传进度%

' + ' ' + ' ' + diff --git a/static/src/js/config.js b/static/src/js/config.js index 69bd9426..6318b242 100644 --- a/static/src/js/config.js +++ b/static/src/js/config.js @@ -28,6 +28,7 @@ var require = { pager: "components/pager", editorComponent: "components/editorComponent", testCaseUploader: "components/testCaseUploader", + spj: "components/spj", // ------ 下面写的都不要直接用,而是使用上面的封装版本 ------ diff --git a/template/src/admin/problem/add_problem.html b/template/src/admin/problem/add_problem.html index 971300e0..396de7fc 100644 --- a/template/src/admin/problem/add_problem.html +++ b/template/src/admin/problem/add_problem.html @@ -13,7 +13,6 @@ -
+
From f69023a0f304c0ac2730b40d2896ec8147dcc639 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Mon, 4 Apr 2016 17:06:42 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E9=A2=98=E7=9B=AEspj=E7=9B=B8=E5=85=B3=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem/views.py | 3 +++ static/src/js/app/admin/problem/addProblem.js | 2 +- .../src/js/app/admin/problem/editProblem.js | 20 +++++++++++++++++-- template/src/admin/problem/edit_problem.html | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/problem/views.py b/problem/views.py index 2ee65648..ffc98682 100644 --- a/problem/views.py +++ b/problem/views.py @@ -128,6 +128,9 @@ class ProblemAdminAPIView(APIView): problem.source = data["source"] problem.time_limit = data["time_limit"] problem.memory_limit = data["memory_limit"] + problem.spj = data["spj"] + problem.spj_language = data["spj_language"] + problem.spj_code = data["spj_code"] problem.difficulty = data["difficulty"] problem.samples = json.dumps(data["samples"]) problem.hint = data["hint"] diff --git a/static/src/js/app/admin/problem/addProblem.js b/static/src/js/app/admin/problem/addProblem.js index e15c116c..d510bf89 100644 --- a/static/src/js/app/admin/problem/addProblem.js +++ b/static/src/js/app/admin/problem/addProblem.js @@ -38,7 +38,7 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", return false; } var spjVM = avalon.vmodels.spjConfig; - if (spjVM.spj && spjVM.spjCode == ""){ + if (spjVM.spj && !spjVM.spjCode){ bsAlert("请填写Special Judge的代码"); return false; } diff --git a/static/src/js/app/admin/problem/editProblem.js b/static/src/js/app/admin/problem/editProblem.js index a8666345..0c134702 100644 --- a/static/src/js/app/admin/problem/editProblem.js +++ b/static/src/js/app/admin/problem/editProblem.js @@ -1,5 +1,5 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", - "csrfToken", "tagEditor", "validator", "jqueryUI", "editorComponent", "testCaseUploader"], + "csrfToken", "tagEditor", "validator", "jqueryUI", "editorComponent", "testCaseUploader", "spj"], function ($, avalon, editor, uploader, bsAlert, csrfTokenHeader) { avalon.ready(function () { @@ -38,6 +38,11 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", bsAlert("请至少添加一个标签,这将有利于用户发现你的题目!"); return false; } + var spjVM = avalon.vmodels.spjConfig; + if (spjVM.spj && !spjVM.spjCode){ + bsAlert("请填写Special Judge的代码"); + return false; + } var ajaxData = { id: avalon.vmodels.admin.problemId, title: vm.title, @@ -52,8 +57,13 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", tags: tags, input_description: vm.inputDescription, output_description: vm.outputDescription, - difficulty: vm.difficulty + difficulty: vm.difficulty, + spj: spjVM.spj }; + if (spjVM.spj) { + ajaxData.spj_language = spjVM.spjLanguage; + ajaxData.spj_code = spjVM.spjCode; + } for (var i = 0; i < vm.samples.$model.length; i++) { ajaxData.samples.push({ @@ -161,6 +171,12 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", vm.inputDescription = problem.input_description; vm.outputDescription = problem.output_description; avalon.vmodels.testCaseUploader.setTestCase(problem.test_case_id); + var spjVM = avalon.vmodels.spjConfig; + spjVM.spj = problem.spj; + // spjLanguage可能是null + spjVM.spjLanguage = problem.spj_language=="2"?"2":"1"; + spjVM.spjCode = problem.spj_code; + vm.source = problem.source; var problemTags = problem.tags; $.ajax({ diff --git a/template/src/admin/problem/edit_problem.html b/template/src/admin/problem/edit_problem.html index 7b5295cd..ab8e5d62 100644 --- a/template/src/admin/problem/edit_problem.html +++ b/template/src/admin/problem/edit_problem.html @@ -105,6 +105,7 @@
+
From db68e925f29c25ead8aff072eff94c0a005bb7e8 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Mon, 4 Apr 2016 17:09:39 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E9=A2=98=E7=9B=AE=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0SPJ=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/src/oj/problem/_problem_header.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/template/src/oj/problem/_problem_header.html b/template/src/oj/problem/_problem_header.html index a2da6c7a..dc809416 100644 --- a/template/src/oj/problem/_problem_header.html +++ b/template/src/oj/problem/_problem_header.html @@ -5,4 +5,7 @@ {% if problem.last_update_time %}最后更新: {{ problem.last_update_time }}  {% endif %} 时间限制: {{ problem.time_limit }}ms   内存限制: {{ problem.memory_limit }}M + {% if problem.spj %} +   SPJ + {% endif %}

\ No newline at end of file From 247d356f7f3d7c877000f87b1243fa4503948357 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Tue, 5 Apr 2016 17:53:04 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E5=AE=8C=E5=96=84SPJ=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E7=9A=84=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem/views.py | 100 ++++++++++++------ static/src/js/app/admin/problem/addProblem.js | 3 +- .../src/js/app/admin/problem/editProblem.js | 3 +- static/src/js/components/spj.js | 51 +++++---- static/src/js/components/testCaseUploader.js | 4 +- 5 files changed, 104 insertions(+), 57 deletions(-) diff --git a/problem/views.py b/problem/views.py index ffc98682..89a6e631 100644 --- a/problem/views.py +++ b/problem/views.py @@ -220,72 +220,106 @@ class TestCaseUploadAPIView(APIView): if len(name_list) == 0: return error_response(u"压缩包内没有文件") - if len(name_list) % 2 == 1: - return error_response(u"测试用例文件格式错误,文件数目为奇数") + for item in name_list: + if not self._is_legal_test_case_file_name(item): + return error_response(u"%s 文件名不符合规范" % item) - for index in range(1, len(name_list) / 2 + 1): - if not (str(index) + ".in" in name_list and str(index) + ".out" in name_list): - return error_response(u"测试用例文件格式错误,缺少" + str(index) + u".in/.out文件") + # 排序,这样name_list就是[1.in, 1.out, 2.in, 2.out]的形式了 + name_list.sort() + + spj = False + + for item in name_list: + # 代表里面有.out文件,所以应该是普通题目的测试用例 + if item.endswith(".out"): + break + else: + # 否则就应该是spj的测试用例 + spj = True + + print name_list, spj + + if not spj: + if len(name_list) % 2 == 1: + return error_response(u"测试用例文件格式错误,文件数目为奇数") + + for index in range(1, len(name_list) / 2 + 1): + if not (str(index) + ".in" in name_list and str(index) + ".out" in name_list): + return error_response(u"测试用例文件格式错误,缺少" + str(index) + u".in/.out文件") + test_case_number = len(name_list) / 2 + else: + for index in range(1, len(name_list) + 1): + if str(index) + ".in" not in name_list: + return error_response(u"测试用例文件格式错误,缺少" + str(index) + u".in文件") + test_case_number = len(name_list) problem_test_dir = rand_str() - test_case_dir = settings.TEST_CASE_DIR + problem_test_dir + "/" + test_case_dir = os.path.join(settings.TEST_CASE_DIR, problem_test_dir) # 得到了合法的测试用例文件列表 然后去解压缩 os.mkdir(test_case_dir) for name in name_list: - f = open(test_case_dir + name, "wb") + f = open(os.path.join(test_case_dir, name), "wb") try: f.write(test_case_file.read(name).replace("\r\n", "\n")) except MemoryError: return error_response(u"单个测试数据体积过大!") finally: f.close() - name_list.sort() - file_info = {"test_case_number": len(name_list) / 2, "test_cases": {}} + file_info = {"test_case_number": test_case_number, "test_cases": {}, "spj": spj} # 计算输出文件的md5 - for i in range(1, len(name_list) / 2 + 1): - md5 = hashlib.md5() - striped_md5 = hashlib.md5() - f = open(test_case_dir + str(i) + ".out", "r") - # 完整文件的md5 - while True: - data = f.read(2 ** 8) - if not data: - break - md5.update(data) + for i in range(1, test_case_number + 1): + if not spj: + md5 = hashlib.md5() + striped_md5 = hashlib.md5() + f = open(os.path.join(test_case_dir, str(i) + ".out"), "r") + # 完整文件的md5 + while True: + data = f.read(2 ** 8) + if not data: + break + md5.update(data) - # 删除标准输出最后的空格和换行 - # 这时只能一次全部读入了,分块读的话,没办法确定文件结尾 - f.seek(0) - striped_md5.update(f.read().rstrip()) + # 删除标准输出最后的空格和换行 + # 这时只能一次全部读入了,分块读的话,没办法确定文件结尾 + f.seek(0) + striped_md5.update(f.read().rstrip()) + + output_md5 = md5.hexdigest() + striped_output_md5 = striped_md5.hexdigest() + output_name = str(i) + ".out" + output_size = os.path.getsize(os.path.join(test_case_dir, output_name)) + else: + output_md5 = striped_output_md5 = output_name = output_size = None file_info["test_cases"][str(i)] = {"input_name": str(i) + ".in", - "output_name": str(i) + ".out", - "output_md5": md5.hexdigest(), - "striped_output_md5": striped_md5.hexdigest(), - "input_size": os.path.getsize(test_case_dir + str(i) + ".in"), - "output_size": os.path.getsize(test_case_dir + str(i) + ".out")} + "output_name": output_name, + "output_md5": output_md5, + "striped_output_md5": striped_output_md5, + "input_size": os.path.getsize(os.path.join(test_case_dir, str(i) + ".in")), + "output_size": output_size} # 写入配置文件 - with open(test_case_dir + "info", "w") as f: - f.write(json.dumps(file_info)) + with open(os.path.join(test_case_dir, "info"), "w") as f: + f.write(json.dumps(file_info)) return success_response({"test_case_id": problem_test_dir, - "file_list": file_info["test_cases"]}) + "file_list": file_info["test_cases"], + "spj": spj}) def get(self, request): test_case_id = request.GET.get("test_case_id", None) if not test_case_id: return error_response(u"参数错误") - test_case_config = settings.TEST_CASE_DIR + test_case_id + "/info" + test_case_config = os.path.join(settings.TEST_CASE_DIR, test_case_id, "info") try: f = open(test_case_config) config = json.loads(f.read()) f.close() except Exception as e: return error_response(u"读取测试用例出错") - return success_response({"file_list": config["test_cases"]}) + return success_response({"file_list": config["test_cases"], "spj": config.get("spj", False)}) def problem_list_page(request, page=1): diff --git a/static/src/js/app/admin/problem/addProblem.js b/static/src/js/app/admin/problem/addProblem.js index d510bf89..00ca52a3 100644 --- a/static/src/js/app/admin/problem/addProblem.js +++ b/static/src/js/app/admin/problem/addProblem.js @@ -109,7 +109,7 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", vm.source = ""; vm.uploadProgress = 0; } - else + else { var vm = avalon.define({ $id: "addProblem", title: "", @@ -153,6 +153,7 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", return "展开"; } }); + } var tagAutoCompleteList = []; diff --git a/static/src/js/app/admin/problem/editProblem.js b/static/src/js/app/admin/problem/editProblem.js index 0c134702..120530ed 100644 --- a/static/src/js/app/admin/problem/editProblem.js +++ b/static/src/js/app/admin/problem/editProblem.js @@ -96,7 +96,7 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", if (avalon.vmodels.editProblem) { var vm = avalon.vmodels.editProblem; } - else + else { var vm = avalon.define({ $id: "editProblem", title: "", @@ -142,6 +142,7 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", avalon.vmodels.admin.template_url = "template/problem/problem_list.html"; } }); + } $.ajax({ url: "/api/admin/problem/?problem_id=" + avalon.vmodels.admin.problemId, diff --git a/static/src/js/components/spj.js b/static/src/js/components/spj.js index 07dc1bc8..19698774 100644 --- a/static/src/js/components/spj.js +++ b/static/src/js/components/spj.js @@ -1,30 +1,39 @@ -define("spj", ["avalon"], function (avalon) { +define("spj", ["avalon", "bsAlert"], function (avalon, bsAlert) { avalon.component("ms:spj", { - $template: '
'+ - ''+ - '
'+ - '
'+ - '
'+ - ''+ - '
'+ - ''+ - '
'+ - '
'+ - '
'+ - ''+ - ''+ - '
', + $template: '
' + + '' + + '
' + + '
' + + '
' + + '' + + '
' + + '' + + '
' + + '
' + + '
' + + '' + + '' + + '
', spj: false, spjLanguage: 1, spjCode: "", + checkboxDisabled: false, + $init: function(vm, el) { + vm.$watch("testCaseUploadFinished", function (spj) { + console.log("watch" + spj); + vm.spj = spj; + vm.checkboxDisabled = true; + }); + }, $ready: function (vm, el) { el.msRetain = true; + } }) }); diff --git a/static/src/js/components/testCaseUploader.js b/static/src/js/components/testCaseUploader.js index 00ab8f08..49d312ec 100644 --- a/static/src/js/components/testCaseUploader.js +++ b/static/src/js/components/testCaseUploader.js @@ -48,6 +48,8 @@ define("testCaseUploader", ["avalon", "uploader", "bsAlert", "jquery"], function } vm.uploaded = true; vm.uploadProgress = 100; + console.log(data.data.spj); + vm.$fire("all!testCaseUploadFinished", data.data.spj); } } }); @@ -72,7 +74,7 @@ define("testCaseUploader", ["avalon", "uploader", "bsAlert", "jquery"], function output: response.data.file_list[key].output_name }) } - bsAlert("测试数据添加成功!共添加" + vm.testCaseList.length + "组测试数据"); + vm.$fire("all!testCaseUploadFinished", response.data.spj); } }, function (file, percentage) { From 7fef9ba6cbd8ab3c3c706810204f56a8cdd2a316 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Tue, 5 Apr 2016 18:43:24 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=AF=94=E8=B5=9B?= =?UTF-8?q?=E9=A2=98=E7=9B=AESPJ=E7=9A=84=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/views.py | 6 ++++++ .../src/js/app/admin/contest/editProblem.js | 19 +++++++++++++++++-- static/src/js/components/spj.js | 1 - static/src/js/components/testCaseUploader.js | 1 - template/src/admin/contest/edit_problem.html | 1 + 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/contest/views.py b/contest/views.py index 53075b01..82fc692f 100644 --- a/contest/views.py +++ b/contest/views.py @@ -202,6 +202,9 @@ class ContestProblemAdminAPIView(APIView): samples=json.dumps(data["samples"]), time_limit=data["time_limit"], memory_limit=data["memory_limit"], + spj=data["spj"], + spj_language=data["spj_language"], + spj_code=data["spj_code"], created_by=request.user, hint=data["hint"], contest=contest, @@ -235,6 +238,9 @@ class ContestProblemAdminAPIView(APIView): contest_problem.test_case_id = data["test_case_id"] contest_problem.time_limit = data["time_limit"] contest_problem.memory_limit = data["memory_limit"] + contest_problem.spj = data["spj"] + contest_problem.spj_language = data["spj_language"] + contest_problem.spj_code = data["spj_code"] contest_problem.samples = json.dumps(data["samples"]) contest_problem.hint = data["hint"] contest_problem.visible = data["visible"] diff --git a/static/src/js/app/admin/contest/editProblem.js b/static/src/js/app/admin/contest/editProblem.js index d2d2bbe5..38626bf8 100644 --- a/static/src/js/app/admin/contest/editProblem.js +++ b/static/src/js/app/admin/contest/editProblem.js @@ -1,5 +1,5 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", - "csrfToken", "tagEditor", "validator", "editorComponent", "testCaseUploader"], + "csrfToken", "tagEditor", "validator", "editorComponent", "testCaseUploader", "spj"], function ($, avalon, editor, uploader, bsAlert, csrfTokenHeader) { avalon.ready(function () { @@ -34,6 +34,11 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", return false; } } + var spjVM = avalon.vmodels.spjConfig; + if (spjVM.spj && !spjVM.spjCode){ + bsAlert("请填写Special Judge的代码"); + return false; + } var ajaxData = { title: vm.title, description: avalon.vmodels.contestProblemDescriptionEditor.content, @@ -46,8 +51,13 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", contest_id: avalon.vmodels.admin.contestId, input_description: vm.inputDescription, output_description: vm.outputDescription, - sort_index: vm.sortIndex + sort_index: vm.sortIndex, + spj: spjVM.spj }; + if (spjVM.spj) { + ajaxData.spj_language = spjVM.spjLanguage; + ajaxData.spj_code = spjVM.spjCode; + } if (avalon.vmodels.admin.contestProblemStatus == "edit") { var method = "put"; @@ -185,6 +195,11 @@ require(["jquery", "avalon", "editor", "uploader", "bsAlert", }) } avalon.vmodels.contestProblemHintEditor.content = problem.hint; + var spjVM = avalon.vmodels.spjConfig; + spjVM.spj = problem.spj; + // spjLanguage可能是null + spjVM.spjLanguage = problem.spj_language=="2"?"2":"1"; + spjVM.spjCode = problem.spj_code; } } }); diff --git a/static/src/js/components/spj.js b/static/src/js/components/spj.js index 19698774..33b90a05 100644 --- a/static/src/js/components/spj.js +++ b/static/src/js/components/spj.js @@ -26,7 +26,6 @@ define("spj", ["avalon", "bsAlert"], function (avalon, bsAlert) { checkboxDisabled: false, $init: function(vm, el) { vm.$watch("testCaseUploadFinished", function (spj) { - console.log("watch" + spj); vm.spj = spj; vm.checkboxDisabled = true; }); diff --git a/static/src/js/components/testCaseUploader.js b/static/src/js/components/testCaseUploader.js index 49d312ec..40e82e59 100644 --- a/static/src/js/components/testCaseUploader.js +++ b/static/src/js/components/testCaseUploader.js @@ -48,7 +48,6 @@ define("testCaseUploader", ["avalon", "uploader", "bsAlert", "jquery"], function } vm.uploaded = true; vm.uploadProgress = 100; - console.log(data.data.spj); vm.$fire("all!testCaseUploadFinished", data.data.spj); } } diff --git a/template/src/admin/contest/edit_problem.html b/template/src/admin/contest/edit_problem.html index a9289497..313d60fd 100644 --- a/template/src/admin/contest/edit_problem.html +++ b/template/src/admin/contest/edit_problem.html @@ -96,6 +96,7 @@
+
From 7414c80ee6c3737e914c407f02dc1a530f235989 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Tue, 5 Apr 2016 20:19:37 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0admin=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?SPJ=E7=9A=84=E5=B8=AE=E5=8A=A9=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/src/js/components/spj.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/src/js/components/spj.js b/static/src/js/components/spj.js index 33b90a05..3db646d5 100644 --- a/static/src/js/components/spj.js +++ b/static/src/js/components/spj.js @@ -5,7 +5,7 @@ define("spj", ["avalon", "bsAlert"], function (avalon, bsAlert) { '
' + '
' + '
' + '' + From 3d396cc8e47452be2deac8291b88a79dd8e41e94 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 6 Apr 2016 13:49:37 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E7=89=88=E6=9C=AC=E5=8F=B7=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8Ejudger=E9=87=8D=E6=96=B0=E7=BC=96=E8=AF=91spj=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0016_contestproblem_spj_version.py | 20 +++++++++++++++++++ contest/views.py | 11 +++++++++- .../migrations/0014_problem_spj_version.py | 20 +++++++++++++++++++ problem/models.py | 1 + problem/views.py | 12 +++++++++-- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 contest/migrations/0016_contestproblem_spj_version.py create mode 100644 problem/migrations/0014_problem_spj_version.py diff --git a/contest/migrations/0016_contestproblem_spj_version.py b/contest/migrations/0016_contestproblem_spj_version.py new file mode 100644 index 00000000..749654e5 --- /dev/null +++ b/contest/migrations/0016_contestproblem_spj_version.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-04-06 04:20 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0015_auto_20160404_1641'), + ] + + operations = [ + migrations.AddField( + model_name='contestproblem', + name='spj_version', + field=models.CharField(blank=True, max_length=32, null=True), + ), + ] diff --git a/contest/views.py b/contest/views.py index 82fc692f..976530c0 100644 --- a/contest/views.py +++ b/contest/views.py @@ -1,7 +1,8 @@ # coding=utf-8 import json +import os import datetime -import redis +import hashlib from django.shortcuts import render from django.db import IntegrityError @@ -176,6 +177,11 @@ class ContestAdminAPIView(APIView): class ContestProblemAdminAPIView(APIView): + def _spj_version(self, code): + if code is None: + return None + return hashlib.md5(code.encode("utf-8")).hexdigest() + def post(self, request): """ 比赛题目发布json api接口 @@ -205,6 +211,7 @@ class ContestProblemAdminAPIView(APIView): spj=data["spj"], spj_language=data["spj_language"], spj_code=data["spj_code"], + spj_version=self._spj_version(data["spj_code"]), created_by=request.user, hint=data["hint"], contest=contest, @@ -228,6 +235,7 @@ class ContestProblemAdminAPIView(APIView): contest_problem = ContestProblem.objects.get(id=data["id"]) except ContestProblem.DoesNotExist: return error_response(u"该比赛题目不存在!") + contest = Contest.objects.get(id=contest_problem.contest_id) if request.user.admin_type != SUPER_ADMIN and contest.created_by != request.user: return error_response(u"比赛不存在") @@ -241,6 +249,7 @@ class ContestProblemAdminAPIView(APIView): contest_problem.spj = data["spj"] contest_problem.spj_language = data["spj_language"] contest_problem.spj_code = data["spj_code"] + contest_problem.spj_version = self._spj_version(data["spj_code"]) contest_problem.samples = json.dumps(data["samples"]) contest_problem.hint = data["hint"] contest_problem.visible = data["visible"] diff --git a/problem/migrations/0014_problem_spj_version.py b/problem/migrations/0014_problem_spj_version.py new file mode 100644 index 00000000..92bf6a46 --- /dev/null +++ b/problem/migrations/0014_problem_spj_version.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-04-06 04:20 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('problem', '0013_auto_20160404_1641'), + ] + + operations = [ + migrations.AddField( + model_name='problem', + name='spj_version', + field=models.CharField(blank=True, max_length=32, null=True), + ), + ] diff --git a/problem/models.py b/problem/models.py index a0491b6c..008b50a9 100644 --- a/problem/models.py +++ b/problem/models.py @@ -42,6 +42,7 @@ class AbstractProblem(models.Model): spj = models.BooleanField(default=False) spj_language = models.IntegerField(blank=True, null=True) spj_code = models.TextField(blank=True, null=True) + spj_version = models.CharField(max_length=32, blank=True, null=True) # 是否可见 false的话相当于删除 visible = models.BooleanField(default=True) # 总共提交数量 diff --git a/problem/views.py b/problem/views.py index 89a6e631..005bfdf3 100644 --- a/problem/views.py +++ b/problem/views.py @@ -66,6 +66,11 @@ class ProblemTagAdminAPIView(APIView): class ProblemAdminAPIView(APIView): + def _spj_version(self, code): + if code is None: + return None + return hashlib.md5(code.encode("utf-8")).hexdigest() + @super_admin_required def post(self, request): """ @@ -94,6 +99,7 @@ class ProblemAdminAPIView(APIView): spj=data["spj"], spj_language=data["spj_language"], spj_code=data["spj_code"], + spj_version=self._spj_version(data["spj_code"]), difficulty=data["difficulty"], created_by=request.user, hint=data["hint"], @@ -120,6 +126,7 @@ class ProblemAdminAPIView(APIView): if serializer.is_valid(): data = serializer.data problem = Problem.objects.get(id=data["id"]) + problem.title = data["title"] problem.description = data["description"] problem.input_description = data["input_description"] @@ -131,6 +138,7 @@ class ProblemAdminAPIView(APIView): problem.spj = data["spj"] problem.spj_language = data["spj_language"] problem.spj_code = data["spj_code"] + problem.spj_version = self._spj_version(data["spj_code"]) problem.difficulty = data["difficulty"] problem.samples = json.dumps(data["samples"]) problem.hint = data["hint"] @@ -146,7 +154,9 @@ class ProblemAdminAPIView(APIView): except ProblemTag.DoesNotExist: tag = ProblemTag.objects.create(name=tag) problem.tags.add(tag) + problem.save() + return success_response(ProblemSerializer(problem).data) else: return serializer_invalid_response(serializer) @@ -237,8 +247,6 @@ class TestCaseUploadAPIView(APIView): # 否则就应该是spj的测试用例 spj = True - print name_list, spj - if not spj: if len(name_list) % 2 == 1: return error_response(u"测试用例文件格式错误,文件数目为奇数") From b3116cc43060a27532a75d6d7621554eb4fa759d Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 6 Apr 2016 20:33:19 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AF=B9SPJ=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xml rpc不能使用None --- judge/client.py | 41 +++++++++++++++++++++++++++---------- judge/compiler.py | 5 +++-- judge/language.py | 2 ++ judge/runner.py | 34 ++++++++++++++++++++++++++---- judge/settings.py | 7 ------- judge/spj_client.py | 26 +++++++++++++++++++++++ judge_dispatcher/models.py | 4 ++++ judge_dispatcher/tasks.py | 42 +++++++++++++++++++++++++++----------- static/src/js/build.js | 1 + submission/tasks.py | 4 ++-- submission/views.py | 9 +++++--- 11 files changed, 134 insertions(+), 41 deletions(-) create mode 100644 judge/spj_client.py diff --git a/judge/client.py b/judge/client.py index 4d5cda87..9dc0129f 100644 --- a/judge/client.py +++ b/judge/client.py @@ -1,12 +1,13 @@ # coding=utf-8 import os import json -import commands import hashlib import judger +import spj_client + from multiprocessing import Pool -from settings import max_running_number, lrun_gid, lrun_uid, judger_workspace +from settings import max_running_number from language import languages from result import result from judge_exceptions import JudgeClientError @@ -20,7 +21,7 @@ def _run(instance, test_case_id): class JudgeClient(object): - def __init__(self, language_code, exe_path, max_cpu_time, max_memory, test_case_dir, judge_base_path): + def __init__(self, language_code, exe_path, max_cpu_time, max_memory, test_case_dir, judge_base_path, spj_path): """ :param language_code: 语言编号 :param exe_path: 可执行文件路径 @@ -53,11 +54,12 @@ class JudgeClient(object): # 测试用例配置项 self._test_case_info = self._load_test_case_info() self._judge_base_path = judge_base_path + self._spj_path = spj_path def _load_test_case_info(self): # 读取测试用例信息 转换为dict try: - f = open(self._test_case_dir + "info") + f = open(os.path.join(self._test_case_dir, "info")) return json.loads(f.read()) except IOError: raise JudgeClientError("Test case config file not found") @@ -96,11 +98,13 @@ class JudgeClient(object): return output_md5, output_md5 == test_case_config["striped_output_md5"] def _judge_one(self, test_case_id): + in_file = os.path.join(self._test_case_dir, str(test_case_id) + ".in") + out_file = os.path.join(self._judge_base_path, str(test_case_id) + ".out") run_result = judger.run(path=self.execute_command[0], max_cpu_time=self._max_cpu_time, max_memory=self._max_memory, - in_file=os.path.join(self._test_case_dir, str(test_case_id) + ".in"), - out_file=os.path.join(self._judge_base_path, str(test_case_id) + ".out"), + in_file=in_file, + out_file=out_file, args=self.execute_command[1:], env=["PATH=" + os.environ["PATH"]], use_sandbox=self._language["use_sandbox"], @@ -113,12 +117,27 @@ class JudgeClient(object): # 将judger返回的结果标志转换为本系统中使用的 if run_result["flag"] == 0: - output_md5, r = self._compare_output(test_case_id) - if r: - run_result["result"] = result["accepted"] + if self._spj_path is None: + output_md5, r = self._compare_output(test_case_id) + if r: + run_result["result"] = result["accepted"] + else: + run_result["result"] = result["wrong_answer"] + run_result["output_md5"] = output_md5 else: - run_result["result"] = result["wrong_answer"] - run_result["output_md5"] = output_md5 + spj_result = spj_client.spj(path=self._spj_path, max_cpu_time=3 * self._max_cpu_time, + max_memory=3 * self._max_memory, + in_path=in_file, + user_out_path=out_file) + if spj_result["spj_result"] == spj_client.AC: + run_result["result"] = result["accepted"] + elif spj_result["spj_result"] == spj_client.WA: + run_result["result"] = result["wrong_answer"] + else: + run_result["result"] = result["system_error"] + run_result["error"] = "SPJ Crashed, return: %d, signal: %d" % \ + (spj_result["spj_result"], spj_result["signal"]) + elif run_result["flag"] in [1, 2]: run_result["result"] = result["time_limit_exceeded"] elif run_result["flag"] == 3: diff --git a/judge/compiler.py b/judge/compiler.py index f4fc9551..0e952e5c 100644 --- a/judge/compiler.py +++ b/judge/compiler.py @@ -7,8 +7,9 @@ from logger import logger from settings import judger_workspace -def compile_(language_item, src_path, exe_path, judge_base_path): - compile_command = language_item["compile_command"].format(src_path=src_path, exe_path=exe_path).split(" ") +def compile_(language_item, src_path, exe_path, judge_base_path, compile_spj=False): + command_item = "spj_compile_command" if compile_spj else "compile_command" + compile_command = language_item[command_item].format(src_path=src_path, exe_path=exe_path).split(" ") compiler = compile_command[0] compile_args = compile_command[1:] compiler_output_file = os.path.join(judge_base_path, "compiler.out") diff --git a/judge/language.py b/judge/language.py index 044e8efe..3bd4b56f 100644 --- a/judge/language.py +++ b/judge/language.py @@ -7,6 +7,7 @@ languages = { "src_name": "main.c", "code": 1, "compile_command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}/main", + "spj_compile_command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}", "execute_command": "{exe_path}/main", "use_sandbox": True }, @@ -15,6 +16,7 @@ languages = { "src_name": "main.cpp", "code": 2, "compile_command": "/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}/main", + "spj_compile_command": "/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}", "execute_command": "{exe_path}/main", "use_sandbox": True }, diff --git a/judge/runner.py b/judge/runner.py index e023e87a..b7874928 100644 --- a/judge/runner.py +++ b/judge/runner.py @@ -12,7 +12,8 @@ from settings import judger_workspace class JudgeInstanceRunner(object): - def run(self, token, submission_id, language_code, code, time_limit, memory_limit, test_case_id): + def run(self, token, submission_id, language_code, code, time_limit, memory_limit, test_case_id, + spj, spj_language, spj_code, spj_version): language = languages[language_code] host_name = socket.gethostname() judge_base_path = os.path.join(judger_workspace, "run", submission_id) @@ -35,25 +36,50 @@ class JudgeInstanceRunner(object): # 编译 try: - exe_path = compile_(language, src_path, judge_base_path, judge_base_path) + exe_path = compile_(language_item=language, src_path=src_path, + exe_path=judge_base_path, judge_base_path=judge_base_path, compile_spj=False) except Exception as e: shutil.rmtree(judge_base_path, ignore_errors=True) return {"code": 1, "data": {"error": str(e), "server": host_name}} + test_case_dir = os.path.join(judger_workspace, "test_case", test_case_id) + + # SPJ相关 + if spj: + spj_path = os.path.join(test_case_dir, "spj-" + spj_version) + if "spj-" + spj_version not in os.listdir(test_case_dir): + spj_language_item = languages[spj_language] + spj_code_path = os.path.join(test_case_dir, "spj-" + spj_language_item["src_name"]) + + f = open(spj_code_path, "w") + f.write(spj_code.encode("utf8")) + f.close() + + try: + compile_(language_item=languages[spj_language], src_path=spj_code_path, + exe_path=spj_path, + judge_base_path=judge_base_path, compile_spj=True) + except Exception as e: + return {"code": 2, "data": {"error": "SPJ Compile error: " + str(e), "server": host_name}} + else: + spj_path = None + # 运行 try: client = JudgeClient(language_code=language_code, exe_path=exe_path, max_cpu_time=int(time_limit), max_memory=int(memory_limit) * 1024 * 1024, - test_case_dir=judger_workspace + "test_case/" + test_case_id + "/", - judge_base_path=judge_base_path) + test_case_dir=test_case_dir, + judge_base_path=judge_base_path, spj_path=spj_path) judge_result = {"result": result["accepted"], "info": client.run(), "accepted_answer_time": None, "server": host_name} for item in judge_result["info"]: if item["result"] != 0: judge_result["result"] = item["result"] + if item.get("error"): + judge_result["info"] = item["error"] break else: l = sorted(judge_result["info"], key=lambda k: k["cpu_time"]) diff --git a/judge/settings.py b/judge/settings.py index 5d9fa432..3dd1268e 100644 --- a/judge/settings.py +++ b/judge/settings.py @@ -1,16 +1,9 @@ # coding=utf-8 -import os # 单个判题端最多同时运行的程序个数,因为判题端会同时运行多组测试数据,比如一共有5组测试数据 # 如果MAX_RUNNING_NUMBER大于等于5,那么这5组数据就会同时进行评测,然后返回结果。 # 如果MAX_RUNNING_NUMBER小于5,为3,那么就会同时运行前三组测试数据,然后再运行后两组数据 # 这样可以避免同时运行的程序过多导致的cpu占用太高 max_running_number = 10 -# lrun运行用户的uid -lrun_uid = 1001 - -# lrun用户组gid -lrun_gid = 1002 - # judger工作目录 judger_workspace = "/var/judger/" diff --git a/judge/spj_client.py b/judge/spj_client.py new file mode 100644 index 00000000..35adb05a --- /dev/null +++ b/judge/spj_client.py @@ -0,0 +1,26 @@ +# coding=utf-8 +import os +import judger + +WA = 1 +AC = 0 +SPJ_ERROR = -1 + + +def file_exists(path): + return os.path.exists(path) + + +def spj(path, max_cpu_time, max_memory, in_path, user_out_path): + if file_exists(in_path) and file_exists(user_out_path): + result = judger.run(path=path, in_file="/dev/null", out_file="/dev/null", + max_cpu_time=max_cpu_time, max_memory=max_memory, + args=[in_path, user_out_path], env=["PATH=" + os.environ.get("PATH", "")], + use_sandbox=True, use_nobody=True) + if result["signal"] == 0 and result["exit_status"] in [AC, WA, SPJ_ERROR]: + result["spj_result"] = result["exit_status"] + else: + result["spj_result"] = SPJ_ERROR + return result + else: + raise ValueError("in_path or user_out_path does not exist") \ No newline at end of file diff --git a/judge_dispatcher/models.py b/judge_dispatcher/models.py index 3bac9474..15cfa2c8 100644 --- a/judge_dispatcher/models.py +++ b/judge_dispatcher/models.py @@ -41,6 +41,10 @@ class JudgeWaitingQueue(models.Model): memory_limit = models.IntegerField() test_case_id = models.CharField(max_length=40) create_time = models.DateTimeField(auto_now_add=True) + spj = models.BooleanField(default=False) + spj_language = models.IntegerField(blank=True, null=True) + spj_code = models.TextField(blank=True, null=True) + spj_version = models.CharField(max_length=32, blank=True, null=True) class Meta: db_table = "judge_waiting_queue" diff --git a/judge_dispatcher/tasks.py b/judge_dispatcher/tasks.py index 8bdf53fd..f9d016fb 100644 --- a/judge_dispatcher/tasks.py +++ b/judge_dispatcher/tasks.py @@ -20,12 +20,23 @@ logger = logging.getLogger("app_info") class JudgeDispatcher(object): - def __init__(self, submission, time_limit, memory_limit, test_case_id): - self.submission = submission + def _none_to_false(self, value): + # xml rpc不能使用None + if value is None: + return False + else: + return value + + def __init__(self, submission_id, time_limit, memory_limit, test_case_id, spj, spj_language, spj_code, spj_version): + self.submission = Submission.objects.get(id=submission_id) self.time_limit = time_limit self.memory_limit = memory_limit self.test_case_id = test_case_id - self.user = User.objects.get(id=submission.user_id) + self.user = User.objects.get(id=self.submission.user_id) + self.spj = spj + self.spj_language = spj_language + self.spj_code = spj_code + self.spj_version = spj_version def choose_judge_server(self): servers = JudgeServer.objects.filter(workload__lt=100, lock=False, status=True).order_by("-workload") @@ -41,16 +52,21 @@ class JudgeDispatcher(object): # 如果没有合适的判题服务器,就放入等待队列中等待判题 if not judge_server: JudgeWaitingQueue.objects.create(submission_id=self.submission.id, time_limit=self.time_limit, - memory_limit=self.memory_limit, test_case_id=self.test_case_id) + memory_limit=self.memory_limit, test_case_id=self.test_case_id, + spj=self.spj, spj_language=self.spj_language, spj_code=self.spj_code, + spj_version=self.spj_version) return judge_server.use_judge_instance() try: - s = TimeoutServerProxy("http://" + judge_server.ip + ":" + str(judge_server.port), timeout=20) + s = TimeoutServerProxy("http://" + judge_server.ip + ":" + str(judge_server.port), + timeout=30) data = s.run(judge_server.token, self.submission.id, self.submission.language, - self.submission.code, self.time_limit, self.memory_limit, self.test_case_id) + self.submission.code, self.time_limit, self.memory_limit, self.test_case_id, + self.spj, self._none_to_false(self.spj_language), + self._none_to_false(self.spj_code), self._none_to_false(self.spj_version)) # 编译错误 if data["code"] == 1: self.submission.result = result["compile_error"] @@ -71,7 +87,7 @@ class JudgeDispatcher(object): judge_server.release_judge_instance() self.submission.judge_end_time = int(time.time() * 1000) - self.submission.save() + self.submission.save(update_fields=["judge_start_time", "result", "info", "accepted_answer_time", "judge_end_time"]) if self.submission.contest_id: self.update_contest_problem_status() @@ -85,13 +101,15 @@ class JudgeDispatcher(object): from submission.tasks import _judge waiting_submission = waiting_submissions.first() - - submission = Submission.objects.get(id=waiting_submission.submission_id) waiting_submission.delete() - - _judge.delay(submission=submission, time_limit=waiting_submission.time_limit, + _judge.delay(submission_id=waiting_submission.submission_id, + time_limit=waiting_submission.time_limit, memory_limit=waiting_submission.memory_limit, - test_case_id=waiting_submission.test_case_id) + test_case_id=waiting_submission.test_case_id, + spj=waiting_submission.spj, + spj_language=waiting_submission.spj_language, + spj_code=waiting_submission.spj_code, + spj_version=waiting_submission.spj_version) def update_problem_status(self): problem = Problem.objects.get(id=self.submission.problem_id) diff --git a/static/src/js/build.js b/static/src/js/build.js index 21f10c84..1bd9953b 100644 --- a/static/src/js/build.js +++ b/static/src/js/build.js @@ -27,6 +27,7 @@ pager: "components/pager", editorComponent: "components/editorComponent", testCaseUploader: "components/testCaseUploader", + spj: "components/spj", // ------ 下面写的都不要直接用,而是使用上面的封装版本 ------ //富文本编辑器simditor -> editor diff --git a/submission/tasks.py b/submission/tasks.py index 4267841a..34ab63d1 100644 --- a/submission/tasks.py +++ b/submission/tasks.py @@ -5,5 +5,5 @@ from judge_dispatcher.tasks import JudgeDispatcher @shared_task -def _judge(submission, time_limit, memory_limit, test_case_id): - JudgeDispatcher(submission, time_limit, memory_limit, test_case_id).judge() \ No newline at end of file +def _judge(submission_id, time_limit, memory_limit, test_case_id, spj=False, spj_language=None, spj_code=None, spj_version=None): + JudgeDispatcher(submission_id, time_limit, memory_limit, test_case_id, spj, spj_language, spj_code, spj_version).judge() \ No newline at end of file diff --git a/submission/views.py b/submission/views.py index cf14c384..94d62739 100644 --- a/submission/views.py +++ b/submission/views.py @@ -53,7 +53,8 @@ def _submit_code(user, problem_id, language, code): problem_id=problem.id) try: - _judge.delay(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) + _judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id, + problem.spj, problem.spj_language, problem.spj_code, problem.spj_version) except Exception as e: logger.error(e) return error_response(u"提交判题任务失败") @@ -147,7 +148,8 @@ class ContestSubmissionAPIView(APIView): code=data["code"], problem_id=problem.id) try: - _judge.delay(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) + _judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id, + problem.spj. problem.spj_language, problem.spj_code, problem.spj_version) except Exception as e: logger.error(e) return error_response(u"提交判题任务失败") @@ -332,7 +334,8 @@ class SubmissionRejudgeAdminAPIView(APIView): except Problem.DoesNotExist: return error_response(u"题目不存在") try: - _judge.delay(submission, problem.time_limit, problem.memory_limit, problem.test_case_id) + _judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id, + problem.spj. problem.spj_language, problem.spj_code, problem.spj_version) except Exception as e: logger.error(e) return error_response(u"提交判题任务失败") From 90801f7f7e1654349b1ed67e8af01bf84f058efe Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 7 Apr 2016 16:19:39 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86logge?= =?UTF-8?q?r=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge/compiler.py | 4 +--- judge/runner.py | 5 +++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/judge/compiler.py b/judge/compiler.py index 0e952e5c..6b5f8dd5 100644 --- a/judge/compiler.py +++ b/judge/compiler.py @@ -1,10 +1,8 @@ # coding=utf-8 -import time import os import judger -from judge_exceptions import CompileError, JudgeClientError +from judge_exceptions import CompileError from logger import logger -from settings import judger_workspace def compile_(language_item, src_path, exe_path, judge_base_path, compile_spj=False): diff --git a/judge/runner.py b/judge/runner.py index b7874928..cb73fb15 100644 --- a/judge/runner.py +++ b/judge/runner.py @@ -3,6 +3,7 @@ import os import socket import shutil +from logger import logger from client import JudgeClient from language import languages from compiler import compile_ @@ -19,6 +20,8 @@ class JudgeInstanceRunner(object): judge_base_path = os.path.join(judger_workspace, "run", submission_id) if not token or token != os.environ.get("rpc_token"): + if token: + logger.info("Invalid token: " + token) return {"code": 2, "data": {"error": "Invalid token", "server": host_name}} try: @@ -78,8 +81,6 @@ class JudgeInstanceRunner(object): for item in judge_result["info"]: if item["result"] != 0: judge_result["result"] = item["result"] - if item.get("error"): - judge_result["info"] = item["error"] break else: l = sorted(judge_result["info"], key=lambda k: k["cpu_time"]) From 379ba3e6d40153837c109c567969be3f931f35fd Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Thu, 7 Apr 2016 16:44:15 +0800 Subject: [PATCH 10/10] =?UTF-8?q?SPJ=E7=A8=8B=E5=BA=8Fwarning=20as=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge/language.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/judge/language.py b/judge/language.py index 3bd4b56f..841ac7c3 100644 --- a/judge/language.py +++ b/judge/language.py @@ -7,7 +7,7 @@ languages = { "src_name": "main.c", "code": 1, "compile_command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}/main", - "spj_compile_command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}", + "spj_compile_command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -Werror -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}", "execute_command": "{exe_path}/main", "use_sandbox": True }, @@ -16,7 +16,7 @@ languages = { "src_name": "main.cpp", "code": 2, "compile_command": "/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}/main", - "spj_compile_command": "/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}", + "spj_compile_command": "/usr/bin/g++ -DONLINE_JUDGE -O2 -Werror -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}", "execute_command": "{exe_path}/main", "use_sandbox": True },
编号 输入文件名 输出文件名