* fix snprintf error with mingw
[blender.git] / release / io / netrender / master_html.py
1 import re
2
3 from netrender.utils import *
4
5
6 def get(handler):
7         def output(text):
8                 handler.wfile.write(bytes(text, encoding='utf8'))
9         
10         def link(text, url):
11                 return "<a href='%s'>%s</a>" % (url, text)
12         
13         def startTable(border=1):
14                 output("<table border='%i'>" % border)
15         
16         def headerTable(*headers):
17                 output("<thead><tr>")
18                 
19                 for c in headers:
20                         output("<td>" + c + "</td>")
21                 
22                 output("</tr></thead>")
23         
24         def rowTable(*data):
25                 output("<tr>")
26                 
27                 for c in data:
28                         output("<td>" + str(c) + "</td>")
29                 
30                 output("</tr>")
31         
32         def endTable():
33                 output("</table>")
34         
35         handler.send_head(content = "text/html")
36         
37         if handler.path == "/html" or handler.path == "/":
38                 output("<html><head><title>NetRender</title></head><body>")
39         
40                 output("<h2>Master</h2>")
41         
42                 output("<h2>Slaves</h2>")
43                 
44                 startTable()
45                 headerTable("id", "name", "address", "stats")
46                 
47                 for slave in handler.server.slaves:
48                         rowTable(slave.id, slave.name, slave.address[0], slave.stats)
49                 
50                 endTable()
51                 
52                 output("<h2>Jobs</h2>")
53                 
54                 startTable()
55                 headerTable("id", "name", "credits", "time since last", "length", "done", "dispatched", "error", "priority", "exception")
56
57                 handler.server.update()
58                 
59                 for job in handler.server.jobs:
60                         results = job.framesStatus()
61                         rowTable(link(job.id, "/html/job" + job.id), job.name, round(job.credits, 1), int(time.time() - job.last_dispatched), len(job), results[DONE], results[DISPATCHED], results[ERROR], handler.server.balancer.applyPriorities(job), handler.server.balancer.applyExceptions(job))
62                 
63                 endTable()
64                 
65                 output("</body></html>")
66         
67         elif handler.path.startswith("/html/job"):
68                 job_id = handler.path[9:]
69                 
70                 output("<html><head><title>NetRender</title></head><body>")
71         
72                 job = handler.server.getJobByID(job_id)
73                 
74                 if job:
75                         output("<h2>Frames</h2>")
76                 
77                         startTable()
78                         headerTable("no", "status", "render time", "slave", "log")
79                         
80                         for frame in job.frames:
81                                 rowTable(frame.number, frame.statusText(), "%.1fs" % frame.time, frame.slave.name if frame.slave else "&nbsp;", link("view log", "/html/log%s_%i" % (job_id, frame.number)) if frame.log_path else "&nbsp;")
82                         
83                         endTable()
84                 else:
85                         output("no such job")
86                 
87                 output("</body></html>")
88         
89         elif handler.path.startswith("/html/log"):
90                 pattern = re.compile("([a-zA-Z0-9]+)_([0-9]+)")
91                 
92                 output("<html><head><title>NetRender</title></head><body>")
93                 
94                 match = pattern.match(handler.path[9:])
95                 if match:
96                         job_id = match.groups()[0]
97                         frame_number = int(match.groups()[1])
98                         
99                         job = handler.server.getJobByID(job_id)
100                         
101                         if job:
102                                 frame = job[frame_number]
103                                 
104                                 if frame:
105                                                 f = open(frame.log_path, 'rb')
106                                                 
107                                                 output("<pre>")
108                                                 
109                                                 shutil.copyfileobj(f, handler.wfile)
110                                                 
111                                                 output("</pre>")
112                                                 
113                                                 f.close()
114                                 else:
115                                         output("no such frame")
116                         else:
117                                 output("no such job")
118                 else:
119                         output("malformed url")
120                 
121                 output("</body></html>")