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/sandbox.py
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
"""Grade a submission in child processes that the parent watches and restarts.
|
|
2
|
+
|
|
3
|
+
Each phase runs in its own child, as an unprivileged user when one is given. The parent
|
|
4
|
+
enforces every deadline itself, since student code can defeat the child's own timer and C code
|
|
5
|
+
ignores it. When a child dies or misses a deadline, the parent kills it and everything it
|
|
6
|
+
started, fails the tests it was working on, and starts a new child for the rest, at most
|
|
7
|
+
`MAX_RESPAWNS` times. Every test gets exactly one outcome. Linux and macOS; a grading user
|
|
8
|
+
and its process cap are for Gradescope (Linux).
|
|
9
|
+
|
|
10
|
+
A child that dies or hangs while it loads the notebook may be stuck in the whole-notebook run
|
|
11
|
+
(`loader.bind`). The parent then starts one more child that skips that run, so names that
|
|
12
|
+
need no run still get graded. Later children and the hidden phase skip it too. If that child
|
|
13
|
+
also fails to load, every test of the phase fails.
|
|
14
|
+
|
|
15
|
+
The parent keeps the first result per test and ignores results for tests the child has not
|
|
16
|
+
announced. Code in the child can still write forged events to the channel; the threat model
|
|
17
|
+
accepts that.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import logging
|
|
21
|
+
import pwd
|
|
22
|
+
import shutil
|
|
23
|
+
import stat
|
|
24
|
+
import sys
|
|
25
|
+
import time
|
|
26
|
+
from collections.abc import Mapping, Sequence
|
|
27
|
+
from dataclasses import dataclass
|
|
28
|
+
from enum import Enum, auto
|
|
29
|
+
from pathlib import Path, PurePosixPath
|
|
30
|
+
|
|
31
|
+
from mogs.grader._process import (
|
|
32
|
+
Child,
|
|
33
|
+
check_user,
|
|
34
|
+
describe_exit,
|
|
35
|
+
kill_user,
|
|
36
|
+
launch_options,
|
|
37
|
+
)
|
|
38
|
+
from mogs.grader.bundle import GradingBundle, Phase
|
|
39
|
+
from mogs.grader.datafiles import SUBMISSION, data_files
|
|
40
|
+
from mogs.grader.protocol import (
|
|
41
|
+
Event,
|
|
42
|
+
Job,
|
|
43
|
+
Loaded,
|
|
44
|
+
Loading,
|
|
45
|
+
Result,
|
|
46
|
+
Start,
|
|
47
|
+
)
|
|
48
|
+
from mogs.runner import FailureReason, Outcome, cap
|
|
49
|
+
|
|
50
|
+
MAX_RESPAWNS = 3
|
|
51
|
+
GRACE = 5.0
|
|
52
|
+
EXIT_GRACE = 2.0
|
|
53
|
+
|
|
54
|
+
logger = logging.getLogger(__name__)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(frozen=True, slots=True)
|
|
58
|
+
class Graded:
|
|
59
|
+
"""What `grade` found.
|
|
60
|
+
|
|
61
|
+
Attributes:
|
|
62
|
+
outcomes: One outcome per test of the bundle: public tests first, then hidden ones.
|
|
63
|
+
load_error: Why the notebook, or the grader's setup code, did not load, if it did
|
|
64
|
+
not in some phase. A hidden helper or test cell that fails does not count.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
outcomes: dict[str, Outcome]
|
|
68
|
+
load_error: str | None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def grade(
|
|
72
|
+
bundle: GradingBundle,
|
|
73
|
+
submission: Path,
|
|
74
|
+
root_workdir: Path,
|
|
75
|
+
*,
|
|
76
|
+
user: str | None,
|
|
77
|
+
files_root: Path | None = None,
|
|
78
|
+
files: Sequence[str] = (),
|
|
79
|
+
python: str = sys.executable,
|
|
80
|
+
) -> Graded:
|
|
81
|
+
"""Run the public phase, then the hidden phase, within one time budget.
|
|
82
|
+
|
|
83
|
+
Each phase gets a fresh `root_workdir/<phase>/` with a copy of the submission (as
|
|
84
|
+
`submission.py`) and of each data file `files_root/<rel>` at `<rel>`. With a `user`, the
|
|
85
|
+
directory belongs to that user, and every process of the user is killed after each phase.
|
|
86
|
+
|
|
87
|
+
The caller must make sure that `user` can enter `root_workdir` but not write to it.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
bundle: The full bundle; each phase gets its own part.
|
|
91
|
+
submission: The student's notebook, a regular file.
|
|
92
|
+
root_workdir: Existing directory for the phase directories.
|
|
93
|
+
user: Dedicated grading user to run student code as, or `None` to keep the current one.
|
|
94
|
+
files_root: Directory the data files are relative to. Required when `files` is given.
|
|
95
|
+
files: Data files or directories for the notebook, as relative paths.
|
|
96
|
+
python: Interpreter for the children. It must have mogs and the
|
|
97
|
+
notebook's dependencies.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
The outcomes, and why the notebook did not load, if it did not.
|
|
101
|
+
|
|
102
|
+
Raises:
|
|
103
|
+
ValueError: If `user` is root or the user running the grader, `submission` is not a
|
|
104
|
+
regular file, or a data path is absolute, leaves its root or is `submission.py`.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
if user is not None:
|
|
108
|
+
check_user(user)
|
|
109
|
+
|
|
110
|
+
if not stat.S_ISREG(submission.lstat().st_mode):
|
|
111
|
+
raise ValueError(f"the submission must be a regular file: {submission}")
|
|
112
|
+
|
|
113
|
+
data = _data_paths(files_root, files)
|
|
114
|
+
deadline = time.monotonic() + bundle.config.budget
|
|
115
|
+
results: dict[str, Outcome] = {}
|
|
116
|
+
run_failed = None
|
|
117
|
+
load_error = None
|
|
118
|
+
|
|
119
|
+
for phase in (Phase.PUBLIC, Phase.HIDDEN):
|
|
120
|
+
workdir = _prepare(root_workdir / phase.value, submission, data, user)
|
|
121
|
+
ran = _run_phase(
|
|
122
|
+
bundle, phase, workdir / SUBMISSION, workdir, user, python, deadline, run_failed
|
|
123
|
+
)
|
|
124
|
+
results |= ran.outcomes
|
|
125
|
+
run_failed = ran.run_failed
|
|
126
|
+
load_error = ran.load_error if load_error is None else load_error
|
|
127
|
+
|
|
128
|
+
if user is not None:
|
|
129
|
+
kill_user(user)
|
|
130
|
+
|
|
131
|
+
return Graded(results, load_error)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def run_phase(
|
|
135
|
+
bundle: GradingBundle,
|
|
136
|
+
phase: Phase,
|
|
137
|
+
submission: Path,
|
|
138
|
+
workdir: Path,
|
|
139
|
+
*,
|
|
140
|
+
user: str | None,
|
|
141
|
+
deadline: float,
|
|
142
|
+
python: str = sys.executable,
|
|
143
|
+
) -> dict[str, Outcome]:
|
|
144
|
+
"""Run one phase's tests in children started in `workdir`.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
bundle: The full bundle; only `phase`'s part reaches the child.
|
|
148
|
+
phase: The phase.
|
|
149
|
+
submission: The student's notebook.
|
|
150
|
+
workdir: Existing working directory for the children.
|
|
151
|
+
user: User to run the children as, or `None` to keep the current one.
|
|
152
|
+
deadline: `time.monotonic()` value at which the time budget runs out.
|
|
153
|
+
python: Interpreter for the children.
|
|
154
|
+
|
|
155
|
+
Returns:
|
|
156
|
+
One outcome per test of the phase, in bundle order.
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
ran = _run_phase(bundle, phase, submission, workdir, user, python, deadline, None)
|
|
160
|
+
return ran.outcomes
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@dataclass(frozen=True, slots=True)
|
|
164
|
+
class _PhaseRun:
|
|
165
|
+
"""One phase's outcomes, why the whole-notebook run failed, and why loading failed."""
|
|
166
|
+
|
|
167
|
+
outcomes: dict[str, Outcome]
|
|
168
|
+
run_failed: str | None
|
|
169
|
+
load_error: str | None
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _run_phase(
|
|
173
|
+
bundle: GradingBundle,
|
|
174
|
+
phase: Phase,
|
|
175
|
+
submission: Path,
|
|
176
|
+
workdir: Path,
|
|
177
|
+
user: str | None,
|
|
178
|
+
python: str,
|
|
179
|
+
deadline: float,
|
|
180
|
+
run_failed: str | None,
|
|
181
|
+
) -> _PhaseRun:
|
|
182
|
+
"""`run_phase`, starting from why the whole-notebook run failed, if it did."""
|
|
183
|
+
|
|
184
|
+
bundle = bundle.for_phase(phase)
|
|
185
|
+
tests = [name for cell in bundle.cells for name in cell.tests]
|
|
186
|
+
done: dict[str, Outcome] = {}
|
|
187
|
+
respawns = 0
|
|
188
|
+
load_error = None
|
|
189
|
+
|
|
190
|
+
while pending := [name for name in tests if name not in done]:
|
|
191
|
+
if respawns > MAX_RESPAWNS:
|
|
192
|
+
message = f"not run: the grader restarted {MAX_RESPAWNS} times after crashes"
|
|
193
|
+
_fail(done, pending, message, FailureReason.NOT_RUN)
|
|
194
|
+
break
|
|
195
|
+
|
|
196
|
+
if time.monotonic() >= deadline:
|
|
197
|
+
_fail(done, pending, _budget_message(bundle), FailureReason.BUDGET)
|
|
198
|
+
break
|
|
199
|
+
|
|
200
|
+
job = Job(bundle, submission, skip=tuple(done), run_failed=run_failed)
|
|
201
|
+
options = launch_options(workdir, user, bundle.config.memory_mb, python)
|
|
202
|
+
child = Child(job.to_json(), options)
|
|
203
|
+
watch = _Watch(bundle, pending, done, deadline, retry_load=run_failed is None)
|
|
204
|
+
|
|
205
|
+
try:
|
|
206
|
+
ending = watch.run(child)
|
|
207
|
+
finally:
|
|
208
|
+
output = child.close()
|
|
209
|
+
if output:
|
|
210
|
+
logger.info("grading child output (%s phase, end):\n%s", phase, output)
|
|
211
|
+
|
|
212
|
+
load_error = watch.load_error if load_error is None else load_error
|
|
213
|
+
|
|
214
|
+
match ending:
|
|
215
|
+
case _Ending.STOP:
|
|
216
|
+
break
|
|
217
|
+
case _Ending.RETRY_WITHOUT_RUN:
|
|
218
|
+
run_failed = watch.load_failure
|
|
219
|
+
case _Ending.RESPAWN:
|
|
220
|
+
respawns += 1
|
|
221
|
+
|
|
222
|
+
return _PhaseRun({name: done[name] for name in tests}, run_failed, load_error)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
class _Ending(Enum):
|
|
226
|
+
"""What `run_phase` does after a child ends."""
|
|
227
|
+
|
|
228
|
+
STOP = auto()
|
|
229
|
+
RESPAWN = auto()
|
|
230
|
+
RETRY_WITHOUT_RUN = auto()
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class _Stage(Enum):
|
|
234
|
+
"""What the child is doing, as far as its events tell."""
|
|
235
|
+
|
|
236
|
+
SETUP = auto()
|
|
237
|
+
LOADING = auto()
|
|
238
|
+
TEST = auto()
|
|
239
|
+
IDLE = auto()
|
|
240
|
+
FINISHING = auto()
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class _Watch:
|
|
244
|
+
"""Follows one child's events, enforces deadlines and fails the tests it is blamed for.
|
|
245
|
+
|
|
246
|
+
Every test it fails or finishes goes into `done`, which the caller shares across children.
|
|
247
|
+
With `retry_load`, a child that dies or hangs before it has loaded the notebook fails no
|
|
248
|
+
test: the watch keeps why in `load_failure` and asks for a retry without the notebook run.
|
|
249
|
+
Why the notebook failed to load for good, as the child reports it or as the watch sees it,
|
|
250
|
+
goes into `load_error`.
|
|
251
|
+
"""
|
|
252
|
+
|
|
253
|
+
def __init__(
|
|
254
|
+
self,
|
|
255
|
+
bundle: GradingBundle,
|
|
256
|
+
pending: list[str],
|
|
257
|
+
done: dict[str, Outcome],
|
|
258
|
+
deadline: float,
|
|
259
|
+
*,
|
|
260
|
+
retry_load: bool,
|
|
261
|
+
) -> None:
|
|
262
|
+
self._timeout = bundle.config.timeout
|
|
263
|
+
self._budget = bundle.config.budget
|
|
264
|
+
self._budget_message = _budget_message(bundle)
|
|
265
|
+
self._pending = pending
|
|
266
|
+
self._done = done
|
|
267
|
+
self._deadline = deadline
|
|
268
|
+
self._started: set[str] = set()
|
|
269
|
+
self._stage = _Stage.SETUP
|
|
270
|
+
self._unit: tuple[str, ...] = ()
|
|
271
|
+
self._since = time.monotonic()
|
|
272
|
+
self._due = self._since + self._timeout + GRACE
|
|
273
|
+
self._retry_load = retry_load
|
|
274
|
+
self.load_failure: str | None = None
|
|
275
|
+
self.load_error: str | None = None
|
|
276
|
+
|
|
277
|
+
def run(self, child: Child) -> _Ending:
|
|
278
|
+
"""Watch `child` until it exits or misses a deadline."""
|
|
279
|
+
|
|
280
|
+
while True:
|
|
281
|
+
until = min(self._due, self._deadline)
|
|
282
|
+
|
|
283
|
+
for event in child.wait(until):
|
|
284
|
+
self._handle(event)
|
|
285
|
+
|
|
286
|
+
if child.error is not None:
|
|
287
|
+
logger.warning("grading child broke the protocol: %s", child.error)
|
|
288
|
+
return self._crashed("the grader received a malformed event")
|
|
289
|
+
|
|
290
|
+
if child.returncode is not None:
|
|
291
|
+
return self._exited(child.returncode)
|
|
292
|
+
|
|
293
|
+
now = time.monotonic()
|
|
294
|
+
|
|
295
|
+
if now >= self._deadline:
|
|
296
|
+
if self._stage is _Stage.FINISHING:
|
|
297
|
+
return _Ending.STOP
|
|
298
|
+
stopped = f"stopped: grading ran past its {self._budget:g} s budget"
|
|
299
|
+
self._fail_unit(stopped, FailureReason.BUDGET)
|
|
300
|
+
self._fail_rest(self._budget_message, FailureReason.BUDGET)
|
|
301
|
+
return _Ending.STOP
|
|
302
|
+
|
|
303
|
+
if now >= self._due:
|
|
304
|
+
return self._overdue(now)
|
|
305
|
+
|
|
306
|
+
def _handle(self, event: Event) -> None:
|
|
307
|
+
match event:
|
|
308
|
+
case Loaded(error):
|
|
309
|
+
if self._stage is _Stage.SETUP:
|
|
310
|
+
self.load_error = error
|
|
311
|
+
self._enter(_Stage.IDLE, ())
|
|
312
|
+
case Loading(tests):
|
|
313
|
+
self._enter(_Stage.LOADING, tuple(t for t in tests if self._open(t)))
|
|
314
|
+
case Start(test) if self._open(test):
|
|
315
|
+
self._started.add(test)
|
|
316
|
+
self._enter(_Stage.TEST, (test,))
|
|
317
|
+
case Result(outcome) if outcome.name in self._started and self._open(outcome.name):
|
|
318
|
+
self._done[outcome.name] = outcome
|
|
319
|
+
self._enter(_Stage.IDLE, ())
|
|
320
|
+
|
|
321
|
+
if all(name in self._done for name in self._pending):
|
|
322
|
+
self._enter(_Stage.FINISHING, ())
|
|
323
|
+
|
|
324
|
+
def _open(self, test: str) -> bool:
|
|
325
|
+
"""Whether `test` belongs to this phase and has no outcome yet."""
|
|
326
|
+
|
|
327
|
+
return test in self._pending and test not in self._done
|
|
328
|
+
|
|
329
|
+
def _enter(self, stage: _Stage, unit: tuple[str, ...]) -> None:
|
|
330
|
+
self._stage = stage
|
|
331
|
+
self._unit = unit
|
|
332
|
+
self._since = time.monotonic()
|
|
333
|
+
grace = EXIT_GRACE if stage is _Stage.FINISHING else self._timeout + GRACE
|
|
334
|
+
self._due = self._since + grace
|
|
335
|
+
|
|
336
|
+
def _exited(self, returncode: int) -> _Ending:
|
|
337
|
+
if self._stage is _Stage.FINISHING:
|
|
338
|
+
return _Ending.STOP
|
|
339
|
+
|
|
340
|
+
return self._crashed(describe_exit(returncode))
|
|
341
|
+
|
|
342
|
+
def _crashed(self, status: str) -> _Ending:
|
|
343
|
+
"""Fail what the child was doing when it died; respawn, or retry setup without the run."""
|
|
344
|
+
|
|
345
|
+
match self._stage:
|
|
346
|
+
case _Stage.SETUP if self._retry_load:
|
|
347
|
+
self.load_failure = status
|
|
348
|
+
return _Ending.RETRY_WITHOUT_RUN
|
|
349
|
+
case _Stage.SETUP:
|
|
350
|
+
message = f"the grader stopped while loading your notebook: {status}"
|
|
351
|
+
self.load_error = message
|
|
352
|
+
self._fail_rest(message, FailureReason.LOAD_FAILED)
|
|
353
|
+
return _Ending.STOP
|
|
354
|
+
case _Stage.LOADING:
|
|
355
|
+
message = f"the grader stopped while loading the test cell: {status}"
|
|
356
|
+
self._fail_unit(message, FailureReason.KILLED)
|
|
357
|
+
case _Stage.TEST:
|
|
358
|
+
message = f"the grader stopped during this test: {status}"
|
|
359
|
+
self._fail_unit(message, FailureReason.KILLED)
|
|
360
|
+
case _Stage.IDLE | _Stage.FINISHING:
|
|
361
|
+
pass
|
|
362
|
+
|
|
363
|
+
return _Ending.RESPAWN
|
|
364
|
+
|
|
365
|
+
def _overdue(self, now: float) -> _Ending:
|
|
366
|
+
"""Fail what the child is stuck on; respawn, or retry setup without the run."""
|
|
367
|
+
|
|
368
|
+
took = f"it took longer than {self._timeout:g} s"
|
|
369
|
+
|
|
370
|
+
match self._stage:
|
|
371
|
+
case _Stage.SETUP if self._retry_load:
|
|
372
|
+
self.load_failure = took
|
|
373
|
+
return _Ending.RETRY_WITHOUT_RUN
|
|
374
|
+
case _Stage.SETUP:
|
|
375
|
+
message = f"the grader stopped while loading your notebook: {took}"
|
|
376
|
+
self.load_error = message
|
|
377
|
+
self._fail_rest(message, FailureReason.TIMEOUT)
|
|
378
|
+
return _Ending.STOP
|
|
379
|
+
case _Stage.LOADING:
|
|
380
|
+
message = f"the grader stopped while loading the test cell: {took}"
|
|
381
|
+
self._fail_unit(message, FailureReason.TIMEOUT)
|
|
382
|
+
case _Stage.TEST:
|
|
383
|
+
message = f"the test took longer than {self._timeout:g} s"
|
|
384
|
+
self._fail_unit(message, FailureReason.TIMEOUT, now - self._since)
|
|
385
|
+
case _Stage.FINISHING:
|
|
386
|
+
return _Ending.STOP
|
|
387
|
+
case _Stage.IDLE:
|
|
388
|
+
pass
|
|
389
|
+
|
|
390
|
+
return _Ending.RESPAWN
|
|
391
|
+
|
|
392
|
+
def _fail_unit(self, message: str, reason: FailureReason, seconds: float = 0.0) -> None:
|
|
393
|
+
_fail(self._done, self._unit, message, reason, seconds)
|
|
394
|
+
|
|
395
|
+
def _fail_rest(self, message: str, reason: FailureReason) -> None:
|
|
396
|
+
_fail(self._done, self._pending, message, reason)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def _fail(
|
|
400
|
+
done: dict[str, Outcome],
|
|
401
|
+
names: Sequence[str],
|
|
402
|
+
message: str,
|
|
403
|
+
reason: FailureReason,
|
|
404
|
+
seconds: float = 0.0,
|
|
405
|
+
) -> None:
|
|
406
|
+
"""Give each of `names` without an outcome a failed one."""
|
|
407
|
+
|
|
408
|
+
for name in names:
|
|
409
|
+
if name not in done:
|
|
410
|
+
done[name] = Outcome(name, False, cap(message), reason, seconds)
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def _budget_message(bundle: GradingBundle) -> str:
|
|
414
|
+
return f"not run: grading took longer than its {bundle.config.budget:g} s budget"
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def _data_paths(files_root: Path | None, files: Sequence[str]) -> dict[PurePosixPath, Path]:
|
|
418
|
+
"""Data files by relative destination; see `datafiles.data_files`.
|
|
419
|
+
|
|
420
|
+
Raises:
|
|
421
|
+
ValueError: If a path is bad or missing, or `files` is given without `files_root`.
|
|
422
|
+
"""
|
|
423
|
+
|
|
424
|
+
if not files:
|
|
425
|
+
return {}
|
|
426
|
+
|
|
427
|
+
if files_root is None:
|
|
428
|
+
raise ValueError("data files need a files_root")
|
|
429
|
+
|
|
430
|
+
return data_files(files_root, files)
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
def _prepare(
|
|
434
|
+
workdir: Path, submission: Path, files: Mapping[PurePosixPath, Path], user: str | None
|
|
435
|
+
) -> Path:
|
|
436
|
+
"""Create `workdir` with copies of the submission and the data files.
|
|
437
|
+
|
|
438
|
+
Raises:
|
|
439
|
+
FileExistsError: If `workdir` exists; each phase needs a fresh one.
|
|
440
|
+
"""
|
|
441
|
+
|
|
442
|
+
workdir.mkdir(mode=0o700)
|
|
443
|
+
shutil.copyfile(submission, workdir / SUBMISSION, follow_symlinks=False)
|
|
444
|
+
|
|
445
|
+
for rel, source in files.items():
|
|
446
|
+
target = workdir / rel
|
|
447
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
448
|
+
shutil.copyfile(source, target)
|
|
449
|
+
|
|
450
|
+
if user is not None:
|
|
451
|
+
_hand_over(workdir, user)
|
|
452
|
+
|
|
453
|
+
return workdir
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def _hand_over(workdir: Path, user: str) -> None:
|
|
457
|
+
"""Make `user` the owner of `workdir` and everything in it."""
|
|
458
|
+
|
|
459
|
+
group = pwd.getpwnam(user).pw_gid
|
|
460
|
+
|
|
461
|
+
for path in [workdir, *workdir.rglob("*")]:
|
|
462
|
+
shutil.chown(path, user, group)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Gradescope's `setup.sh` and `run_autograder`, rendered from the files in this folder."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from importlib.resources import files
|
|
5
|
+
from string import Template
|
|
6
|
+
|
|
7
|
+
from mogs.config import ASSIGNMENT, AssignmentConfig, ConfigError
|
|
8
|
+
from mogs.grader._user import STUDENT_USER
|
|
9
|
+
|
|
10
|
+
# The uv that `setup.sh` installs on Gradescope.
|
|
11
|
+
UV_VERSION = "0.11.21"
|
|
12
|
+
PYTHON_RE = re.compile(r"3\.\d+(\.\d+)?")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def setup_script(cfg: AssignmentConfig) -> str:
|
|
16
|
+
"""Render `setup.sh` for the assignment.
|
|
17
|
+
|
|
18
|
+
Raises:
|
|
19
|
+
ConfigError: If `cfg.python` is not a version such as `3.13` or `3.13.5`.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
if not PYTHON_RE.fullmatch(cfg.python):
|
|
23
|
+
raise ConfigError(
|
|
24
|
+
f'{ASSIGNMENT} `python` must be a version such as "3.13", got {cfg.python!r}.'
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
template = Template(_read("setup.sh"))
|
|
28
|
+
return template.substitute(uv_version=UV_VERSION, python=cfg.python, user=STUDENT_USER)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def run_autograder_script() -> str:
|
|
32
|
+
"""The `run_autograder` script."""
|
|
33
|
+
|
|
34
|
+
return _read("run_autograder")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _read(name: str) -> str:
|
|
38
|
+
return files(__name__).joinpath(name).read_text(encoding="utf-8")
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Gradescope runs this once, as root, when it builds the autograder image.
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
|
|
5
|
+
# A pinned uv.
|
|
6
|
+
command -v curl >/dev/null || { apt-get update && apt-get install -y curl ca-certificates; }
|
|
7
|
+
# The grader kills the grading user's processes with pkill.
|
|
8
|
+
command -v pkill >/dev/null || { apt-get update && apt-get install -y procps; }
|
|
9
|
+
curl -LsSf https://astral.sh/uv/${uv_version}/install.sh \
|
|
10
|
+
| env UV_INSTALL_DIR=/usr/local/bin INSTALLER_NO_MODIFY_PATH=1 sh
|
|
11
|
+
|
|
12
|
+
# Python and the dependencies, outside /autograder/source so the student user can read them.
|
|
13
|
+
export UV_PYTHON_INSTALL_DIR=/opt/mogs-python
|
|
14
|
+
uv venv --managed-python --python ${python} /opt/mogs-venv
|
|
15
|
+
# Compiled now, so grading children (unprivileged, no write access) do not recompile per run.
|
|
16
|
+
uv pip install --python /opt/mogs-venv/bin/python --index-strategy unsafe-best-match \
|
|
17
|
+
--compile-bytecode -r /autograder/source/requirements.txt
|
|
18
|
+
|
|
19
|
+
# The unprivileged user that runs student code.
|
|
20
|
+
id -u ${user} >/dev/null 2>&1 \
|
|
21
|
+
|| useradd --system --no-create-home --shell /usr/sbin/nologin ${user}
|
|
22
|
+
|
|
23
|
+
# Python and the venv are readable by everyone; the hidden tests only by root.
|
|
24
|
+
chmod -R a+rX /opt/mogs-python /opt/mogs-venv
|
|
25
|
+
chmod -R go-rwx /autograder/source
|
|
26
|
+
|
|
27
|
+
# Fail the build here, not at grading time, if the student user cannot import the grader.
|
|
28
|
+
runuser -u ${user} -- /opt/mogs-venv/bin/python -c "import mogs, marimo"
|