-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path_optimize_compiler.py
112 lines (103 loc) · 3.26 KB
/
_optimize_compiler.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
#coding=utf-8
import sys
if sys.version[0]=='2':
os.system('start c:/python34/python.exe '+sys.argv[0])
sys.exit(-1)
import subprocess
import os
import hashlib
def buildraw(compiler):
lines=[]
hllines=[]
#interpret
for data in profile_hl.split('\n'):
if data:
splited=data.split()
if len(splited)==2:
hllines.append(splited)
elif len(splited)==1:
lines.append('"%s"[color="black",fillcolor="greenyellow",style="bold,filled"];\n'%\
splited[0].replace('\\','\\\\').replace('"','\\"'))
else:
raise SyntaxError('qipa highlight')
for data in profile_text.split('\n'):
if not data:
continue
splited=data.split()
for now in range(len(splited)):
splited[now]=splited[now].replace('\\','\\\\').replace('"','\\"')
if len(splited)==3:
if [splited[0],splited[1]] in hllines:
lines.append('"%s"->"%s"[label="%s",color="red",style="bold,filled"];\n'\
%tuple(splited))
else:
lines.append('"%s"->"%s"[label="%s"];\n'%tuple(splited))
elif len(splited)==2:
if [splited[0],splited[1]] in hllines:
lines.append('"%s"->"%s"[color="red",style="bold,filled"];\n'%tuple(splited))
else:
lines.append('"%s"->"%s";\n'%tuple(splited))
else:
raise SyntaxError('qipa text')
#write
if not os.path.exists('output'):
os.mkdir('output')
try:
with open('output/out.gv','w') as f:
f.write('digraph A{\n')
for a in lines:
f.write(a)
f.write('}')
except Exception as e:
raise ZeroDivisionError('cannot write file')
#build
if not os.path.isfile('compiler/%s.exe'%compiler):
raise ZeroDivisionError('no compiler')
exe=subprocess.Popen(
'"compiler/%s.exe" output/out.gv -o output/out.png -Tpng'%compiler,
shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout,stderr=exe.communicate()
code=exe.wait()
if code!=0:
raise ZeroDivisionError('build failed')
#judge
if not os.path.isfile('output/out.png'):
raise ZeroDivisionError('no out file')
with open('output/out.png','rb') as f:
return hashlib.md5(f.read()).hexdigest()
compilers=['circo','dot','fdp','sfdp']
profile_hl='''
1
2
3 4
5 6
'''
profile_text='''
1 2 1to2
2 3
3 4 3to4
4 5 4to5
!@#$%^&*() -_=+[]{};:\'",.<>/?
5 6 5to6
6 1
'''
print('--- Calculating md5...')
correct_md5={}
correct_md5['sfdp']=buildraw('sfdp')
correct_md5['fdp']=buildraw('fdp')
correct_md5['dot']=buildraw('dot')
correct_md5['circo']=buildraw('circo')
fnum=len(os.listdir('compiler'))
for ind,file in enumerate(os.listdir('compiler')):
print('--- %d/%d: %s...'%(ind+1,fnum,file))
os.rename('compiler/'+file,'compiler/'+file+'__')
try:
for c in compilers:
if buildraw(c)!=correct_md5[c]:
raise ZeroDivisionError('wrong md5')
except ZeroDivisionError as e:
os.rename('compiler/'+file+'__','compiler/'+file)
print(' ! %s'%e)
else:
print(' ^ Deleted')
os.remove('compiler/'+file+'__')