summaryrefslogtreecommitdiff
path: root/tests/ctest_pretty_print.py
blob: 0b81f985f249f6ae5d67e358ae9b09e2798f8dd9 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/python
import string,platform,sys,re,os

wiki_basepath = 'OpenSCAD'
logfilename = 'LastTest.log'
builddir = '.'
logpath = os.path.join(builddir,'Testing','Temporary',logfilename)
NO_END = False
if logfilename.endswith('.tmp'): NO_END = True

def read_sysinfo():
	try:
		f=open('sysinfo.txt')
	except:
		return ''
	data=f.read()
	machine_str, osplain_str, renderer_str = '','',''
	machine = re.search('Machine:(.*?)\n',data)
	osinfo = re.search('OS info:(.*?)\n',data)
	renderer = re.search('GL Renderer:(.*?)\n',data)
	if machine: machine_str = machine.group(1).strip()
	if osinfo: osplain_str = osinfo.group(1).strip().split(' ')[0].strip()
	if renderer: 
		tmp = renderer.group(1).strip().split(' ')
		renderer_str = string.join(tmp[0:3],'-')
	platform = osplain_str + '_' + machine_str + '_' + renderer_str
	platform = platform.lower()
	return data, platform

def readlog():
	try:
		print 'reading',logpath
		f = open(logpath)
	except:
		print 'couldnt open ',logpath
		return None

	data = f.read()
	return data

class Test:
	def __init__(self,fullname,time,passed,output,type,outputfile,expectedfile):
		self.fullname,self.time,self.passed,self.output = \
			fullname, time, passed, output
		self.type = type
		self.outputfile = outputfile
		self.expectedfile = expectedfile

	def __str__(self):
		x = 'fullname: ' + self.fullname
		x+= '\noutputfile: ' + self.outputfile
		x+= '\nexpectedfile: ' + self.expectedfile
		x+= '\ntesttime: ' + self.time
		x+= '\ntesttype: ' + self.type
		x+= '\npassfail: ' + self.passfail
		x+= '\noutput: \n' + self.output
		x+= '\n'
		return x

def gettest_strings(data):
	chunks = data.split('----------------------------------------------------------')
	print 'read',len(chunks), 'chunks'
	if NO_END:
		enddate = 'n/a (cancelled)'
		chunks.pop()
	else:
		enddate = chunks.pop().replace('End testing: ','').strip()
	chunks.reverse()
	startdate = chunks.pop().replace('Start testing: ','').strip()
	chunks.reverse()
	tests=[]
	chunksize = 3
	for i in range(0,len(chunks),chunksize):
		testchunks = chunks[i:i+chunksize]
		test = string.join(testchunks,'-----')
		tests += [test]
		#print '----------<<<<<<<<<<<<<<<<'
		#print test
		#print '----------<<<<<<<<<<<<<<<<'
		test = ''
	sysinfo, platform = read_sysinfo()
	return startdate, tests, enddate, sysinfo, platform

def parsetest(teststring):
	s = teststring
	def regex(pat,str):
		x=re.search(pat,str,re.DOTALL|re.MULTILINE)
		if x: 
			if len(x.groups())>0: 
				return x.group(1).strip()
		return ''
	testfullname = regex("Test:(.*?)\n",s)
	testtime = regex("Test time =(.*?)\n",s).replace(' sec','')
	passfail = regex("Test time.*?Test (Passed)",s)
	command = regex("Command:(.*?)\n",s)
	tmp = command.split(' "')
	testtype = ''
	try:
		testtype = tmp[3].strip('"')
	except:
		print 'failed to parse log', teststring
	goodimg = regex("expected image:(.*?)\n",s)
	actualimg = regex("actual image:(.*?)\n",s)
	if passfail=='Passed': passed = True
	else: passed = False
	output = regex("Output:(.*?)<end of output>",s).replace('-----','')
	test = Test(testfullname, testtime, passed, output, testtype, actualimg, goodimg )
	return test

def parse(data):
	startdate, test_strs, enddate, sysinfo, platform = gettest_strings(data)
	print 'found', len(test_strs),'test results'
	tests = []
	for i in range(len(test_strs)):
		test = parsetest(test_strs[i])
		tests += [test]
	return startdate, tests, enddate, sysinfo, platform

def towiki(startdate, tests, enddate, sysinfo, platform):
	def convert_path(fulltestname,platform,path):
		# convert system path name (image file) to wiki path name
		testprogram = fulltestname[0:fulltestname.find('_')]
		testprogram = testprogram.replace('test','')
		filename = os.path.basename(path)
		filename = filename.replace('-actual','')
		filename = filename.replace('-tests','')
		filename = filename.replace('-expected','')
		try:
			platform = platform[0].upper() + platform[1:]
		except:
			platform = 'error'
		newpath = testprogram + '_' + filename
		# must use _ not / b/c of wikinet.org weird name mangling
		newpath = wiki_basepath + '_' + platform + '_' + newpath
		return newpath
		
	x='''
<h3>OpenSCAD test run</h3>

platform: PLATFORM

detailed system info: 
<pre>
SYSINFO
</pre>

runtime: STARTDATE to ENDDATE

Failed tests

{|TABLESTYLE
! Testname !! expected output !! actual output
|-
| FTESTNAME || [[File:EXPECTEDIMG|thumb|250px]] || [[File:ACTUALIMG|thumb|250px]]
|}

Passed tests

{|TABLESTYLE 
! Testname 
|-
| PTESTNAME
|}

'''

	repeat1='''
|-
| FTESTNAME || [[File:EXPECTEDIMG|thumb|250px]] || [[File:ACTUALIMG|thumb|250px]]
'''

	repeat2='''
|-
| PTESTNAME
'''

	x = x.replace('TABLESTYLE','border=1 cellspacing=0 cellpadding=1 align="center"')
	x = x.replace('STARTDATE',startdate)
	x = x.replace('ENDDATE',enddate)
	x = x.replace('SYSINFO',sysinfo)
	x = x.replace('PLATFORM',platform)

	for t in tests:
		if t.passed:
			tmp = str(repeat2)
			tmp = tmp.replace('PTESTNAME',t.fullname)
			x = x.replace(repeat2,tmp + repeat2)
		else:
			tmp = str(repeat1)
			tmp = tmp.replace('FTESTNAME',t.fullname)
			wiki_imgpath1 = convert_path(t.fullname,'expected',t.expectedfile)
			tmp = tmp.replace('EXPECTEDIMG',wiki_imgpath1)
			wiki_imgpath2 = convert_path(t.fullname,platform,t.outputfile)
			tmp = tmp.replace('ACTUALIMG',wiki_imgpath2)
			x = x.replace(repeat1,tmp + repeat1)

	x = x.replace(repeat1,'')
	x = x.replace(repeat2,'')
	return x

def wikitohtml(data):
	# not pretty 
	data = data.replace('\n\n','\n<p>\n')
	data = re.sub('\{\|.*?\n','<table border=1>\n',data)
	data = re.sub('\n\! ','\n<tr>\n<td>',data)
	data = data.replace(' !! ','<td>')
	data = data.replace('|-','<tr>')
	data = re.sub('\n\| ','\n<td>',data)
	data = data.replace(' || ','<td>')
	data = data.replace('|}','\n</table>')
	data = re.sub('[[File:(.*?)|.*?]]','<img src="(\1)">',data)
	return data

def testsort(tests):
	passed = []
	failed = []
	for t in tests:
		if t.passed: passed+=[t]
		else: failed +=[t]
	return failed+passed

def save(data,filename):
	try:
		f=open(filename,'w')
	except:
		print 'couldnt open ',filename, 'for writing'
		return None
	print 'writing',len(data),'bytes to',filename
	f.write(data)
	f.close()

def main():
	data = readlog()
	startdate, tests, enddate, sysinfo, platform = parse(data)
	tests = testsort(tests)
	wikidata = towiki(startdate, tests, enddate, sysinfo, platform)
	htmldata = wikitohtml(wikidata) 
	save(wikidata, platform+'.wiki')
	save(htmldata, platform+'.html')

main()

contact: Jan Huwald // Impressum