-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfoxrenderfarm.py
More file actions
369 lines (300 loc) · 13.2 KB
/
foxrenderfarm.py
File metadata and controls
369 lines (300 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# ! /usr/bin/env python
# coding=utf-8
import requests
import json
import os
import pprint
import copy
import sys
class Api(object):
def __init__(self, render_server):
self.url = 'https://%s/api/v1/task' % (render_server)
self.headers = {"Content-Type": "application/json"}
self.debug = 0
def post(self, data):
if self.debug:
print "\n"
print "Post data:"
pprint.pprint(data)
print "\n"
if isinstance(data, dict):
data = json.dumps(data)
r = requests.post(self.url, headers=self.headers,
data=data)
if r.status_code == 200:
return r.json()
elif r.status_code == 405:
print r.status_code
raise Exception("Connect server error.")
else:
print r.status_code
raise Exception("Server internal error.")
class Fox(Api):
root = os.path.dirname(os.path.abspath(__file__))
ascp_exe = os.path.join(root, "aspera", "ascp.exe")
def __init__(self, render_server, account, access_key,
aspera_server, aspera_password, language="en"):
Api.__init__(self, render_server)
self.data = {"head": {"access_key": access_key,
"account": account,
"msg_locale": language,
"action": ""},
"body": {}}
self.login()
self.aspera_server = aspera_server
self.aspera_upload = self.account_id + "_upload"
self.aspera_download = self.account_id + "_download"
self.aspera_password = aspera_password
def login(self):
result = self.get_users()
if result:
self.account_id = result[0]["id"]
else:
raise Exception("account or access_key is not valid.")
def submit_task(self, **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "create_task"
if kwargs:
for i in kwargs:
data["body"][i] = kwargs[i]
if "project_name" not in kwargs:
raise Exception("Missing project_name args, please check.")
if "input_scene_path" not in kwargs:
raise Exception("Missing input_scene_path args, please check.")
if "frames" not in kwargs:
raise Exception("Missing frames, please check args.")
data["body"]["input_scene_path"] = data["body"]["input_scene_path"].replace(":", "").replace("\\", "/")
data["body"]["submit_account"] = data["head"]["account"]
project = self.get_projects(kwargs["project_name"])
if not project:
raise Exception("Project <%s> doesn't exists." % (kwargs["project_name"]))
plugins = project[0]["plugins"]
if not plugins:
raise Exception("Project <%s> doesn't have any plugin settings." % (kwargs["project_name"]))
default_plugin = [i for i in plugins
if "is_default" in i if i["is_default"] == '1']
if len(plugins) == 1:
default_plugin = plugins
if not default_plugin:
raise Exception("Project <%s> doesn't have a default plugin settings." % (kwargs["project_name"]))
data["body"]["cg_soft_name"] = default_plugin[0]["cg_soft_name"]
if "plugin_name" in default_plugin[0]:
data["body"]["plugin_name"] = default_plugin[0]["plugin_name"]
result = self.post(data)
if result["head"]["result"] == '0':
return int(result["body"]["data"][0]["task_id"])
else:
pprint.pprint(result)
return -1
def get_users(self, has_child_account=0):
data = copy.deepcopy(self.data)
data["head"]["action"] = "query_customer"
if not has_child_account:
data["body"]["login_name"] = data["head"]["account"]
result = self.post(data)
if result["head"]["result"] == "0":
return result["body"]["data"]
else:
return []
def get_projects(self, project_name=None):
data = copy.deepcopy(self.data)
data["head"]["action"] = "query_project"
if project_name:
data["body"]["project_name"] = project_name
result = self.post(data)
if result["head"]["result"] == "0":
return result["body"]["data"]
else:
return []
def get_tasks(self, task_id=None, project_name=None, has_frames=0, task_filter={}):
data = copy.deepcopy(self.data)
data["head"]["action"] = "query_task"
if project_name:
data["body"]["project_name"] = project_name
if task_id:
data["body"]["task_id"] = str(task_id)
if has_frames:
data["body"]["is_jobs_included"] = "1"
if task_filter:
for i in task_filter:
data["body"][i] = task_filter[i]
result = self.post(data)
if result["head"]["result"] == "0":
return result["body"]["data"]
else:
return []
def upload(self, path_list, skip_same=1, user=None, password=None):
user = user if user else self.aspera_upload
os.environ["ASPERA_SCP_PASS"] = password if password else self.aspera_password
overwrite = "older" if skip_same else "always"
result = {}
for i in set(path_list):
if os.path.exists(i):
server_path = os.path.dirname(i).replace(":", "")
cmd = "echo y | \"%s\" -P 33001 -O 33001 -d -p -l 1000000 " \
"--overwrite=%s \"%s\" %s@%s:/%s" % (self.ascp_exe,
overwrite,
i,
user,
self.aspera_server,
server_path)
print cmd
sys.stdout.flush()
result[i] = os.system(cmd)
else:
result[i] = -1
return result
def download(self, task_id, local_path, skip_same=1, user=None, password=None):
user = user if user else self.aspera_download
os.environ["ASPERA_SCP_PASS"] = password if password else self.aspera_password
overwrite = "older" if skip_same else "always"
task = self.get_tasks(task_id)
if task:
server_path = "%s_%s" % (task_id, os.path.splitext(os.path.basename(task[0]["scene_name"]))[0])
cmd = "echo y | \"%s\" -P 33001 -O 33001 -d -p -l 1000000 " \
"--overwrite=%s %s@%s:/%s \"%s\"" % (self.ascp_exe,
overwrite,
user,
self.aspera_server,
server_path,
local_path)
print cmd
sys.stdout.flush()
return os.system(cmd)
else:
return -1
def get_server_files(self):
''
def delete_server_files(self):
''
""" NO 7.2.3
:param project_name: the name of the project you want to create
:param kwargs: can be used to pass more arguments, not necessary
including project_path, render_os, remark, sub_account
"""
def create_project(self, project_name, **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "create_project"
if not project_name:
raise Exception("Missing project_name, please check")
data["body"]["project_name"] = project_name
for key, value in kwargs.items():
data["body"][key] = value
result = self.post(data=data)
if result["head"]["result"] == '0':
project_id = int(result["body"]["project_id"])
self._message_output("INFO", "Project ID: {0}".format(project_id))
return True, project_id
else:
self._message_output("ERROR", result["head"]["error_message"])
return False
def _message_output(self, msg_type=None, msg=None):
print "[{0}]: {1}".format(msg_type, msg)
""" NO: 7.2.2 Query plugins
:param kwargs: can be used to pass more arguments, not necessary
including cg_soft_name, plugin_name
Here some examples::
get_plugin()
get_plugin(cg_soft_name="3ds Max 2010")
get_plugin(cg_soft_name="3ds Max 2010", plugin_name="finalrender 3.5sp6")
"""
def get_plugins_available(self, **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "query_plugin"
for key, value in kwargs.items():
data["body"][key] = value
result = self.post(data=data)
if result["head"]["result"] == "0":
self._save_list2file(result["body"], "plugins.txt")
return True, result["body"]
else:
self._message_output("ERROR", result["head"]["error_message"])
return False
def _save_list2file(self, list_data, file_name, remark="\n"):
basedir = os.path.abspath(os.path.dirname(__file__))
save_path = os.path.join(basedir, file_name)
with open(save_path,"a+") as f:
if remark:
f.write(remark)
for line in list_data:
f.write(str(line) + "\n")
print "[INFO]:" + save_path + " has saved."
""" NO 7.2.4 Add config for project
:param project_id: the id of the existed project you choose
:param cg_soft_name: the software you use
:param plugin_name: the plugin you use
:param is_default: make it as default setting
:param kwargs: can be used to pass more arguments, not necessary
"""
def add_project_config(self, project_id, cg_soft_name, plugin_name=None,
is_default=0, **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "operate_project"
data["body"]["operate_type"] = 0
data["body"]["project_id"] = int(project_id)
data["body"]["cg_soft_name"] = cg_soft_name
if plugin_name:
data["body"]["plugin_name"] = plugin_name
data["body"]["is_default"] = is_default
for key,value in kwargs.items():
data["body"][key] = value
result = self.post(data=data)
if result["head"]["result"] == "0":
return True
else:
self._message_output("ERROR", result["head"]["error_message"])
return False
""" NO 7.2.4 Delete config for project
:param project_id: the id of the existed project you choose
:param config_id: the id of configuration you want to delete
if not pass this argument it will delete all
you can use "get_projects" to get config_id
:param kwargs: can be used to pass more arguments, not necessary
"""
def delete_project_config(self, project_id, config_id=None, **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "operate_project"
data["body"]["operate_type"] = 2
data["body"]["project_id"] = int(project_id)
if config_id:
data["body"]["config_id"] = int(config_id)
for key,value in kwargs.items():
data["body"][key] = value
result = self.post(data=data)
if result["head"]["result"] == "0":
self._message_output("INFO", "configuration delete")
return True
else:
self._message_output("ERROR", result["head"]["error_message"])
return False
""" NO 7.2.4 Modify config for project
:param project_id: the id of the existed project you choose
:param config_id: the id of configuration you want to delete
if not pass this argument it will delete all
you can use "get_projects" to get config_id
:param cg_soft_name: the software you use
:param plugin_name: the plugin you use
:param is_default: make it as default setting, just one default allowed
:param kwargs: can be used to pass more arguments, not necessary
"""
def modify_project_config(self, project_id, config_id, cg_soft_name,
plugin_name=None, is_default=None, **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "operate_project"
data["body"]["operate_type"] = 1
data["body"]["project_id"] = int(project_id)
data["body"]["config_id"] = int(config_id)
data["body"]["cg_soft_name"] = cg_soft_name
if plugin_name is not None:
data["body"]["plugin_name"] = plugin_name
if is_default:
data["body"]["is_default"] = int(is_default)
for key, value in kwargs.items():
data["body"][key] = value
result = self.post(data=data)
if result["head"]["result"] == "0":
self._message_output("INFO", "modify the configuration")
return True
else:
self._message_output("ERROR", result["head"]["error_message"])
return False