Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved original modules into a single Python package called nukeExtern…

…alControl, and renamed them to 'client' and 'server.' Moved constants, exception definitions, and pickle-safe type lists to a new 'common' submodule. Also cleaned up some docstrings.
  • Loading branch information...
commit 3baa58583ce9c4b6cdb3c9f1168fe6e15864ffe5 1 parent ea86519
Nathan Rusch authored
0  nukeExternalControl/__init__.py
No changes.
57 nukeCommandClient.py → nukeExternalControl/client.py
... ... @@ -1,8 +1,7 @@
1 1 '''
2   -This script defines the client-side classes for the Nuke command server interface.
  2 +This module defines the client-side classes for the Nuke command server interface.
3 3
4   -It also functions as an executable for the purposes of launching NukeCommandManager
5   -instances.
  4 +It also functions as an executable to launch NukeCommandManager instances.
6 5 '''
7 6
8 7 import inspect
@@ -12,31 +11,8 @@
12 11 import sys
13 12 import threading
14 13 import time
15   -from os import devnull
16   -
17   -basicTypes = [int, float, complex, str, unicode, buffer, xrange, bool, type(None)]
18   -listTypes = [list, tuple, set, frozenset]
19   -dictTypes = [dict]
20   -
21   -# This constant should be set to whatever absolute or
22   -# relative call your system uses to launch Nuke (excluding
23   -# any flags or arguments).
24   -NUKE_EXEC = 'Nuke'
25   -
26   -MAX_SOCKET_BYTES = 2048
27   -
28   -class NukeLicenseError(StandardError):
29   - pass
30   -
31   -class NukeConnectionError(StandardError):
32   - pass
33   -
34   -class NukeManagerError(NukeConnectionError):
35   - pass
36   -
37   -class NukeServerError(NukeConnectionError):
38   - pass
39 14
  15 +from nukeExternalControl.common import *
40 16
41 17 class NukeConnection():
42 18 '''
@@ -52,8 +28,8 @@ def __init__(self, port=None, host="localhost", instance=0):
52 28 self._host = host
53 29 self.is_active = False
54 30 if not port:
55   - start_port = 54200 + instance
56   - end_port = 54300
  31 + start_port = DEFAULT_START_PORT + instance
  32 + end_port = DEFAULT_END_PORT
57 33 self._port = self.find_connection_port(start_port, end_port)
58 34 if self._port == -1:
59 35 raise NukeConnectionError("Connection with Nuke failed")
@@ -72,13 +48,11 @@ def find_connection_port(self, start_port, end_port):
72 48 return -1
73 49
74 50 def send(self, data):#
75   - size = 4096
76   -
77 51 try:
78 52 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
79 53 s.connect((self._host, self._port))
80 54 s.send(data)
81   - result = s.recv(size)
  55 + result = s.recv(SOCKET_BUFFER_SIZE)
82 56 s.close()
83 57 except socket.error:
84 58 raise NukeConnectionError("Connection with Nuke failed")
@@ -272,8 +246,8 @@ class NukeCommandManager():
272 246 available OS-assigned port.
273 247
274 248 When the manager's __enter__ method is called, the server
275   - subprocess is started. The manager then waits for the managed
276   - server to call back with its status and bound port.
  249 + subprocess is started. The manager then waits for the server
  250 + to call back with its status and bound port.
277 251
278 252 A NukeConnection instance is then started using the port number
279 253 returned by the managed server's callback. This instance is
@@ -286,9 +260,6 @@ class NukeCommandManager():
286 260 its companion server the 'shutdown' signal. This will cause the
287 261 server to send back its shutdown message, close the connection to
288 262 the client, and exit cleanly.
289   -
290   - The __exit__ method then waits for the server thread to exit by
291   - calling its '.join()' method.
292 263 '''
293 264 def __init__(self, license_retry_count=5, license_retry_delay=5):
294 265 self.manager_port = -1
@@ -334,7 +305,6 @@ def __exit__(self, type, value, traceback):
334 305 self.nuke_stdout, self.nuke_stderr = self.serverProc.communicate()
335 306
336 307 def start_server(self):
337   - bufsize = 4096
338 308 # Make sure the port number has a trailing space... this is a bug in Nuke's
339 309 # Python argument parsing (logged with The Foundry as Bug 17918)
340 310 procArgs = ([NUKE_EXEC, '-t', '-m', '1', '--', inspect.getabsfile(self.__class__), '%d ' % self.manager_port],)
@@ -347,7 +317,7 @@ def start_server(self):
347 317 try:
348 318 while True:
349 319 try:
350   - # This will time out after 10 seconds based on the socket settings
  320 + # Times out after 10 seconds based on the socket settings
351 321 server, address = self.manager_socket.accept()
352 322 except socket.timeout:
353 323 retCode = self.serverProc.poll()
@@ -355,7 +325,7 @@ def start_server(self):
355 325 raise NukeLicenseError
356 326 else: # Nuke is either still running or dead for other reasons.
357 327 raise NukeManagerError("Server process failed to start properly.")
358   - data = server.recv(bufsize)
  328 + data = server.recv(SOCKET_BUFFER_SIZE)
359 329 if data:
360 330 serverData = pickle.loads(data)
361 331 server.close()
@@ -390,13 +360,12 @@ def shutdown_server(self):
390 360 client could not be initialized.
391 361 Returns the server's shutdown message.
392 362 '''
393   - bufsize = 1024 * 1024
394 363 packet = {'action':'shutdown', 'id':-1, 'parameters':None}
395 364 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
396 365 try:
397 366 s.connect(('', self.server_port))
398 367 s.send(pickle.dumps(packet))
399   - result = s.recv(bufsize)
  368 + result = s.recv(SOCKET_BUFFER_SIZE)
400 369 s.close()
401 370 return pickle.loads(result)
402 371 except socket.error:
@@ -410,8 +379,8 @@ def start_managed_nuke_server(manager_port=None):
410 379 server instance that will communicate with a NukeCommandManager
411 380 on the specified port. Must be called from within Nuke.
412 381 '''
413   - import nukeCommandServer
414   - nukeCommandServer.NukeManagedServer(manager_port=manager_port)
  382 + import nukeExternalControl.server as comServer
  383 + comServer.NukeManagedServer(manager_port=manager_port)
415 384
416 385
417 386 if __name__ == '__main__':
28 nukeExternalControl/common.py
... ... @@ -0,0 +1,28 @@
  1 +SOCKET_BUFFER_SIZE = 4096
  2 +MAX_SOCKET_BYTES = 2048
  3 +
  4 +# These constants set the default port range for any automatic searches.
  5 +DEFAULT_START_PORT = 54200
  6 +DEFAULT_END_PORT = 54300
  7 +
  8 +# This constant should be set to whatever absolute or relative call your system
  9 +# uses to launch Nuke (excluding any flags or arguments).
  10 +NUKE_EXEC = 'Nuke'
  11 +
  12 +# Safe type lists for pickling. Objects whose types are not included in one
  13 +# of these lists will be represented by proxy objects on the client side.
  14 +basicTypes = [int, float, complex, str, unicode, buffer, xrange, bool, type(None)]
  15 +listTypes = [list, tuple, set, frozenset]
  16 +dictTypes = [dict]
  17 +
  18 +class NukeLicenseError(StandardError):
  19 + pass
  20 +
  21 +class NukeConnectionError(StandardError):
  22 + pass
  23 +
  24 +class NukeManagerError(NukeConnectionError):
  25 + pass
  26 +
  27 +class NukeServerError(NukeConnectionError):
  28 + pass
19 nukeCommandServer.py → nukeExternalControl/server.py
... ... @@ -1,5 +1,5 @@
1 1 '''
2   -This script defines the server-side classes for the Nuke command server interface.
  2 +This module defines the server-side classes for the Nuke command server interface.
3 3
4 4 It can also be passed as an executable to automatically start server instances.
5 5 '''
@@ -10,14 +10,7 @@
10 10 import imp
11 11 import nuke
12 12
13   -basicTypes = [int, float, complex, str, unicode, buffer, xrange, bool, type(None)]
14   -listTypes = [list, tuple, set, frozenset]
15   -dictTypes = [dict]
16   -
17   -MAX_SOCKET_BYTES = 2048
18   -
19   -class NukeConnectionError(StandardError):
20   - pass
  13 +from nukeExternalControl.common import *
21 14
22 15 def nuke_command_server():
23 16 t = threading.Thread(None, NukeInternal)
@@ -30,13 +23,12 @@ def __init__(self, port=None):
30 23 self._next_object_id = 0
31 24 self.port = port
32 25 self.bound_port = False
33   - self.buffer_size = 4096
34 26
35 27 host = ''
36 28 backlog = 5
37 29 if not self.port:
38   - start_port = 54200
39   - end_port = 54300
  30 + start_port = DEFAULT_START_PORT
  31 + end_port = DEFAULT_END_PORT
40 32 else:
41 33 start_port = end_port = self.port
42 34
@@ -64,7 +56,7 @@ def start_server(self, sock):
64 56 while 1:
65 57 client, address = sock.accept()
66 58 try:
67   - data = client.recv(self.buffer_size)
  59 + data = client.recv(SOCKET_BUFFER_SIZE)
68 60 if data:
69 61 result = self.receive(data)
70 62 client.send(result)
@@ -240,5 +232,6 @@ def manager_callback(self, status):
240 232 if not status:
241 233 raise NukeConnectionError("Cannot find port to bind to")
242 234
  235 +
243 236 if __name__ == '__main__':
244 237 NukeInternal()

0 comments on commit 3baa585

Please sign in to comment.
Something went wrong with that request. Please try again.