-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.py
135 lines (99 loc) · 3.99 KB
/
utils.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import shutil
import tempfile
def compare_outputs(code1_op, code2_op, result_file):
with open(code1_op) as code_file:
code1_op_lines = code_file.readlines()
with open(code2_op) as code_file:
code2_op_lines = code_file.readlines()
if len(code1_op_lines) != len(code2_op_lines):
print("Error - both output files dont have same no of lines")
return -1
res = open(result_file, 'w')
diff = 0
i = 0
for c1l, c2l in zip(code1_op_lines, code2_op_lines):
c1l = c1l.strip()
c2l = c2l.strip()
i += 1
if c1l != c2l:
res.write("-" * 10 + " Line - " + str(i) + " " + "-" * 10 + "\n")
res.write("code1 >" + "\n")
res.write(c1l + "\n")
res.write("code2 >" + "\n")
res.write(c2l + "\n")
res.write("\n")
diff += 1
res.close()
return diff
def write_stats(stats, report_file, single_file):
res = open(report_file, 'w')
stat_size = len(stats)
max_code1_time = max(stats,
key=lambda x: x['code1_time'])['code1_time']
min_code1_time = min(stats,
key=lambda x: x['code1_time'])['code1_time']
avg_code1_time = sum(s['code1_time'] for s in stats)/stat_size
res.write("-" * 10 + " Code1 File " + "-" * 10 + "\n")
res.write("Minimum time : " + str(min_code1_time) + " sec" + "\n")
res.write("Maximum time : " + str(max_code1_time) + " sec" + "\n")
res.write("Average time : " + str(avg_code1_time) + " sec" + "\n")
res.write("\n")
if not single_file:
max_code2_time = max(stats,
key=lambda x: x['code2_time'])['code2_time']
min_code2_time = min(stats,
key=lambda x: x['code2_time'])['code2_time']
avg_code2_time = sum(s['code2_time'] for s in stats)/stat_size
res.write("-" * 10 + " Code2 File " + "-" * 10 + "\n")
res.write("Minimum time : " + str(min_code2_time) + " sec" + "\n")
res.write("Maximum time : " + str(max_code2_time) + " sec" + "\n")
res.write("Average time : " + str(avg_code2_time) + " sec" + "\n")
res.write("\n")
is_wrong = False
res.write("-" * 10 + " Wrong Testcases " + "-" * 10 + "\n")
for i in range(stat_size):
if stats[i]['diff'] == -1:
res.write("Testcase " + str(i) + " produced invalid output"
+ "\n")
is_wrong = True
elif stats[i]['diff'] != 0:
res.write("Testcase " + str(i) + " different at "
+ str(stats[i]['diff']) + " positions" + "\n")
is_wrong = True
if not is_wrong:
res.write("No wrong testcase" + "\n")
res.close()
def copy_to_grp(i, filename, grptype, idxfile, idxlen):
sidx = filename.rfind('/') + 1
didx = filename.rfind('.')
if grptype == 1:
fldrnam = str(i).zfill(idxlen)
elif grptype == 2:
idxfile = True
fldrnam = filename[sidx:didx]
else:
fldrnam = str(i)
fileext = (str(i).zfill(idxlen) if idxfile else "") + ".txt"
if not os.path.exists(filename[:sidx] + fldrnam):
os.makedirs(filename[:sidx] + fldrnam)
new_filename = filename[:sidx] + fldrnam + '/'
new_filename += filename[sidx:didx] + fileext
shutil.copyfile(filename, new_filename)
return fldrnam
def create_folder(folder):
if not os.path.exists(folder):
os.makedirs(folder)
def delete_file(filename):
if os.path.exists(filename):
os.remove(filename)
def delete_folder(foldername):
if os.path.exists(foldername):
shutil.rmtree(foldername)
def delete_folders(basepath, folder_list):
for folder in folder_list:
delete_folder(basepath + "/" + folder)
def make_zip(folder):
tmpzip = tempfile.gettempdir() + "/" + folder
shutil.make_archive(tmpzip, 'zip', folder)
shutil.move(tmpzip + ".zip", folder + "/" + folder + ".zip")