box for other views
[can-benchmark.git] / gw-tests / genhtml / genhtml.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import os;
4 import dircache;
5 import sys;
6 import urllib
7
8 class DimValue:
9     def __init__(self, dim, value):
10         self.dim = dim
11         self.value = value
12         self.dim.addValue(self)
13     def __str__(self):
14         return self.dim.val2str(self.value)
15     def __repr__(self):
16         return "DimValue(%s, %s)" % (repr(self.dim), repr(self.value))
17     def htmlTableHeading(self):
18         return self.dim.htmlTableHeading(self.value)
19
20 class DimValues(list):
21     def replace(self, val):
22         for i in xrange(len(self)):
23             if self[i].dim == val.dim:
24                 self[i] = val
25     def __add__(self, val):
26         ret = DimValues(self)
27         ret.append(val)
28         return ret
29     def __sub__(self, dim):
30         result = DimValues(self)
31         for v in self:
32             if v.dim == dim:
33                 result.remove(v)
34         return result
35     def key(self):
36         return tuple([v.value for v in self])
37
38 class Dimension(dict):
39     def __init__(self, atype, name=None):
40         self.type = atype
41         if (name):
42             self.name = name
43         else:
44             self.name = atype
45         self.sortedKeys = []
46
47     def __iter__(self):
48         for i in xrange(len(self)):
49             yield self.getValue(i)
50     def getValue(self, index):
51         return self[self.sortedKeys[index]]
52
53     def addValue(self, *values):
54         for value in values:
55             if value not in self:
56                 if isinstance(value, DimValue):
57                     self[value.value] = value
58                 else:
59                     raise Exception("Unsupported usage of addValue")
60                     #self[value] = DimValue(self, value)
61         self.sortedKeys = self.keys()
62         self.sortedKeys.sort()
63     def val2str(self, v):
64         return str(v)
65     def htmlTableHeading(self, v):
66         return self.val2str(v)
67     def __str__(self):
68         return self.name
69     def __repr__(self):
70         return "Dimension(%s)"%self.type
71
72 class DimensionKern(Dimension):
73     def __init__(self):
74         Dimension.__init__(self, 'gwkern', 'GW kernel')
75     def htmlTableHeading(self, v):
76         i=v.find(":")
77         if i>0: kver=v[:i]
78         else: kver=v
79         return v+"<br><a href='config-%s'>config</a>"%(urllib.quote(kver))
80     def versions(self):
81         for v in self.values:
82             i=v.find(":")
83             if i>0: kver=v[:i]
84             else: kver=v
85             yield kver
86
87 class DimensionHostKern(Dimension):
88     def __init__(self):
89         Dimension.__init__(self, 'hostkern', 'Host kernel')
90     def val2str(self, v):
91         if v.find("host-") == 0:
92             v = v[5:]
93         return v
94     def htmlTableHeading(self, v):
95         v = self.val2str(v)
96         i = v.find(":")
97         if i>0: kver = v[:i]
98         else: kver = v
99         return v+"<br><a href='config-%s'>config</a>"%(urllib.quote(kver))
100     def versions(self):
101         for v in self.values:
102             i=v.find(":")
103             if i>0: kver=v[:i]
104             else: kver=v
105             yield kver
106
107 class DimensionTest(Dimension):
108     def __init__(self):
109         Dimension.__init__(self, 'test', 'Test')
110     def htmlTableHeading(self, v):
111         return v+"<br><a href='%s.sh.html'>source</a>"%(urllib.quote(v))
112
113 class DimensionTraffic(Dimension):
114     def __init__(self):
115         Dimension.__init__(self, 'traf', 'Traffic')
116     def val2str(self, v):
117         if v == "50":
118             return "50%"
119         elif v == "oneatatime":
120             return "one message at a time"
121         else:
122             return v
123     def htmlTableHeading(self, v):
124         return self.val2str(v)
125 class Test:
126     @classmethod
127     def isOnPath(cls, path):
128         f = os.path.join(path, '.results')
129         return os.path.isfile(f)
130     def __init__(self, path, values):
131         self.path = path
132         self.name = os.path.basename(path)
133         self.values = values
134     def printThumbLink(self, file):
135         thumb = self.path+'/thumb'
136         try:
137             imgs = [img for img in dircache.listdir(thumb)]
138         except OSError:
139             imgs = [ self.name + ".png" ]
140         for img in imgs:
141             print >>file, "<a href='%s/results.html'><img src='%s/thumb/%s'></a>" % \
142                   (urllib.quote(self.path), urllib.quote(self.path), img)
143     def generateHtml(self):
144         html = open(os.path.join(path, 'results.html'), "w")
145         print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
146 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
147 <head>
148 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
149 <title>%s</title>
150 <link rel="stylesheet" href="style.css" type="text/css" /> 
151 </head>
152 <body>
153 <h1>%s</h1>"""  % (title, title)
154         params = ["%s %s" % (v.dim, v) for v in self.vals]
155         print >>html, "Results for:", ", ".join(params), "<hr />"
156         for d in self.dimOther:
157             print >>html, "%s: " % d
158             print >>html, "<a href='%s'>X axis</a>, " % \
159                 Page((self.dimy, d), self.valsOther - d + self.dimx.getValue(0), self.tests).getName()
160             print >>html, "<a href='%s'>Y axis</a>;&nbsp;&nbsp;" % \
161                 Page((d, self.dimx), self.valsOther - d + self.dimy.getValue(0), self.tests).getName()
162             links = []
163             for v in d.values():
164                 if v in self.valsOther:
165                     links.append(str(v))
166                 else:
167                     vv = DimValues(self.valsOther)
168                     vv.replace(v)
169                     links.append("<a href='%s'>%s</a>"%(urllib.quote(Page((self.dimy, self.dimx), vv, self.tests).getName()), str(v)))
170             print >>html, " | ".join(links)
171             print >>html, "<br>"
172             try:
173                 print >>html, d.htmlPreamble()
174             except Exception:
175                 pass
176
177         
178         html.close()
179
180 class Space(list):
181     """List of Dimensions()s (order matters)"""
182     def __init__(self, *dimensions):
183         self.extend(list(dimensions))
184     def iterValues(self):
185         idx = [0 for i in xrange(len(self))]
186         done=False
187         while not done:
188             values=DimValues()
189             for i in xrange(len(self)):
190                 values.append(self[i].values()[idx[i]])
191             yield values
192             done=True
193             for i in xrange(len(self)):
194                 idx[i] += 1
195                 if idx[i] < len(self[i]):
196                     done=False
197                     break
198                 idx[i] = 0
199     def reorder(self, dimValues):
200         reordered = DimValues()
201         for d in self:
202             for v in dimValues:
203                 if v.dim == d:
204                     reordered.append(v)
205         return reordered
206     def iterDimensionPairs(self):
207         for i in xrange(len(self)):
208             for j in xrange(i+1, len(self)):
209                 yield (self[i], self[j])
210                 yield (self[j], self[i])
211     def iterRemainingDimensions(self, dimensionPair):
212         for d in self:
213             if d not in dimensionPair:
214                 yield d
215
216
217 class Tests(dict):
218     """Represents all tests organized along several dimensions"""
219     def __init__(self, rootpath, space):
220         dict.__init__(self)
221         self.space = space
222         if (rootpath):
223             self.populate(rootpath)
224     def getTest(self, key):
225         if len(key) != len(self.space):
226             raise KeyError("The coordinates in key do not match the dimension of the space")
227         realkey = self.space.reorder(key)
228         return self[realkey.key()]
229
230     def addTest(self, test):
231         self[test.values.key()] = test
232         
233     def populate(self, rootpath):
234         for root, dirs, files in os.walk(rootpath):
235             if (root.find(rootpath) == 0):
236                 coordinates = root[len(rootpath):].split("/")
237             else:
238                 coordinates = rootpath.split("/")
239             if Test.isOnPath(root):
240                 if len(coordinates) != len(self.space):
241                     raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
242                 dv = DimValues([DimValue(self.space[i], coordinates[i]) for i in xrange(len(coordinates))])
243                 self.addTest(Test(root, dv))
244     def generateHtml(self):
245         for pair in self.space.iterDimensionPairs():
246             remDims = Space(*tuple([d for d in self.space.iterRemainingDimensions(pair)]))
247             for vals in remDims.iterValues():
248                 page = Page(pair, vals, self)
249                 page.generate()
250         try:
251             os.remove("index.html")
252         except OSError: pass
253         os.symlink(page.getName(), "index.html")
254         css = open("style.css", "w")
255         print >>css, """img { border: 0; }
256 table { border-collapse: collapse; }
257 th, td { border: 1px solid lightgray; padding: 4px;}
258 h4 { margin: 0; }
259 .box { border: 1px solid black; padding: 1ex; margin: 1ex 0}
260 """
261         css.close()
262
263         #os.system("source-highlight -d --output-dir=. ../*.sh")
264
265 class Page:
266     def __init__(self, dimPair, valsOther, tests):
267         self.dimy, self.dimx = dimPair
268         self.dimOther = [v.dim for v in valsOther]
269         self.valsOther = tests.space.reorder(valsOther)
270         self.tests = tests
271     def getName(self):
272         return "%s-vs-%s-for-%s.html"%(self.dimy.type, self.dimx.type,
273                                        "-".join([v.value for v in self.valsOther]))
274     def generate(self):
275         html = open(self.getName(), "w")
276         title = "CAN gateway timing analysis" 
277         print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
278 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
279 <head>
280 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
281 <title>%s</title>
282 <link rel="stylesheet" href="style.css" type="text/css" /> 
283 </head>
284 <body>
285 <h1>%s</h1>"""  % (title, title)
286         params = ["%s %s" % (v.dim, v) for v in self.valsOther]
287         print >>html, "<h3>Results for ", ", ".join(params), "</h3>"
288         print >>html, "<div class='box'><h4>Other views</h4>"
289         for d in self.dimOther:
290             print >>html, "%s: " % d
291             print >>html, "<a href='%s'>X axis</a>, " % \
292                 Page((self.dimy, d), self.valsOther - d + self.dimx.getValue(0), self.tests).getName()
293             print >>html, "<a href='%s'>Y axis</a>;&nbsp;&nbsp;" % \
294                 Page((d, self.dimx), self.valsOther - d + self.dimy.getValue(0), self.tests).getName()
295             links = []
296             for v in d.values():
297                 if v in self.valsOther:
298                     links.append(str(v))
299                 else:
300                     vv = DimValues(self.valsOther)
301                     vv.replace(v)
302                     links.append("<a href='%s'>%s</a>"%(urllib.quote(Page((self.dimy, self.dimx), vv, self.tests).getName()), str(v)))
303             print >>html, " | ".join(links)
304             print >>html, "<br>"
305             try:
306                 print >>html, d.htmlPreamble()
307             except Exception:
308                 pass
309         print >>html, "</div>"
310
311         print >>html, "<table><thead><tr><td>%s &rarr; <br />%s &darr;</td>" % (self.dimx.name, self.dimy.name)
312         for x in self.dimx:
313             print >>html, "<th>%s</th>" % x.htmlTableHeading()
314         print >>html, "</tr></thead>"
315         for y in self.dimy:
316             print >>html, "<tr><th>%s</th>" % y.htmlTableHeading()
317
318             for x in self.dimx:
319                 print >>html, "<td>"
320                 idx = [x,y]
321                 idx.extend(self.valsOther)
322                 try:
323                     test = tests.getTest(idx)
324                     test.printThumbLink(html)
325                 except KeyError:
326                     print >>html, "N/A"
327                 print >>html, "</td>"
328             print >>html, "</tr>"
329         print >> html, """
330 </table>
331 <div style="font-size: small; color: gray; margin-top: 1em;">Authors: Michal Sojka, Pavel Píša, Copyright © 2010 Czech Technical University in Prague</div>
332 </body>
333 """
334
335
336 if __name__ == "__main__":
337     os.chdir(sys.argv[1])
338     os.system("rm *.html")
339     tests = Tests("./", Space(DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionTest()))
340     tests.generateHtml()
341     sys.exit(0)