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