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