-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_tests.py
executable file
·112 lines (91 loc) · 2.73 KB
/
make_tests.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
#!/usr/bin/python
# == Structure of files:
#
# problems/
# problem_name_1/
# tests/
# 01
# 01.a
# problem_name_2/
# tests/
# 01.in
# 01.out
# problem_name_3/
# 01
# 01.a
# make_correct_tests.py
#
# == Run
# $ ./make_correct_tests.py
#
# Written by Zhomart Mukhamejanov, 2013, KBTU
import os
import re
import shutil
import stat
if not os.path.exists('tests'):
os.mkdir('tests')
if not os.path.exists('checkers'):
os.mkdir('checkers')
def copy_java_checker(problem, the_file):
from_file = problem+'/'+the_file
to_file = 'checkers/Check' + problem.capitalize()+'.java'
exe_name = 'checkers/check_' + problem.lower()
# == REMOVE java keyword public
f = open(from_file)
source_code = f.read()
f.close()
class_name = 'Check%s' % problem.capitalize()
source_code = re.sub(r'(public)? *class\s*\S*Check\S*', 'public class %s' % class_name, source_code, re.I)
f = open(from_file, "w")
f.write(source_code)
f.close()
# == copy to checkers
shutil.copy(from_file, to_file)
# == generate sh
f = open(exe_name, "w")
f.write("#! /bin/sh\n" + ('exec java -cp `dirname $0`:`dirname $0`/testlib4j.jar ru.ifmo.testlib.CheckerFramework %s "$@"' % class_name))
# f.write("#! /bin/sh\n" + ('exec java -cp \"`dirname $0`;`dirname $0`\\*.jar\" %s "$@"' % class_name))
f.close()
# == add chmod +x
st = os.stat(exe_name)
os.chmod(exe_name, st.st_mode | stat.S_IEXEC)
def make_3(tdir, p):
if not os.path.exists(tdir): return
if tdir[-1:] != '/': tdir += '/'
for k in os.listdir(tdir):
t = re.match('^(input)?(\d{2,3})(\.tst$|\.txt$|\.dat$|$)', k, re.I)
if t:
tmp = t.group(2)
if len(tmp) == 2: tmp = '0'+ tmp
shutil.move(tdir+k, tdir+tmp)
t = re.match('^(answer|output)?(\d{2,3})(\.ans$|\.txt$|\.a$)', k, re.I)
if t:
tmp = t.group(2)
if len(tmp) == 2: tmp = '0'+ tmp
shutil.move(tdir+k, tdir+tmp+'.a')
if re.match('^\d{3}$', k) and not os.path.exists(tdir+k+'.a'): open(tdir+k+'.a','w').close()
if not os.path.exists('tests/'+p): os.mkdir('tests/'+p)
for f in os.listdir(tdir):
if re.match('^\d{3}(\.a)?$', f):
shutil.copy(tdir+f, 'tests/'+p+'/'+f)
t = re.match('check.*\.(\w{3})',f,re.I)
if t:
ext = t.group(1).lower()
if ext == 'dpr': ext = 'pas'
if ext in ('cpp', 'pas'):
shutil.copy(tdir+f, 'checkers/check_'+p.lower()+'.'+ext)
for f in os.listdir(p):
t = re.match('check.*\.(\w{1,4})',f,re.I)
if t:
ext = t.group(1).lower()
if ext == 'dpr': ext = 'pas'
if ext in ('cpp', 'pas', 'c'):
shutil.copy(p+'/'+f, 'checkers/check_'+p.lower()+'.'+ext)
if ext in ('java'):
copy_java_checker(p, f)
for d in os.listdir('.'):
if os.path.isdir(d) and d not in ('tests', 'checkers'):
dr = d
if os.path.exists(d+'/tests'): dr += '/tests'
make_3(dr, d)