50ca08d1723bf783ebe3b6b60ff8b8ebe64bccf0
[blender.git] / release / io / netrender / utils.py
1 import bpy
2 import sys, os
3 import re
4 import http, http.client, http.server, urllib
5 import subprocess, shutil, time, hashlib
6
7 import netrender.model
8
9 VERSION = b"0.5"
10
11 # Jobs status
12 JOB_WAITING = 0 # before all data has been entered
13 JOB_PAUSED = 1 # paused by user
14 JOB_QUEUED = 2 # ready to be dispatched
15
16 # Frames status
17 QUEUED = 0
18 DISPATCHED = 1
19 DONE = 2
20 ERROR = 3
21
22 def rnaType(rna_type):
23         bpy.types.register(rna_type)
24         return rna_type
25
26 def rnaOperator(rna_op):
27         bpy.ops.add(rna_op)
28         return rna_op
29
30 def clientConnection(scene):
31                 netsettings = scene.network_render
32                 
33                 if netsettings.server_address == "[default]":
34                         bpy.ops.render.netclientscan()
35                 
36                 conn = http.client.HTTPConnection(netsettings.server_address, netsettings.server_port)
37                 
38                 if clientVerifyVersion(conn):
39                         return conn
40                 else:
41                         conn.close()
42                         return None
43
44 def clientVerifyVersion(conn):
45         conn.request("GET", "/version")
46         response = conn.getresponse()
47         
48         if response.status != http.client.OK:
49                 conn.close()
50                 return False
51         
52         server_version = response.read()
53         
54         if server_version != VERSION:
55                 print("Incorrect server version!")
56                 print("expected", VERSION, "received", server_version)
57                 return False
58         
59         return True
60
61 def prefixPath(prefix_directory, file_path, prefix_path):
62         if os.path.isabs(file_path):
63                 # if an absolute path, make sure path exists, if it doesn't, use relative local path
64                 full_path = file_path
65                 if not os.path.exists(full_path):
66                         p, n = os.path.split(full_path)
67                         
68                         if prefix_path and p.startswith(prefix_path):
69                                 directory = prefix_directory + p[len(prefix_path):]
70                                 full_path = directory + n
71                                 if not os.path.exists(directory):
72                                         os.mkdir(directory)
73                         else:
74                                 full_path = prefix_directory + n
75         else:
76                 full_path = prefix_directory + file_path
77         
78         return full_path