-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscore.py
executable file
·92 lines (71 loc) · 2.54 KB
/
score.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
from pathlib import Path
import subprocess
import threading
from threading import Event
import time
import json
import signal
import os
import sys
from collections import defaultdict
script_dir = Path(__file__).parent
def read_from_pipe(proc, event, append_data):
while not event.is_set():
append_data(proc.stderr.read(1))
def test_submission(submission: Path):
print(f"Testing {submission.name}")
with subprocess.Popen(
["make", "--no-builtin-rules", "run"],
cwd=submission,
stderr=subprocess.PIPE,
universal_newlines=True,
preexec_fn=os.setsid,
) as proc:
# Read from pipe in a thread
event = Event()
data = ""
def append_data(new_data):
print(new_data, end="")
nonlocal data
data += new_data
thread = threading.Thread(target=read_from_pipe, args=(proc, event, append_data))
thread.start()
# Wait a while then terminate
time.sleep(60)
event.set()
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
return data
def main():
if len(sys.argv) == 3:
# Run just the language/submission combo specified in the command line
force = (sys.argv[1], sys.argv[2])
elif len(sys.argv) == 1:
# Run all languages/submissions
force = None
else:
print(f"USAGE: {sys.argv[0]} [language] [submission]")
sys.exit(1)
results_file = script_dir / "results.json"
results = defaultdict(dict)
if force is not None:
# Try to load all results from previous results file
if results_file.exists():
with open(results_file, "r") as f:
results = defaultdict(dict, json.load(f))
for lang in (script_dir / "submissions").glob("*"):
for submission in lang.glob("*"):
if submission.is_dir() and not any(
child.name == "TBD" or child.name == "DISQUALIFIED" for child in submission.iterdir()
):
if force is not None:
force_language, force_submission = force
if lang.name != force_language or submission.name != force_submission:
print(f"Skipping {lang.name} {submission.name}")
continue
print(f"Testing {lang.name} {submission.name}")
results[lang.name][submission.name] = test_submission(submission)
with open(results_file, "w") as f:
json.dump(results, f)
if __name__ == "__main__":
main()