genhtml: Correct navigation between pages
[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):
131         self.path = path
132         self.name = os.path.basename(path)
133     def printThumbLink(self, file):
134         thumb = self.path+'/thumb'
135         try:
136             imgs = [img for img in dircache.listdir(thumb)]
137         except OSError:
138             imgs = [ self.name + ".png" ]
139         for img in imgs:
140             print >>file, "<a href='%s/%s'><img src='%s/thumb/%s'></a>" % \
141                   (urllib.quote(self.path), img, urllib.quote(self.path), img)
142
143 class Space(list):
144     """List of Dimensions()s (order matters)"""
145     def __init__(self, *dimensions):
146         self.extend(list(dimensions))
147     def iterValues(self):
148         idx = [0 for i in xrange(len(self))]
149         done=False
150         while not done:
151             values=DimValues()
152             for i in xrange(len(self)):
153                 values.append(self[i].values()[idx[i]])
154             yield values
155             done=True
156             for i in xrange(len(self)):
157                 idx[i] += 1
158                 if idx[i] < len(self[i]):
159                     done=False
160                     break
161                 idx[i] = 0
162     def reorder(self, dimValues):
163         reordered = DimValues()
164         for d in self:
165             for v in dimValues:
166                 if v.dim == d:
167                     reordered.append(v)
168         return reordered
169     def iterDimensionPairs(self):
170         for i in xrange(len(self)):
171             for j in xrange(i+1, len(self)):
172                 yield (self[i], self[j])
173                 yield (self[j], self[i])
174     def iterRemainingDimensions(self, dimensionPair):
175         for d in self:
176             if d not in dimensionPair:
177                 yield d
178
179
180 class Tests(dict):
181     """Represents all tests organized along several dimensions"""
182     def __init__(self, rootpath, space):
183         dict.__init__(self)
184         self.space = space
185         if (rootpath):
186             self.populate(rootpath)
187     def getTest(self, key):
188         if len(key) != len(self.space):
189             raise KeyError("The coordinates in key do not match the dimension of the space")
190         realkey = self.space.reorder(key)
191         return self[realkey.key()]
192
193     def addTest(self, test, coordinates):
194         if len(coordinates) != len(self.space):
195             raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
196         self[tuple(coordinates)] = test
197         for i in xrange(len(coordinates)):
198             DimValue(self.space[i], coordinates[i])
199
200     def populate(self, rootpath):
201         for root, dirs, files in os.walk(rootpath):
202             if (root.find(rootpath) == 0):
203                 coordinates = root[len(rootpath):]
204             else:
205                 coordinates = rootpath
206             if Test.isOnPath(root):
207                 self.addTest(Test(root), coordinates.split("/"))
208     def generateHtml(self):
209         for pair in self.space.iterDimensionPairs():
210             remDims = Space(*tuple([d for d in self.space.iterRemainingDimensions(pair)]))
211             for vals in remDims.iterValues():
212                 page = Page(pair, vals, self)
213                 page.generate()
214         try:
215             os.remove("index.html")
216         except OSError: pass
217         os.symlink(page.getName(), "index.html")
218
219         #os.system("source-highlight -d --output-dir=. ../*.sh")
220
221 class Page:
222     def __init__(self, dimPair, valsOther, tests):
223         self.dimy, self.dimx = dimPair
224         self.dimOther = [v.dim for v in valsOther]
225         self.valsOther = tests.space.reorder(valsOther)
226         self.tests = tests
227     def getName(self):
228         return "%s-vs-%s-for-%s.html"%(self.dimy.type, self.dimx.type,
229                                        "-".join([v.value for v in self.valsOther]))
230     def generate(self):
231         html = open(self.getName(), "w")
232         title = "CAN gateway timing analysis" 
233         print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
234 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
235 <head>
236 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
237 <title>%s</title>
238 <style>
239 img { border: 0; }
240 table { border-collapse: collapse; }
241 th, td { border: 1px solid lightgray; padding: 4px;}
242 </style>
243 </head>
244 <body>
245 <h1>%s</h1>"""  % (title, title)
246         params = ["%s %s" % (v.dim, v) for v in self.valsOther]
247         print >>html, "Results for:", ", ".join(params), "<hr />"
248         for d in self.dimOther:
249             print >>html, "%s: " % d
250             print >>html, "<a href='%s'>X axis</a>, " % \
251                 Page((self.dimy, d), self.valsOther - d + self.dimx.getValue(0), self.tests).getName()
252             print >>html, "<a href='%s'>Y axis</a>;&nbsp;&nbsp;" % \
253                 Page((d, self.dimx), self.valsOther - d + self.dimy.getValue(0), self.tests).getName()
254             links = []
255             for v in d.values():
256                 if v in self.valsOther:
257                     links.append(str(v))
258                 else:
259                     vv = DimValues(self.valsOther)
260                     vv.replace(v)
261                     links.append("<a href='%s'>%s</a>"%(urllib.quote(Page((self.dimy, self.dimx), vv, self.tests).getName()), str(v)))
262             print >>html, " | ".join(links)
263             print >>html, "<br>"
264             try:
265                 print >>html, d.htmlPreamble()
266             except Exception:
267                 pass
268
269         print >>html, "<table><thead><tr><td>%s &rarr; <br />%s &darr;</td>" % (self.dimx.name, self.dimy.name)
270         for x in self.dimx:
271             print >>html, "<th>%s</th>" % x.htmlTableHeading()
272         print >>html, "</tr></thead>"
273         for y in self.dimy:
274             print >>html, "<tr><th>%s</th>" % y.htmlTableHeading()
275
276             for x in self.dimx:
277                 print >>html, "<td>"
278                 idx = [x,y]
279                 idx.extend(self.valsOther)
280                 try:
281                     test = tests.getTest(idx)
282                     test.printThumbLink(html)
283                 except KeyError:
284                     print >>html, "N/A"
285                 print >>html, "</td>"
286             print >>html, "</tr>"
287         print >> html, """
288 </table>
289 <div style="font-size: small; color: gray; margin-top: 1em;">Authors: Michal Sojka, Pavel Píša, Copyright © 2010 Czech Technical University in Prague</div>
290 </body>
291 """
292
293
294 if __name__ == "__main__":
295     os.chdir(sys.argv[1])
296     os.system("rm *.html")
297     tests = Tests("./", Space(DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionTest()))
298     tests.generateHtml()
299     sys.exit(0)