1 | # -*- coding: latin-1 -*- |
---|
2 | """ |
---|
3 | Writes a html report of the build results. build_games.py |
---|
4 | should be run first. |
---|
5 | """ |
---|
6 | from __future__ import with_statement |
---|
7 | import sys |
---|
8 | import os |
---|
9 | from xml.dom.minidom import Document |
---|
10 | from os.path import * |
---|
11 | import shutil |
---|
12 | from shutil import * |
---|
13 | from glob import * |
---|
14 | from subprocess import * |
---|
15 | from datetime import * |
---|
16 | import time |
---|
17 | import re |
---|
18 | |
---|
19 | |
---|
20 | hide_pong_games = False |
---|
21 | |
---|
22 | report_location = "\\\\eppu.it.jyu.fi\\kurssit\\npo\\temp" |
---|
23 | report = None |
---|
24 | table = None |
---|
25 | |
---|
26 | |
---|
27 | CSS = """ |
---|
28 | |
---|
29 | table, th, td { |
---|
30 | border: 1px solid black; |
---|
31 | } |
---|
32 | |
---|
33 | th, td { |
---|
34 | padding-left: 1em; |
---|
35 | padding-right: 1em; |
---|
36 | } |
---|
37 | |
---|
38 | td { |
---|
39 | color: white; |
---|
40 | } |
---|
41 | |
---|
42 | td.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 | |
---|
56 | def check_requirements(): |
---|
57 | if not exists("build"): |
---|
58 | raise Exception("Directory build not found. Run build_games.py first!") |
---|
59 | |
---|
60 | def addElement(parent, name): |
---|
61 | node = report.createElement(name) |
---|
62 | parent.appendChild(node) |
---|
63 | return node |
---|
64 | |
---|
65 | |
---|
66 | def 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 | |
---|
73 | def 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 | |
---|
98 | def 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 | |
---|
123 | def write_report(): |
---|
124 | with open(join(report_location, "build_report.html"), "w") as f: |
---|
125 | f.write(report.toprettyxml()) |
---|
126 | |
---|
127 | |
---|
128 | def 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 | |
---|
137 | def 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 | |
---|
153 | if __name__ == '__main__': |
---|
154 | sys.exit(main()) |
---|
155 | |
---|