Update genhtml etc.
[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     def __repr__(self):
13         return repr(self.value)
14     def htmlLabel(self):
15         return self.dim.htmlLabel(self.value)
16
17 class Dimension(dict):
18     def __init__(self, atype, name=None):
19         self.type = atype
20         if (name):
21             self.name = name
22         else:
23             self.name = atype
24
25     def __iter__(self):
26         keys = self.keys()
27         keys.sort()
28         for k in keys:
29             yield self[k]
30
31     def addValue(self, *values):
32         for value in values:
33             if value not in self:
34                 self[value] = DimValue(self, value)
35     def htmlLabel(self, v):
36         return v
37     def __str__(self):
38         return self.name
39     def __repr__(self):
40         return self.type
41
42 class DimensionKern(Dimension):
43     def __init__(self):
44         Dimension.__init__(self, 'gwkern', 'GW kernel')
45     def htmlLabel(self, v):
46         i=v.find(":")
47         if i>0: kver=v[:i]
48         else: kver=v
49         return v+"<br><a href='config-%s'>config</a>"%(urllib.quote(kver))
50     def versions(self):
51         for v in self.values:
52             i=v.find(":")
53             if i>0: kver=v[:i]
54             else: kver=v
55             yield kver
56
57 class DimensionHostKern(Dimension):
58     def __init__(self):
59         Dimension.__init__(self, 'hostkern', 'Host kernel')
60     def htmlLabel(self, v):
61         if v.find("host-") == 0:
62             v = v[5:]
63         # TODO: remove host- prefix
64         i = v.find(":")
65         if i>0: kver = v[:i]
66         else: kver = v
67         return v+"<br><a href='config-%s'>config</a>"%(urllib.quote(kver))
68     def versions(self):
69         for v in self.values:
70             i=v.find(":")
71             if i>0: kver=v[:i]
72             else: kver=v
73             yield kver
74
75 class DimensionTest(Dimension):
76     def __init__(self):
77         Dimension.__init__(self, 'test', 'Test')
78     def htmlLabel(self, v):
79         return v+"<br><a href='%s.sh.html'>source</a>"%(urllib.quote(v))
80
81 class DimensionTraffic(Dimension):
82     def __init__(self):
83         Dimension.__init__(self, 'traf', 'Traffic')
84     def htmlLabel(self, v):
85         return v
86
87 class Test:
88     @classmethod
89     def isOnPath(cls, path):
90         f = os.path.join(path, '.results')
91         return os.path.isfile(f)
92     def __init__(self, path):
93         self.path = path
94     def printThumbLink(self, file):
95         print self.path
96         for img in dircache.listdir(self.path+'/thumb'):
97             print >>file, "<a href='%s/%s'><img src='%s/thumb/%s'></a>" % \
98                 (urllib.quote(self.path), img, urllib.quote(self.path), img)
99
100 def iterDimValues(dimensions):
101     idx = [0 for i in xrange(len(dimensions))]
102     done=False
103     while not done:
104         values=[]
105         for i in xrange(len(dimensions)):
106             values.append(dimensions[i].values()[idx[i]])
107         yield values
108         done=True
109         for i in xrange(len(dimensions)):
110             idx[i] += 1
111             if idx[i] < len(dimensions[i]):
112                 done=False
113                 break
114             idx[i] = 0
115
116 class Tests(dict):
117     """Represents all tests organized along several dimensions"""
118     def __init__(self, rootpath, *dimensions):
119         dict.__init__(self)
120         self.dimensions = dimensions
121         if (rootpath):
122             self.populate(rootpath)
123     def getTest(self, key):
124         realkey=[]
125         for d in self.dimensions:
126             for i in key:
127                 if i.dim == d:
128                     realkey.append(i.value)
129         if len(realkey) != len(self.dimensions):
130             raise KeyError("The coordinates in key do not match dimensions")
131         return self[tuple(realkey)]
132
133     def addTest(self, test, coordinates):
134         if len(coordinates) != len(self.dimensions):
135             raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
136         self[tuple(coordinates)] = test
137         for i in xrange(len(coordinates)):
138             self.dimensions[i].addValue(coordinates[i])
139
140     def populate(self, rootpath):
141         for root, dirs, files in os.walk(rootpath):
142             if (root.find(rootpath) == 0):
143                 coordinates = root[len(rootpath):]
144             else:
145                 coordinates = rootpath
146             if Test.isOnPath(root):
147                 self.addTest(Test(root), coordinates.split("/"))
148     def iterDimensionPairs(self):
149         for i in xrange(len(self.dimensions)):
150             for j in xrange(i+1, len(self.dimensions)):
151                 yield (self.dimensions[i], self.dimensions[j])
152                 yield (self.dimensions[j], self.dimensions[i])
153     def iterRemainingDimensions(self, dimensionPair):
154         for d in self.dimensions:
155             if d not in dimensionPair:
156                 yield d
157     def generateHtml(self):
158         for pair in self.iterDimensionPairs():
159             remDims = [d for d in self.iterRemainingDimensions(pair)]
160             print pair, remDims
161             for vals in iterDimValues(remDims):
162                 page = Page(pair, remDims, vals, self)
163                 page.generate()
164         try:
165             os.remove("index.html")
166         except OSError: pass
167         os.symlink(page.getName(), "index.html")
168
169         #os.system("source-highlight -d --output-dir=. ../*.sh")
170
171 class Page:
172     def __init__(self, dimPair, dimOther, valsOther, tests):
173         self.dimy, self.dimx = dimPair
174         self.dimOther = dimOther
175         self.valsOther = valsOther
176         self.tests = tests
177     def getName(self):
178         return "%s-vs-%s-%s.html"%(self.dimy.type, self.dimx.type, "-".join([v.value for v in self.valsOther]))
179     def generate(self):
180         html = open(self.getName(), "w")
181         title = "CAN gateway timing analysis" + ", ".join([v.dim.name+" "+v.value for v in self.valsOther])
182         print >> html, """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
183 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
184 <head>
185 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
186 <title>%s</title>
187 <style>
188 img { border: 0; }
189 table { border-collapse: collapse; }
190 th, td { border: 1px solid lightgray; padding: 4px;}
191 </style>
192 </head>
193 <body>
194 <h1>%s</h1>"""  % (title, title)
195         for d in self.dimOther:
196             pass
197 #             print >>html, "View for %s: " % str(ps.pageclass.name)
198 #             for v in ps.values:
199 #                 print >>html, "<a href='%s-%s.html'>%s</a> | "%(ps.values.type, urllib.quote(v), v)
200 #             print >>html, "<br>"
201 #             try:
202 #                 print >>html, d.htmlPreamble()
203 #             except Exception:
204 #                 pass
205
206         print >>html, "<table><thead><tr><td> </td>"
207         for x in self.dimx:
208             print >>html, "<th>%s</th>" % x.htmlLabel()
209         print >>html, "</tr></thead>"
210         for y in self.dimy:
211             print >>html, "<tr><th>%s</th>" % y.htmlLabel()
212
213             for x in self.dimx:
214                 print >>html, "<td>"
215                 idx = [x,y]
216                 idx.extend(self.valsOther)
217                 test = tests.getTest(idx)
218                 test.printThumbLink(html)
219                 print >>html, "</td>"
220             print >>html, "</tr>"
221         print >> html, """
222 </table>
223 <div style="font-size: small; color: gray; margin-top: 1em;">Authors: Michal Sojka, Pavel Píša, Copyright © 2010 Czech Technical University in Prague</div>
224 </body>
225 """
226
227
228 if __name__ == "__main__":
229     os.chdir(sys.argv[1])
230     tests = Tests("./", DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionTest())
231     tests.generateHtml()
232     sys.exit(0)