-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (100 loc) · 3.92 KB
/
Copy pathmain.py
File metadata and controls
114 lines (100 loc) · 3.92 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import subprocess as sp
import time
class Test:
def __init__(self, tl_time, ml_memory):
self.tl_time = tl_time # time in sec
self.ml_memory = ml_memory * 1024 # in kB
def create_file(self, text, output_file_name):
with open(output_file_name, 'w') as inp:
print(text)
inp.write(text)
inp.close()
def delete_file(self, path):
os.remove(path)
def compile_С(self, name_file, out_name="a.out"):
proc = sp.Popen(["g++", "-std=c++17", name_file, "-o", out_name],
stdout=sp.PIPE, stderr=sp.PIPE)
output, err = proc.communicate()
if output == b'' and err == b'':
return True
else:
return False
def compile_pas(self, name_file, out_name="a"):
proc = sp.Popen(['fpc', "-TLINUX", name_file, f'-o{out_name}'],
stdout=sp.PIPE, stderr=sp.PIPE)
output, err = proc.communicate()
print(output, err)
if (err == b'' or
err == b'/usr/bin/ld.bfd: warning: programms/link.res contains output sections;'
b' did you forget -T?\n') and 'compiled' in output.decode():
return True
else:
return False
def run_all_tests(self, tests_dir, file_name="a.out"):
for n, file in enumerate(sorted(filter(lambda x: not x.endswith(".a"),
os.listdir(tests_dir)), key=lambda x: int(x))):
status = self.run_one_test(f"{tests_dir}/{file}", f"{tests_dir}/{file}.a", file_name)
if status:
return status, n
return False
def mem(self, pid): # returns mem in kB
try:
col_mem = sp.check_output([f"cat /proc/{pid}/status | grep -i VMSIZE"],
shell=True).rstrip()
return int(col_mem.decode()[11:-3])
except BaseException:
return 0 # todo: refactor it
def get_ans(self, ans_file):
with open(ans_file, 'r') as content_file:
return content_file.read().rstrip()
def run_one_test(self, test_file, ans_file, file_name):
with open(test_file) as inp:
tl = False
ml = False
proc = sp.Popen([f'exec ./{file_name}'], shell=True,
stdin=inp, stdout=sp.PIPE, stderr=sp.PIPE)
time_start = time.time()
while True:
if proc.poll() is None and time.time() - time_start >= self.tl_time:
tl = True
proc.kill()
break
elif self.mem(proc.pid) >= self.ml_memory:
ml = True
proc.kill()
break
elif proc.poll() is not None:
break
if tl:
return "TL"
if ml:
return "ML"
output, err = proc.communicate()
ret = proc.returncode
if ret != 0:
return "RE"
print([output.decode().rstrip(), self.get_ans(ans_file)])
if output.decode().rstrip() == self.get_ans(ans_file):
return False # if all ok return false :) NICE ))
return "WA"
if __name__ == "__main__":
test = Test(1, 16)
# test.create_file("""#include <iostream>
# using namespace std;
# int main(){
# int a, b;
# cin >> a >> b;
# cout << a + b;
# return 0;
# }""", "text.cpp")
# start_time = time.time()
# print(test.compile_pas("source/36.pas"))
# print(test.run_all_tests("tests", "out"))
# print(time.time() - start_time)
# print([test.get_ans('tests/0.a')])
# print(test.mem("23229"))
# print(test.run_one_test("tests/0", 'tests/0.a', 'a.out'))
# print(test.run_all_tests("tests"))
# print(test.run_test("tests/0", "tests/0.a"))
# test.compile("main.cpp")