-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge.py
72 lines (68 loc) · 2.77 KB
/
judge.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
import os, sys, subprocess
file = sys.argv[1] if len(sys.argv) > 1 else input('Filename?')
extension = file.split('.')[-1].lower() if '.' in file else ''
language = extension if extension in ['py', 'java', 'class', 'cpp', 'exe'] else input('Language?').lower()
problem = input('Problem?')
def get_args():
global file
if language in ['python', 'py', 'python3', 'py3']:
return ['py', file]
if language in ['java']:
subprocess.run(['javac', file], capture_output=True, text=True, check=True)
if language in ['java', 'class']:
return ['java', os.path.splitext(os.path.basename(file))[0]]
if language in ['cpp', 'c++']:
subprocess.run(['g++', file], capture_output=True, text=True, check=True)
file = 'a.exe'
if language in ['cpp', 'c++', 'app', 'exe', 'executable']:
return [file]
verdict = 'Something went wrong'
for i in os.listdir('.'):
if i.startswith(str(problem).zfill(2)):
if not os.path.isdir('!LOGS'):
os.mkdir('!LOGS')
for j in os.listdir('!LOGS'):
os.remove(f'!LOGS/{j}')
verdict = 'Verdict: '
try:
args = get_args()
except subprocess.CalledProcessError as e:
verdict = 'C'
with open(f'!LOGS/err.err', 'w') as f:
f.write(e.stderr)
break
if not args:
verdict = 'Something went wrong'
break
for j in range(10):
with open(f'{i}/{j}.txt') as f:
process = subprocess.run(['py', f'{i}/main.py'], capture_output=True, text=True, input=f.read())
a = [k.strip() for k in process.stdout.splitlines() if k.strip()]
with open(f'!LOGS/{j}.txt', 'w') as g:
g.write(process.stdout)
f.seek(0)
v = 'W'
process = None
try:
process = subprocess.run(args, capture_output=True, text=True, input=f.read(), timeout=10, check=True)
except subprocess.TimeoutExpired as e:
v = 'T'
process = e
except (subprocess.CalledProcessError, IndexError) as e:
v = 'E'
process = e
b = None
try:
b = [k.strip() for k in (process.stdout or '').splitlines() if k.strip()]
with open(f'!LOGS/{j}.out', 'w') as g:
g.write(process.stdout or '')
with open(f'!LOGS/{j}.err', 'w') as g:
g.write(process.stderr or '')
except AttributeError as e:
pass
if a == b:
v = 'A'
verdict += v
break
print(verdict)
input()