forked from rindera09/python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoxrenderfarm.py
More file actions
581 lines (492 loc) · 21.6 KB
/
foxrenderfarm.py
File metadata and controls
581 lines (492 loc) · 21.6 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# ! /usr/bin/env python
# coding=utf-8
import requests
import json
import os
import pprint
import copy
import sys
import time
class RvOs(object):
is_win = 0
is_linux = 0
is_mac = 0
if sys.platform.startswith("win"):
os_type = "win"
is_win = 1
elif sys.platform.startswith("linux"):
os_type = "linux"
is_linux = 1
else:
os_type = "mac"
is_mac = 1
class Api(object):
def __init__(self, render_server, debug=0):
self.url = 'https://%s/api/v2/task' % (render_server)
self.headers = {"Content-Type": "application/json"}
self.debug = debug
def post(self, data):
time.sleep(10)
if self.debug:
print "\n"
print "URL:"
print self.url
print "\n"
print "headers:"
pprint.pprint(self.headers)
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:
if r.json()["head"]["result"] != "0":
print "[ERROR]: " + r.json()["head"]["error_message"]
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, RvOs):
root = os.path.dirname(os.path.abspath(__file__))
def __init__(self, render_server, account, access_key, language="en",
debug=0):
Api.__init__(self, render_server, debug=debug)
self.data = {"head": {"access_key": access_key,
"account": account,
"msg_locale": language,
"action": ""},
"body": {}}
self.login()
def login(self):
result = self.get_users()
if result:
self.user_info = result[0]
self._init_upload_download_config()
else:
raise Exception("Login failed.")
def _init_upload_download_config(self):
if self.is_win:
self.rayvision_exe = os.path.join(self.root, "rayvision", "windows",
"rayvision_transmitter.exe")
else:
self.rayvision_exe = os.path.join(self.root, "rayvision", "centos",
"rayvision_transmitter")
self.account_id = self.user_info["id"]
self.upload_id = self.user_info["upload_id"]
self.download_id = self.user_info["download_id"]
self.transports = self.user_info["transports"]
if self.transports:
self.engine_type = self.transports[0]["engine"]
self.server_name = self.transports[0]["server"]
self.server_ip = self.transports[0]["ip"]
self.server_port = self.transports[0]["port"]
def submit_task(self, **kwargs):
data = copy.deepcopy(self.data)
if "action" in kwargs:
data["head"]["action"] = kwargs["action"]
else:
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"]
no_plugin = True
for i in plugins:
if i:
no_plugin = False
break
if no_plugin:
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 submit_maya(self, **kwargs):
return self.submit_task(**kwargs)
def submit_houdini(self, **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "create_houdini_task"
if kwargs:
for i in kwargs:
if i != "rop_info":
data["body"][i] = kwargs[i]
else:
data["body"]["layer_list"] = kwargs["rop_info"]
for j in data["body"]["layer_list"]:
j["layerName"] = j["rop"]
j.pop("rop")
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 "rop_info" not in kwargs:
raise Exception("Missing rop info, please check args.")
data["body"]["input_scene_path"] = data["body"]["input_scene_path"].replace(":", "").replace("\\", "/")
project = self.get_projects(kwargs["project_name"])
if not project:
raise Exception("Project <%s> doesn't exists." % (kwargs["project_name"]))
plugins = project[0]["plugins"]
no_plugin = True
for i in plugins:
if i:
no_plugin = False
break
if no_plugin:
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 submit_blender(self, **kwargs):
return self.submit_task(action="create_blender_task", **kwargs)
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, local_path_list, server_path='/', **kwargs):
transmit_type = "upload_files"
result = {}
for i in set(local_path_list):
if os.path.exists(i):
local_path = i
cmd = "echo y | %s %s %s %s %s %s %s %s %s %s" % (self.rayvision_exe,
self.engine_type,
self.server_name,
self.server_ip,
self.server_port,
self.upload_id,
self.account_id,
transmit_type,
local_path,
server_path)
if self.debug:
print cmd
result[i] = True
sys.stdout.flush()
result[i] = os.system(cmd)
else:
result[i] = False
return result
def download(self, task_id, local_path, **kwargs):
transmit_type = "download_files"
task = self.get_tasks(task_id)
if task:
input_scene_path = task[0]["input_scene_path"]
server_path = "%s_%s" % (task_id, os.path.splitext(os.path.basename(input_scene_path))[0].strip())
cmd = "echo y | %s %s %s %s %s %s %s %s %s %s" % (self.rayvision_exe,
self.engine_type,
self.server_name,
self.server_ip,
self.server_port,
self.download_id,
self.account_id,
transmit_type,
local_path,
server_path)
if self.debug:
print cmd
sys.stdout.flush()
return os.system(cmd)
else:
return False
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, cg_soft_name, plugin_name="",
render_os="", **kwargs):
data = copy.deepcopy(self.data)
data["head"]["action"] = "create_project"
if not render_os:
if self.is_win:
render_os = "Windows"
else:
render_os = "Linux"
data["body"]["render_os"] = render_os
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.add_project_config(project_id, cg_soft_name, plugin_name,
is_default=1)
self._message_output("INFO", "Project ID: {0}".format(project_id))
return project_id
else:
return -1
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:
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_project_info" 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
""" NO 7.1.3 Restart the tasks
:param task_id: the tasks you what to restart
:param restart_type: 0 -- restart the failed frames
1 -- restart the frames that give up
2 -- restart the finished frames
3 -- restart the start frames
4 -- restart the waiting frames
Here some example:: restart_tasks("123", "0")
restart_tasks(["123", "456"], "3")
"""
def restart_tasks(self, task_id, restart_type="0"):
data = copy.deepcopy(self.data)
data["head"]["action"] = "operate_task"
data["body"]["operate_order"] = "1"
data["body"]["restart_type"] = str(restart_type)
if isinstance(task_id, list) and len(task_id) > 1:
task_id = ''.join([str(id) + ',' for id in task_id[:-1]]) + str(task_id[-1])
data["body"]["task_id"] = str(task_id)
result = self.post(data=data)
if result["head"]["result"] == "0":
self._message_output("INFO", "task {0} restart.".format(task_id))
return True
else:
self._message_output("ERROR", result["head"]["error_message"])
return False
""" NO 7.1.3 Stop the tasks
:param task_id: the tasks you what to pause
Here some example:: stop_tasks(123)
stop_tasks("123")
stop_tasks(["123", "456"])
stop_tasks([123, 456])
"""
def stop_tasks(self, task_id):
data = copy.deepcopy(self.data)
data["head"]["action"] = "operate_task"
data["body"]["operate_order"] = "0"
if isinstance(task_id, list) and len(task_id) > 1:
task_id = ''.join(map(lambda id: str(id) + ",", task_id[:-1])) + str(task_id[-1])
data["body"]["task_id"] = str(task_id)
result = self.post(data=data)
if result["head"]["result"] == "0":
self._message_output("INFO", "task {0} paused.".format(task_id))
return True
else:
self._message_output("ERROR", result["head"]["error_message"])
return False
""" NO 7.1.3 Delete the tasks
:param task_id: the tasks you what to delete
Here some example:: delete_tasks(123)
delete_tasks("123")
delete_tasks(["123", "456"])
delete_tasks([123, 456])
"""
def delete_tasks(self, task_id):
data = copy.deepcopy(self.data)
data["head"]["action"] = "operate_task"
data["body"]["operate_order"] = "2"
if isinstance(task_id, list) and len(task_id) > 1:
task_id = ''.join(map(lambda id: str(id) + ",", task_id[:-1])) + str(task_id[-1])
data["body"]["task_id"] = str(task_id)
result = self.post(data=data)
if result["head"]["result"] == "0":
self._message_output("INFO", "task {0} deleted.".format(task_id))
return True
else:
self._message_output("ERROR", result["head"]["error_message"])
return False
""" Get the plugins of the project
:param project_name: the name of the project
"""
def get_project_plugins_config(self, project_name):
data = copy.deepcopy(self.data)
data["head"]["action"] = "query_project"
if not project_name:
self._message_output("WARNING", "Mising project name")
return []
data["body"]["project_name"] = project_name
result = self.post(data)
plugins = []
if result["body"]["data"]:
plugins = result["body"]["data"][0]["plugins"]
if result["head"]["result"] == "0":
self._message_output("INFO", "Query plugins config id:")
return plugins
else:
self._message_output("WARNING", result["head"]["error_message"])
return []