Repository navigation
Expand file tree
/
Copy pathgrade.py
More file actions
48 lines (43 loc) · 2.03 KB
/
Copy pathgrade.py
File metadata and controls
48 lines (43 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Trusted submission gate: protected inputs/tests cannot be altered."""
from pathlib import Path
import sys, json, filecmp, subprocess, os
Q = Path(__file__).resolve().parent.parent
root = Path(sys.argv[1]).resolve()
output = Path(sys.argv[2]).resolve()
allowed = json.loads((Q / 'author/editable.json').read_text())
def editable(rel):
return any(rel == p or (p.endswith('/') and rel.startswith(p)) for p in allowed) and '/__tests__/' not in '/' + rel and '.test.' not in rel
errors = []
if not root.is_dir():
errors.append('source root missing')
else:
for base in (Q / 'candidate').rglob('*'):
if not base.is_file():
continue
rel = base.relative_to(Q / 'candidate').as_posix()
if editable(rel):
continue
if rel.startswith(('.build/', '.work/')):
continue
other = root / rel
if not other.is_file() or other.is_symlink() or not filecmp.cmp(base, other, shallow=False):
errors.append(rel)
if errors:
result = {'status': 'environment_invalid', 'reason': 'submission changed protected inputs or package incomplete', 'files': errors}
else:
try:
p = subprocess.run(
[sys.executable, '-B', str(Q / 'author/grade_impl.py'), str(root), str(output)],
env=dict(os.environ, PYTHONDONTWRITEBYTECODE='1'),
capture_output=True, text=True, timeout=180
)
if p.returncode or not output.exists():
result = {'status': 'environment_invalid', 'reason': 'grader could not complete', 'stdout': p.stdout[-4000:], 'stderr': p.stderr[-4000:]}
else:
result = json.loads(output.read_text())
result['submissionGate'] = 'passed'
except subprocess.TimeoutExpired:
result = {'status': 'environment_invalid', 'reason': 'grader safety timeout'}
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(json.dumps(result, ensure_ascii=False, indent=2) + '\n')
print(json.dumps({k: v for k, v in result.items() if k not in ['details', 'raw']}, ensure_ascii=False))