-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·66 lines (53 loc) · 1.85 KB
/
test.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
#!/usr/bin/env python3
import argparse
import subprocess
from pathlib import Path
RESET = "\033[0m"
GRAY = "\033[1;90m"
GREEN = "\033[1;32m"
MAGENTA = "\033[1;35m"
RED = "\033[1;31m"
def find_solution(path, solutions):
if path.suffix == ".input":
solutions[path] = path.parent / (path.stem + ".solution")
def find_solutions(paths):
solutions = {}
for path in paths:
if path.is_dir():
for subpath in path.glob("**/*.input"):
find_solution(subpath, solutions)
else:
find_solution(path, solutions)
return solutions
def main():
parser = argparse.ArgumentParser()
parser.add_argument("file", type=Path, nargs="+")
parser.add_argument("--command", "-c", required=True, action="append")
parser.add_argument("--diff", "-d", default="diff")
args = parser.parse_args()
for inputpath, solutionpath in sorted(find_solutions(args.file).items()):
try:
with open(solutionpath) as f:
solutionstr = f.read()
except FileNotFoundError:
print(f"{GRAY}No solution{RESET} for {inputpath}")
continue
try:
resultstr = subprocess.run(
args.command + [inputpath],
check=True,
text=True,
capture_output=True,
).stdout
except subprocess.CalledProcessError:
print(f"{RED}Crashed{RESET} on {inputpath}")
continue
if resultstr.strip() == solutionstr.strip():
print(f"{GREEN}Passed{RESET} {inputpath}")
elif resultstr.strip() == "":
print(f"{MAGENTA}No answer{RESET} for {inputpath}")
else:
print(f"{RED}Failed{RESET} {inputpath}")
subprocess.run([args.diff, solutionpath, "-"], input=resultstr, text=True)
if __name__ == "__main__":
main()