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