e8046d7ac8c5ab4cadeb78ab6e46a6b8a46ad134
[blender.git] / release / io / netrender / model.py
1 import sys, os
2 import http, http.client, http.server, urllib
3 import subprocess, shutil, time, hashlib
4
5 from netrender.utils import *
6
7 class LogFile:
8         def __init__(self, job_id = 0, frames = []):
9                 self.job_id = job_id
10                 self.frames = frames
11         
12         def serialize(self):
13                 return  {
14                                                         "job_id": self.job_id,
15                                                         "frames": self.frames
16                                                 }
17         
18         @staticmethod
19         def materialize(data):
20                 if not data:
21                         return None
22                 
23                 logfile = LogFile()
24                 logfile.job_id = data["job_id"]
25                 logfile.frames = data["frames"]
26                 
27                 return logfile
28
29 class RenderSlave:
30         _slave_map = {}
31         
32         def __init__(self):
33                 self.id = ""
34                 self.name = ""
35                 self.address = (0,0)
36                 self.stats = ""
37                 self.total_done = 0
38                 self.total_error = 0
39                 self.last_seen = 0.0
40                 
41         def serialize(self):
42                 return  {
43                                                         "id": self.id,
44                                                         "name": self.name,
45                                                         "address": self.address,
46                                                         "stats": self.stats,
47                                                         "total_done": self.total_done,
48                                                         "total_error": self.total_error,
49                                                         "last_seen": self.last_seen
50                                                 }
51         
52         @staticmethod
53         def materialize(data):
54                 if not data:
55                         return None
56                 
57                 slave_id = data["id"]
58                 
59                 if slave_id in RenderSlave._slave_map:
60                         return RenderSlave._slave_map[slave_id]
61                 else:
62                         slave = RenderSlave()
63                         slave.id = slave_id
64                         slave.name = data["name"]
65                         slave.address = data["address"]
66                         slave.stats = data["stats"]
67                         slave.total_done = data["total_done"]
68                         slave.total_error = data["total_error"]
69                         slave.last_seen = data["last_seen"]
70                         
71                         RenderSlave._slave_map[slave_id] = slave
72                         
73                         return slave
74
75 class RenderJob:
76         def __init__(self):
77                 self.id = ""
78                 self.name = ""
79                 self.files = []
80                 self.frames = []
81                 self.chunks = 0
82                 self.priority = 0
83                 self.credits = 0
84                 self.blacklist = []
85                 self.last_dispatched = 0.0
86         
87         def addFile(self, file_path, start=-1, end=-1):
88                 self.files.append((file_path, start, end))
89         
90         def addFrame(self, frame_number):
91                 frame = RenderFrame(frame_number)
92                 self.frames.append(frame)
93                 return frame
94         
95         def __len__(self):
96                 return len(self.frames)
97         
98         def countFrames(self, status=QUEUED):
99                 total = 0
100                 for f in self.frames:
101                         if f.status == status:
102                                 total += 1
103                 
104                 return total
105         
106         def countSlaves(self):
107                 return len(set((frame.slave for frame in self.frames if frame.status == DISPATCHED)))
108         
109         def framesStatus(self):
110                 results = {
111                                                                 QUEUED: 0,
112                                                                 DISPATCHED: 0,
113                                                                 DONE: 0,
114                                                                 ERROR: 0
115                                                         }
116                 
117                 for frame in self.frames:
118                         results[frame.status] += 1
119                         
120                 return results
121         
122         def __contains__(self, frame_number):
123                 for f in self.frames:
124                         if f.number == frame_number:
125                                 return True
126                 else:
127                         return False
128         
129         def __getitem__(self, frame_number):
130                 for f in self.frames:
131                         if f.number == frame_number:
132                                 return f
133                 else:
134                         return None
135                 
136         def serialize(self, frames = None):
137                 min_frame = min((f.number for f in frames)) if frames else -1
138                 max_frame = max((f.number for f in frames)) if frames else -1
139                 return  {
140                                                         "id": self.id,
141                                                         "name": self.name,
142                                                         "files": [f for f in self.files if f[1] == -1 or not frames or (f[1] <= min_frame <= f[2] or f[1] <= max_frame <= f[2])],
143                                                         "frames": [f.serialize() for f in self.frames if not frames or f in frames],
144                                                         "chunks": self.chunks,
145                                                         "priority": self.priority,
146                                                         "credits": self.credits,
147                                                         "blacklist": self.blacklist,
148                                                         "last_dispatched": self.last_dispatched
149                                                 }
150
151         @staticmethod
152         def materialize(data):
153                 if not data:
154                         return None
155                 
156                 job = RenderJob()
157                 job.id = data["id"]
158                 job.name = data["name"]
159                 job.files = data["files"]
160                 job.frames = [RenderFrame.materialize(f) for f in data["frames"]]
161                 job.chunks = data["chunks"]
162                 job.priority = data["priority"]
163                 job.credits = data["credits"]
164                 job.blacklist = data["blacklist"]
165                 job.last_dispatched = data["last_dispatched"]
166
167                 return job
168
169 class RenderFrame:
170         def __init__(self, number = 0):
171                 self.number = number
172                 self.time = 0
173                 self.status = QUEUED
174                 self.slave = None
175
176         def serialize(self):
177                 return  {
178                                                         "number": self.number,
179                                                         "time": self.time,
180                                                         "status": self.status,
181                                                         "slave": None if not self.slave else self.slave.serialize()
182                                                 }
183                                                 
184         @staticmethod
185         def materialize(data):
186                 if not data:
187                         return None
188                 
189                 frame = RenderFrame()
190                 frame.number = data["number"]
191                 frame.time = data["time"]
192                 frame.status = data["status"]
193                 frame.slave = RenderSlave.materialize(data["slave"])
194
195                 return frame