|
26 | 26 |
|
27 | 27 | """ |
28 | 28 |
|
| 29 | +import json |
29 | 30 | import logging |
| 31 | +import difflib |
30 | 32 |
|
31 | 33 | from cms.db import Dataset, File, Submission |
32 | 34 | from cms.grading.languagemanager import get_language |
@@ -88,6 +90,89 @@ def get(self, file_id): |
88 | 90 | self.fetch(digest, "text/plain", real_filename) |
89 | 91 |
|
90 | 92 |
|
| 93 | +class SubmissionDiffHandler(BaseHandler): |
| 94 | + """Shows a diff between two submissions. |
| 95 | + """ |
| 96 | + @require_permission(BaseHandler.AUTHENTICATED) |
| 97 | + def get(self, old_id, new_id): |
| 98 | + sub_old = Submission.get_from_id(old_id, self.sql_session) |
| 99 | + sub_new = Submission.get_from_id(new_id, self.sql_session) |
| 100 | + |
| 101 | + self.set_header("Content-type", "application/json; charset=utf-8") |
| 102 | + resp = { |
| 103 | + 'message': None, |
| 104 | + 'files': [] |
| 105 | + } |
| 106 | + |
| 107 | + if sub_old is None or sub_new is None: |
| 108 | + missing_id = old_id if sub_old is None else new_id |
| 109 | + resp['message'] = f"Submission ID {missing_id} not found." |
| 110 | + self.write(json.dumps(resp)) |
| 111 | + return |
| 112 | + |
| 113 | + if sub_old.task_id == sub_new.task_id: |
| 114 | + files_to_compare = sub_old.task.submission_format |
| 115 | + old_files = sub_old.files |
| 116 | + new_files = sub_new.files |
| 117 | + elif len(sub_old.files) == 1 and len(sub_new.files) == 1: |
| 118 | + old_file = list(sub_old.files.values())[0] |
| 119 | + old_files = {"submission.%l": old_file} |
| 120 | + new_file = list(sub_new.files.values())[0] |
| 121 | + new_files = {"submission.%l": new_file} |
| 122 | + files_to_compare = ["submission.%l"] |
| 123 | + else: |
| 124 | + resp['message'] = "Cannot compare submissions: they are for " \ |
| 125 | + "different tasks and have more than 1 file." |
| 126 | + self.write(json.dumps(resp)) |
| 127 | + return |
| 128 | + |
| 129 | + result_files = [] |
| 130 | + for fname in files_to_compare: |
| 131 | + if ".%l" in fname: |
| 132 | + if sub_old.language == sub_new.language and sub_old.language is not None: |
| 133 | + ext = get_language(sub_old.language).source_extension |
| 134 | + else: |
| 135 | + ext = ".txt" |
| 136 | + real_fname = fname.replace(".%l", ext) |
| 137 | + else: |
| 138 | + real_fname = fname |
| 139 | + |
| 140 | + def get_file(x, which): |
| 141 | + if fname not in x: |
| 142 | + return None, f"File not present in {which} submission" |
| 143 | + digest = x[fname].digest |
| 144 | + file_bin = self.service.file_cacher.get_file_content(digest) |
| 145 | + if len(file_bin) > 1000000: |
| 146 | + return None, f"{which} file is too big to diff".capitalize() |
| 147 | + file_lines = file_bin.decode(errors='replace').splitlines() |
| 148 | + if len(file_lines) > 5000: |
| 149 | + return None, f"{which} file has too many lines to diff".capitalize() |
| 150 | + return file_lines, None |
| 151 | + |
| 152 | + old_content, old_status = get_file(old_files, "old") |
| 153 | + if old_status: |
| 154 | + result_files.append({"fname": real_fname, "status": old_status}) |
| 155 | + continue |
| 156 | + new_content, new_status = get_file(new_files, "new") |
| 157 | + if new_status: |
| 158 | + result_files.append({"fname": real_fname, "status": new_status}) |
| 159 | + continue |
| 160 | + |
| 161 | + if old_content == new_content: |
| 162 | + result_files.append({"fname": real_fname, "status": "No changes"}) |
| 163 | + else: |
| 164 | + diff_iter = difflib.unified_diff(old_content, new_content, lineterm='') |
| 165 | + # skip the "---" and "+++" lines. |
| 166 | + next(diff_iter) |
| 167 | + next(diff_iter) |
| 168 | + diff = '\n'.join(diff_iter) |
| 169 | + |
| 170 | + result_files.append({"fname": real_fname, "diff": diff}) |
| 171 | + |
| 172 | + resp['files'] = result_files |
| 173 | + self.write(json.dumps(resp)) |
| 174 | + |
| 175 | + |
91 | 176 | class SubmissionCommentHandler(BaseHandler): |
92 | 177 | """Called when the admin comments on a submission. |
93 | 178 |
|
|
0 commit comments