source: 2011/26/write_report.py @ 2389

Revision 2389, 2.9 KB checked in by tekrjant, 12 years ago (diff)

lyhyempi aikaväli.

Line 
1"""
2Writes a html report of the build results. build_games.py
3should be run first.
4
5The report is written continuously. Hit Ctrl-c to stop.
6"""
7from __future__ import with_statement
8import sys
9import os
10from xml.dom.minidom import Document
11from os.path import *
12import shutil
13from shutil import *
14from glob import *
15from subprocess import *
16from datetime import *
17import time
18
19
20report_location = "\\\\eppu.it.jyu.fi\\kurssit\\npo\\temp"
21report = None
22table = None
23
24
25CSS = """
26tr {
27  color: white;
28}
29
30.success {
31  background-color: green;
32}
33
34.fail {
35  background-color: red;
36}
37"""
38
39
40def check_requirements():
41        if not exists("build"):
42                raise Exception("Directory build not found. Run build_games.py first!")
43
44def addElement(parent, name):
45        node = report.createElement(name)
46        parent.appendChild(node)
47        return node
48
49
50def addTextElement(parent, name, value):
51        node = report.createElement(name)
52        parent.appendChild(node)
53        node.appendChild(report.createTextNode(str(value)))
54        return node
55
56
57def initialize_report():
58        global report
59
60        report = Document()
61        html = addElement(report, "html")
62
63        refresh_tag = addElement(html, "meta")
64        refresh_tag.setAttribute("http-equiv", "refresh")
65        refresh_tag.setAttribute("content", "30")
66
67        head = addElement(html, "head")
68        style = addTextElement(head, "style", CSS)
69        style.setAttribute("type", "text/css")
70        addTextElement(head, "title", "Build")
71
72        body = addElement(html, "body")
73        global table
74        table = addElement(body, "table")
75
76
77def write_results():
78    for d in os.listdir("build"):
79            with open(join("build", d), "r") as f:
80                    lines = f.readlines()
81                    author = lines[0].strip()
82                    name = lines[1].strip()
83                    result = lines[2].strip()
84                    time_of_update = lines[3].strip()
85
86                    success = (result == "success")
87
88                    style = "success"
89                    if not success:
90                            style = "fail"
91                    tr = addElement(table, "tr")
92                    tr.setAttribute("class", style)
93                    addTextElement(tr, "td", author)
94                    addTextElement(tr, "td", name)
95                    addTextElement(tr, "td", time_of_update)
96
97
98def write_report():
99        with open(join(report_location, "build_report.html"), "w") as f:
100                f.write(report.toprettyxml())
101
102
103def main():
104        try:
105                while True:
106                        check_requirements()
107                        initialize_report()
108                        write_results()
109                        write_report()
110                        print "report written"
111                        time.sleep(10)
112        except KeyboardInterrupt:
113                print "cancelled"
114        except Exception, e:
115                print
116                print "NOT SUCCESSFUL:", e
117                return 1
118
119
120if __name__ == '__main__':
121        sys.exit(main())
122
Note: See TracBrowser for help on using the repository browser.