mogs 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mogs/__init__.py +17 -0
- mogs/_assertrewrite.py +44 -0
- mogs/_env.py +26 -0
- mogs/_run.py +111 -0
- mogs/_typst/preview/cmarker/0.1.10/LICENSE +7 -0
- mogs/_typst/preview/cmarker/0.1.10/lib.typ +462 -0
- mogs/_typst/preview/cmarker/0.1.10/plugin.wasm +0 -0
- mogs/_typst/preview/cmarker/0.1.10/typst.toml +11 -0
- mogs/_typst/preview/mitex/0.2.7/LICENSE +176 -0
- mogs/_typst/preview/mitex/0.2.7/lib.typ +1 -0
- mogs/_typst/preview/mitex/0.2.7/mitex.typ +45 -0
- mogs/_typst/preview/mitex/0.2.7/mitex.wasm +0 -0
- mogs/_typst/preview/mitex/0.2.7/specs/latex/standard.typ +1187 -0
- mogs/_typst/preview/mitex/0.2.7/specs/mod.typ +12 -0
- mogs/_typst/preview/mitex/0.2.7/specs/prelude.typ +230 -0
- mogs/_typst/preview/mitex/0.2.7/typst.toml +12 -0
- mogs/assignment.py +172 -0
- mogs/build.py +605 -0
- mogs/check.py +410 -0
- mogs/cli.py +272 -0
- mogs/config.py +619 -0
- mogs/environment.py +118 -0
- mogs/export.py +556 -0
- mogs/grader/__init__.py +1 -0
- mogs/grader/_process.py +383 -0
- mogs/grader/_user.py +7 -0
- mogs/grader/bundle.py +320 -0
- mogs/grader/child.py +317 -0
- mogs/grader/datafiles.py +54 -0
- mogs/grader/main.py +132 -0
- mogs/grader/protocol.py +175 -0
- mogs/grader/results.py +337 -0
- mogs/grader/sandbox.py +462 -0
- mogs/grader/templates/__init__.py +38 -0
- mogs/grader/templates/run_autograder +3 -0
- mogs/grader/templates/setup.sh +28 -0
- mogs/loader.py +260 -0
- mogs/pdf.py +676 -0
- mogs/regrade.py +223 -0
- mogs/requirements.py +232 -0
- mogs/runner.py +281 -0
- mogs/scaffold.py +155 -0
- mogs/selfcheck.py +173 -0
- mogs/source.py +1438 -0
- mogs/transform.py +769 -0
- mogs-0.7.0.dist-info/METADATA +17 -0
- mogs-0.7.0.dist-info/RECORD +49 -0
- mogs-0.7.0.dist-info/WHEEL +4 -0
- mogs-0.7.0.dist-info/entry_points.txt +3 -0
mogs/grader/results.py
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
"""Find the uploaded files and turn test outcomes into Gradescope's `results.json`.
|
|
2
|
+
|
|
3
|
+
Students see the score summary, the public test results and the notices. Each question's row,
|
|
4
|
+
which also lists its hidden tests, is hidden from them. Text a student controls (failure
|
|
5
|
+
messages, file names) is capped and quoted so it cannot change the layout around it.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
import stat
|
|
11
|
+
from collections.abc import Mapping, Sequence
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import NotRequired, TypedDict
|
|
15
|
+
|
|
16
|
+
from mogs.config import TESTS_FOLDER
|
|
17
|
+
from mogs.grader.bundle import GradingBundle
|
|
18
|
+
from mogs.runner import Outcome, cap
|
|
19
|
+
|
|
20
|
+
SKIPPED_DIRS = {"__MACOSX", "__pycache__"}
|
|
21
|
+
NOTICE_ROW = "Submission"
|
|
22
|
+
PUBLIC_ROW = "Public tests"
|
|
23
|
+
ALL_PASSED = "All tests passed"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Row(TypedDict):
|
|
27
|
+
"""One entry of `tests` in `results.json`."""
|
|
28
|
+
|
|
29
|
+
name: str
|
|
30
|
+
number: NotRequired[str]
|
|
31
|
+
score: float
|
|
32
|
+
max_score: float
|
|
33
|
+
output: str
|
|
34
|
+
output_format: str
|
|
35
|
+
visibility: str
|
|
36
|
+
status: NotRequired[str]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Results(TypedDict):
|
|
40
|
+
"""The content of `results.json`.
|
|
41
|
+
|
|
42
|
+
`score` is set only when `tests` is empty, since Gradescope then has no rows to add up.
|
|
43
|
+
`output` is the score summary for students: Gradescope hides the total when a row is hidden.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
tests: list[Row]
|
|
47
|
+
stdout_visibility: str
|
|
48
|
+
score: NotRequired[float]
|
|
49
|
+
output: NotRequired[str]
|
|
50
|
+
output_format: NotRequired[str]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True, slots=True)
|
|
54
|
+
class Submission:
|
|
55
|
+
"""The files found in an upload.
|
|
56
|
+
|
|
57
|
+
Attributes:
|
|
58
|
+
py: The notebook to grade, or `None` when there is none or it is ambiguous.
|
|
59
|
+
answers: The written answers: a PDF, else the export's Markdown fallback
|
|
60
|
+
`<notebook stem>_answers.md`, else `None`. Only tells whether the student sent
|
|
61
|
+
any; graders see every uploaded file.
|
|
62
|
+
error: Why `py` is `None`, ready to show the student.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
py: Path | None
|
|
66
|
+
answers: Path | None
|
|
67
|
+
error: str | None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def find_submission(sub_dir: Path, notebook: str, name: str | None = None) -> Submission:
|
|
71
|
+
"""Find the notebook and the written answers anywhere under `sub_dir`.
|
|
72
|
+
|
|
73
|
+
Skips `__MACOSX`, `__pycache__`, dot files and dot folders, and never follows or picks a
|
|
74
|
+
symlink. Public tests, the `.py` files in a `tests/` folder next to another `.py` file, are
|
|
75
|
+
never the notebook. Of the rest, a file named `notebook` wins; otherwise the only `.py` file
|
|
76
|
+
is taken. Any PDF counts as the written answers. Of Markdown files, only the export's
|
|
77
|
+
fallback `<name>_answers.md` (any case) does, so a stray `README.md` does not.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
sub_dir: The unpacked upload.
|
|
81
|
+
notebook: File name of the student notebook.
|
|
82
|
+
name: Assignment name, which the export names its files after. Defaults to the
|
|
83
|
+
notebook's stem.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
The files found, with an error message when the notebook is missing or ambiguous.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
files = sorted(_regular_files(sub_dir))
|
|
90
|
+
scripts = [path for path in files if path.suffix == ".py"]
|
|
91
|
+
# Not the public tests the student copy ships in `tests/`, next to the notebook.
|
|
92
|
+
folders = {path.parent for path in scripts}
|
|
93
|
+
others = [
|
|
94
|
+
path
|
|
95
|
+
for path in scripts
|
|
96
|
+
if path.parent.name != TESTS_FOLDER or path.parent.parent not in folders
|
|
97
|
+
]
|
|
98
|
+
exact = [path for path in others if path.name == notebook]
|
|
99
|
+
pdfs = [path for path in files if path.suffix.lower() == ".pdf"]
|
|
100
|
+
fallback = f"{name or Path(notebook).stem}_answers.md".casefold()
|
|
101
|
+
markdown = [path for path in files if path.name.casefold() == fallback]
|
|
102
|
+
answers = next(iter(pdfs or markdown), None)
|
|
103
|
+
wanted = inline_code(notebook)
|
|
104
|
+
|
|
105
|
+
match exact, others:
|
|
106
|
+
case [py], _:
|
|
107
|
+
return Submission(py, answers, None)
|
|
108
|
+
case [], [py]:
|
|
109
|
+
return Submission(py, answers, None)
|
|
110
|
+
case [], []:
|
|
111
|
+
error = f"No `.py` file found in your upload. Upload {wanted}."
|
|
112
|
+
case [], several:
|
|
113
|
+
names = _names(several, sub_dir)
|
|
114
|
+
error = f"No {wanted} in your upload, and several other `.py` files: {names}. " + (
|
|
115
|
+
f"Upload {wanted}, or a single `.py` file."
|
|
116
|
+
)
|
|
117
|
+
case several, _:
|
|
118
|
+
error = f"Found {wanted} more than once: {_names(several, sub_dir)}. Upload one."
|
|
119
|
+
|
|
120
|
+
return Submission(None, answers, error)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _names(paths: Sequence[Path], root: Path) -> str:
|
|
124
|
+
"""The paths relative to `root`, as inline code, comma separated."""
|
|
125
|
+
|
|
126
|
+
return ", ".join(inline_code(str(path.relative_to(root))) for path in paths)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _regular_files(root: Path) -> list[Path]:
|
|
130
|
+
"""Regular files under `root`, skipping junk folders, dot names and symlinks."""
|
|
131
|
+
|
|
132
|
+
found = []
|
|
133
|
+
|
|
134
|
+
for folder, dirs, names in os.walk(root, followlinks=False):
|
|
135
|
+
dirs[:] = [name for name in dirs if not name.startswith(".") and name not in SKIPPED_DIRS]
|
|
136
|
+
|
|
137
|
+
for name in names:
|
|
138
|
+
path = Path(folder, name)
|
|
139
|
+
|
|
140
|
+
if not name.startswith(".") and stat.S_ISREG(path.lstat().st_mode):
|
|
141
|
+
found.append(path)
|
|
142
|
+
|
|
143
|
+
return found
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def build_results(
|
|
147
|
+
bundle: GradingBundle, outcomes: Mapping[str, Outcome], notices: Sequence[str]
|
|
148
|
+
) -> Results:
|
|
149
|
+
"""Gradescope's `results.json` content.
|
|
150
|
+
|
|
151
|
+
Each auto-graded question gets a row worth `points × passed / total`, hidden from students,
|
|
152
|
+
that lists all its tests with their failure messages. A visible 0-point "Public tests" row,
|
|
153
|
+
marked failed when one of them fails, lists the public tests. The top-level output gives the
|
|
154
|
+
total and each question's score.
|
|
155
|
+
Notices go first, in a 0-point "Submission" row marked failed, so Gradescope does not show
|
|
156
|
+
it as passed. Manual questions get no row; with no rows at all, the results carry a
|
|
157
|
+
top-level score of 0.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
bundle: The full bundle.
|
|
161
|
+
outcomes: One outcome per test of the bundle.
|
|
162
|
+
notices: Messages about the upload, in Markdown.
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
The results, ready for `json.dump`.
|
|
166
|
+
|
|
167
|
+
Raises:
|
|
168
|
+
KeyError: If a test has no outcome.
|
|
169
|
+
ValueError: If an auto-graded question has no tests.
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
public_parts = []
|
|
173
|
+
public_failed = False
|
|
174
|
+
question_rows = []
|
|
175
|
+
|
|
176
|
+
for question in bundle.config.questions.values():
|
|
177
|
+
if question.manual:
|
|
178
|
+
continue
|
|
179
|
+
|
|
180
|
+
cells = [cell for cell in bundle.cells if cell.qid == question.id]
|
|
181
|
+
public = [outcomes[name] for cell in cells if not cell.hidden for name in cell.tests]
|
|
182
|
+
hidden = [outcomes[name] for cell in cells if cell.hidden for name in cell.tests]
|
|
183
|
+
total = len(public) + len(hidden)
|
|
184
|
+
|
|
185
|
+
if total == 0:
|
|
186
|
+
raise ValueError(f"question {question.id} has no tests")
|
|
187
|
+
|
|
188
|
+
if public:
|
|
189
|
+
public_parts.append(f"**{question.title}**\n\n{_output(public, [])}")
|
|
190
|
+
public_failed |= not all(outcome.passed for outcome in public)
|
|
191
|
+
|
|
192
|
+
passed = sum(outcome.passed for outcome in public + hidden)
|
|
193
|
+
score = round(question.points * passed / total, 4)
|
|
194
|
+
output = _output(public, hidden)
|
|
195
|
+
question_rows.append(
|
|
196
|
+
row(question.title, score, question.points, output, question.id, hidden=True)
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
rows = [notice_row(notices)] if notices else []
|
|
200
|
+
|
|
201
|
+
if public_parts:
|
|
202
|
+
rows.append(row(PUBLIC_ROW, 0, 0, "\n\n".join(public_parts), failed=public_failed))
|
|
203
|
+
|
|
204
|
+
rows += question_rows
|
|
205
|
+
|
|
206
|
+
if not question_rows:
|
|
207
|
+
return {"tests": rows, "stdout_visibility": "hidden", "score": 0.0}
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
"tests": rows,
|
|
211
|
+
"stdout_visibility": "hidden",
|
|
212
|
+
"output": _summary(question_rows),
|
|
213
|
+
"output_format": "md",
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _summary(rows: Sequence[Row]) -> str:
|
|
218
|
+
"""The total and each question's score, in Markdown."""
|
|
219
|
+
|
|
220
|
+
score = sum(row["score"] for row in rows)
|
|
221
|
+
max_score = sum(row["max_score"] for row in rows)
|
|
222
|
+
lines = [f"Total: {_fraction(score, max_score)}", ""]
|
|
223
|
+
lines += [f"- {row['name']}: {_fraction(row['score'], row['max_score'])}" for row in rows]
|
|
224
|
+
|
|
225
|
+
return "\n".join(lines)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _fraction(score: float, max_score: float) -> str:
|
|
229
|
+
"""`18.75 / 25`."""
|
|
230
|
+
|
|
231
|
+
return f"{round(score, 4):g} / {max_score:g}"
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def failure_results(bundle: GradingBundle | None, error: BaseException) -> Results:
|
|
235
|
+
"""Results for a grader crash: every question at 0, or one row without a bundle.
|
|
236
|
+
|
|
237
|
+
Every row is marked failed. Only the exception type is shown, since its text may quote
|
|
238
|
+
hidden test source.
|
|
239
|
+
"""
|
|
240
|
+
|
|
241
|
+
message = (
|
|
242
|
+
f"The autograder failed ({type(error).__name__}), so nothing was graded. "
|
|
243
|
+
"Please tell your instructor."
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
if bundle is None:
|
|
247
|
+
rows = [row("Autograder", 0, 0, message, failed=True)]
|
|
248
|
+
else:
|
|
249
|
+
rows = [
|
|
250
|
+
row(q.title, 0, q.points, message, q.id, failed=True)
|
|
251
|
+
for q in bundle.config.questions.values()
|
|
252
|
+
if not q.manual
|
|
253
|
+
]
|
|
254
|
+
|
|
255
|
+
return {"tests": rows, "stdout_visibility": "hidden"}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def notice_row(notices: Sequence[str]) -> Row:
|
|
259
|
+
"""The 0-point row that shows messages about the upload."""
|
|
260
|
+
|
|
261
|
+
return row(NOTICE_ROW, 0, 0, "\n\n".join(cap(notice) for notice in notices), failed=True)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def row(
|
|
265
|
+
name: str,
|
|
266
|
+
score: float,
|
|
267
|
+
max_score: float,
|
|
268
|
+
output: str,
|
|
269
|
+
number: str | None = None,
|
|
270
|
+
*,
|
|
271
|
+
failed: bool = False,
|
|
272
|
+
hidden: bool = False,
|
|
273
|
+
) -> Row:
|
|
274
|
+
"""One entry of `tests` in `results.json`.
|
|
275
|
+
|
|
276
|
+
`failed` sets its status to `failed`, and `hidden` hides the row from students.
|
|
277
|
+
"""
|
|
278
|
+
|
|
279
|
+
entry = Row(
|
|
280
|
+
name=name,
|
|
281
|
+
score=score,
|
|
282
|
+
max_score=max_score,
|
|
283
|
+
output=output,
|
|
284
|
+
output_format="md",
|
|
285
|
+
visibility="hidden" if hidden else "visible",
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
if number is not None:
|
|
289
|
+
entry["number"] = number
|
|
290
|
+
|
|
291
|
+
if failed:
|
|
292
|
+
entry["status"] = "failed"
|
|
293
|
+
|
|
294
|
+
return entry
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _output(public: list[Outcome], hidden: list[Outcome]) -> str:
|
|
298
|
+
"""Markdown for tests: `All tests passed`, or each test with its failure message."""
|
|
299
|
+
|
|
300
|
+
if all(outcome.passed for outcome in public + hidden):
|
|
301
|
+
return ALL_PASSED
|
|
302
|
+
|
|
303
|
+
parts = []
|
|
304
|
+
labeled = [(outcome, "") for outcome in public] + [(o, " (hidden)") for o in hidden]
|
|
305
|
+
|
|
306
|
+
for outcome, label in labeled:
|
|
307
|
+
mark = "✓" if outcome.passed else "✗"
|
|
308
|
+
parts.append(f"{mark} {inline_code(outcome.name)}{label}")
|
|
309
|
+
|
|
310
|
+
if not outcome.passed and outcome.message:
|
|
311
|
+
parts.append(code_block(cap(outcome.message)))
|
|
312
|
+
|
|
313
|
+
return "\n\n".join(parts)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def code_block(text: str) -> str:
|
|
317
|
+
"""A fenced code block holding `text` verbatim; the fence outgrows every backtick run."""
|
|
318
|
+
|
|
319
|
+
fence = "`" * max(3, _longest_backtick_run(text) + 1)
|
|
320
|
+
|
|
321
|
+
return f"{fence}\n{text}\n{fence}"
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def inline_code(text: str) -> str:
|
|
325
|
+
"""`text` as one line of inline code that its own backticks cannot end.
|
|
326
|
+
|
|
327
|
+
Control characters, newlines included, become `?`.
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
text = re.sub(r"[\x00-\x1f\x7f]", "?", text)
|
|
331
|
+
fence = "`" * (_longest_backtick_run(text) + 1)
|
|
332
|
+
|
|
333
|
+
return f"{fence} {text} {fence}" if "`" in text else f"{fence}{text}{fence}"
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _longest_backtick_run(text: str) -> int:
|
|
337
|
+
return max((len(run) for run in re.findall("`+", text)), default=0)
|