source: 2014/24/write_report.py @ 5032

Revision 4993, 3.7 KB checked in by anlakane, 5 years ago (diff)
Line 
1# -*- coding: latin-1 -*-
2"""
3Writes a html report of the build results. build_games.py
4should be run first.
5"""
6from __future__ import with_statement
7import sys
8import os
9from xml.dom.minidom import Document
10from os.path import *
11import shutil
12from shutil import *
13from glob import *
14from subprocess import *
15from datetime import *
16import time
17import re
18
19
20hide_pong_games = False
21
22report_location = "\\\\itsovpa.it.jyu.fi\\kurssit\\npo\\temp"
23report = None
24table = None
25
26
27CSS = """
28
29table, th, td {
30  border: 1px solid black;
31}
32
33th, td {
34  padding-left: 1em;
35  padding-right: 1em;
36}
37
38td {
39  color: white;
40}
41
42td.datetime {
43  text-align: right;
44}
45
46.success {
47  background-color: green;
48}
49
50.fail {
51  background-color: red;
52}
53"""
54
55
56def check_requirements():
57    if not exists("build"):
58        raise Exception("Directory build not found. Run build_games.py first!")
59
60def addElement(parent, name):
61    node = report.createElement(name)
62    parent.appendChild(node)
63    return node
64
65
66def addTextElement(parent, name, value):
67    node = report.createElement(name)
68    parent.appendChild(node)
69    node.appendChild(report.createTextNode(str(value)))
70    return node
71
72
73def initialize_report():
74    global report
75
76    report = Document()
77    html = addElement(report, "html")
78
79    refresh_tag = addElement(html, "meta")
80    refresh_tag.setAttribute("http-equiv", "refresh")
81    refresh_tag.setAttribute("content", "30")
82
83    head = addElement(html, "head")
84    style = addTextElement(head, "style", CSS)
85    style.setAttribute("type", "text/css")
86    addTextElement(head, "title", "Build")
87
88    body = addElement(html, "body")
89
90    global table
91    table = addElement(body, "table")
92    header_row = addElement(table, "tr")
93    addTextElement(header_row, "th", "Tekijä")
94    addTextElement(header_row, "th", "Peli")
95    addTextElement(header_row, "th", "Viimeisimmän talletuksen aika")
96
97
98def write_results():
99    results = []
100
101    for d in os.listdir("build"):
102        with open(join("build", d), "r") as f:
103            lines = f.readlines()
104            author = lines[0].strip()
105            name = lines[1].strip()
106            result = lines[2].strip()
107            time_of_update = lines[3].strip()
108            if hide_pong_games and re.search("^pong", name, flags=re.IGNORECASE):
109                continue
110
111            success = (result == "success")
112            rtuple = (author, name, result, time_of_update, success)
113            results.append(rtuple)
114
115    results.sort(key=lambda t: t[0].lower())
116
117    for r in results:
118        style = "success"
119        if not r[4]:
120            style = "fail"
121        tr = addElement(table, "tr")
122        tr.setAttribute("class", style)
123        addTextElement(tr, "td", r[0])
124        addTextElement(tr, "td", r[1])
125        timeElement = addTextElement(tr, "td", r[3])
126        timeElement.setAttribute("class", "datetime")
127
128def write_report():
129    with open(join(report_location, "build_report.html"), "w") as f:
130        f.write(report.toprettyxml())
131
132
133def parse_arguments():
134    for arg in sys.argv[1:]:
135        if arg == "--hide-pong":
136            global hide_pong_games
137            hide_pong_games = True
138        else:
139            raise Exception("Unknown argument: " + arg)
140
141
142def main():
143    try:
144        parse_arguments()
145        check_requirements()
146        initialize_report()
147        write_results()
148        write_report()
149        print "report written"
150    except KeyboardInterrupt:
151        print "cancelled"
152    except Exception, e:
153        print
154        print "NOT SUCCESSFUL:", e
155        return 1
156
157
158if __name__ == '__main__':
159    sys.exit(main())
160
Note: See TracBrowser for help on using the repository browser.