source: 2012/24/write_report.py @ 3843

Revision 2910, 3.5 KB checked in by tojukarp, 7 years ago (diff)

Auto-build script

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 = "\\\\eppu.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    for d in os.listdir("build"):
100        with open(join("build", d), "r") as f:
101            lines = f.readlines()
102            author = lines[0].strip()
103            name = lines[1].strip()
104            result = lines[2].strip()
105            time_of_update = lines[3].strip()
106
107            if hide_pong_games and re.search("pong", name, flags=re.IGNORECASE):
108                continue
109
110            success = (result == "success")
111
112            style = "success"
113            if not success:
114                style = "fail"
115            tr = addElement(table, "tr")
116            tr.setAttribute("class", style)
117            addTextElement(tr, "td", author)
118            addTextElement(tr, "td", name)
119            timeElement = addTextElement(tr, "td", time_of_update)
120            timeElement.setAttribute("class", "datetime")
121
122
123def write_report():
124    with open(join(report_location, "build_report.html"), "w") as f:
125        f.write(report.toprettyxml())
126
127
128def parse_arguments():
129    for arg in sys.argv[1:]:
130        if arg == "--hide-pong":
131            global hide_pong_games
132            hide_pong_games = True
133        else:
134            raise Exception("Unknown argument: " + arg)
135
136
137def main():
138    try:
139        parse_arguments()
140        check_requirements()
141        initialize_report()
142        write_results()
143        write_report()
144        print "report written"
145    except KeyboardInterrupt:
146        print "cancelled"
147    except Exception, e:
148        print
149        print "NOT SUCCESSFUL:", e
150        return 1
151
152
153if __name__ == '__main__':
154    sys.exit(main())
155
Note: See TracBrowser for help on using the repository browser.