Permalink
Browse files

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...
1 parent ea86519 commit 3baa58583ce9c4b6cdb3c9f1168fe6e15864ffe5 Nathan Rusch committed May 4, 2011
No changes.
@@ -1,8 +1,7 @@
'''
-This script defines the client-side classes for the Nuke command server interface.
+This module defines the client-side classes for the Nuke command server interface.
-It also functions as an executable for the purposes of launching NukeCommandManager
-instances.
+It also functions as an executable to launch NukeCommandManager instances.
'''
import inspect
@@ -12,31 +11,8 @@
import sys
import threading
import time
-from os import devnull
-
-basicTypes = [int, float, complex, str, unicode, buffer, xrange, bool, type(None)]
-listTypes = [list, tuple, set, frozenset]
-dictTypes = [dict]
-
-# This constant should be set to whatever absolute or
-# relative call your system uses to launch Nuke (excluding
-# any flags or arguments).
-NUKE_EXEC = 'Nuke'
-
-MAX_SOCKET_BYTES = 2048
-
-class NukeLicenseError(StandardError):
- pass
-
-class NukeConnectionError(StandardError):
- pass
-
-class NukeManagerError(NukeConnectionError):
- pass
-
-class NukeServerError(NukeConnectionError):
- pass
+from nukeExternalControl.common import *
class NukeConnection():
'''
@@ -52,8 +28,8 @@ def __init__(self, port=None, host="localhost", instance=0):
self._host = host
self.is_active = False
if not port:
- start_port = 54200 + instance
- end_port = 54300
+ start_port = DEFAULT_START_PORT + instance
+ end_port = DEFAULT_END_PORT
self._port = self.find_connection_port(start_port, end_port)
if self._port == -1:
raise NukeConnectionError("Connection with Nuke failed")
@@ -72,13 +48,11 @@ def find_connection_port(self, start_port, end_port):
return -1
def send(self, data):#
- size = 4096
-
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self._host, self._port))
s.send(data)
- result = s.recv(size)
+ result = s.recv(SOCKET_BUFFER_SIZE)
s.close()
except socket.error:
raise NukeConnectionError("Connection with Nuke failed")
@@ -272,8 +246,8 @@ class NukeCommandManager():
available OS-assigned port.
When the manager's __enter__ method is called, the server
- subprocess is started. The manager then waits for the managed
- server to call back with its status and bound port.
+ subprocess is started. The manager then waits for the server
+ to call back with its status and bound port.
A NukeConnection instance is then started using the port number
returned by the managed server's callback. This instance is
@@ -286,9 +260,6 @@ class NukeCommandManager():
its companion server the 'shutdown' signal. This will cause the
server to send back its shutdown message, close the connection to
the client, and exit cleanly.
-
- The __exit__ method then waits for the server thread to exit by
- calling its '.join()' method.
'''
def __init__(self, license_retry_count=5, license_retry_delay=5):
self.manager_port = -1
@@ -334,7 +305,6 @@ def __exit__(self, type, value, traceback):
self.nuke_stdout, self.nuke_stderr = self.serverProc.communicate()
def start_server(self):
- bufsize = 4096
# Make sure the port number has a trailing space... this is a bug in Nuke's
# Python argument parsing (logged with The Foundry as Bug 17918)
procArgs = ([NUKE_EXEC, '-t', '-m', '1', '--', inspect.getabsfile(self.__class__), '%d ' % self.manager_port],)
@@ -347,15 +317,15 @@ def start_server(self):
try:
while True:
try:
- # This will time out after 10 seconds based on the socket settings
+ # Times out after 10 seconds based on the socket settings
server, address = self.manager_socket.accept()
except socket.timeout:
retCode = self.serverProc.poll()
if retCode == 100: # License failure.
raise NukeLicenseError
else: # Nuke is either still running or dead for other reasons.
raise NukeManagerError("Server process failed to start properly.")
- data = server.recv(bufsize)
+ data = server.recv(SOCKET_BUFFER_SIZE)
if data:
serverData = pickle.loads(data)
server.close()
@@ -390,13 +360,12 @@ def shutdown_server(self):
client could not be initialized.
Returns the server's shutdown message.
'''
- bufsize = 1024 * 1024
packet = {'action':'shutdown', 'id':-1, 'parameters':None}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('', self.server_port))
s.send(pickle.dumps(packet))
- result = s.recv(bufsize)
+ result = s.recv(SOCKET_BUFFER_SIZE)
s.close()
return pickle.loads(result)
except socket.error:
@@ -410,8 +379,8 @@ def start_managed_nuke_server(manager_port=None):
server instance that will communicate with a NukeCommandManager
on the specified port. Must be called from within Nuke.
'''
- import nukeCommandServer
- nukeCommandServer.NukeManagedServer(manager_port=manager_port)
+ import nukeExternalControl.server as comServer
+ comServer.NukeManagedServer(manager_port=manager_port)
if __name__ == '__main__':
@@ -0,0 +1,28 @@
+SOCKET_BUFFER_SIZE = 4096
+MAX_SOCKET_BYTES = 2048
+
+# These constants set the default port range for any automatic searches.
+DEFAULT_START_PORT = 54200
+DEFAULT_END_PORT = 54300
+
+# This constant should be set to whatever absolute or relative call your system
+# uses to launch Nuke (excluding any flags or arguments).
+NUKE_EXEC = 'Nuke'
+
+# Safe type lists for pickling. Objects whose types are not included in one
+# of these lists will be represented by proxy objects on the client side.
+basicTypes = [int, float, complex, str, unicode, buffer, xrange, bool, type(None)]
+listTypes = [list, tuple, set, frozenset]
+dictTypes = [dict]
+
+class NukeLicenseError(StandardError):
+ pass
+
+class NukeConnectionError(StandardError):
+ pass
+
+class NukeManagerError(NukeConnectionError):
+ pass
+
+class NukeServerError(NukeConnectionError):
+ pass
@@ -1,5 +1,5 @@
'''
-This script defines the server-side classes for the Nuke command server interface.
+This module defines the server-side classes for the Nuke command server interface.
It can also be passed as an executable to automatically start server instances.
'''
@@ -10,14 +10,7 @@
import imp
import nuke
-basicTypes = [int, float, complex, str, unicode, buffer, xrange, bool, type(None)]
-listTypes = [list, tuple, set, frozenset]
-dictTypes = [dict]
-
-MAX_SOCKET_BYTES = 2048
-
-class NukeConnectionError(StandardError):
- pass
+from nukeExternalControl.common import *
def nuke_command_server():
t = threading.Thread(None, NukeInternal)
@@ -30,13 +23,12 @@ def __init__(self, port=None):
self._next_object_id = 0
self.port = port
self.bound_port = False
- self.buffer_size = 4096
host = ''
backlog = 5
if not self.port:
- start_port = 54200
- end_port = 54300
+ start_port = DEFAULT_START_PORT
+ end_port = DEFAULT_END_PORT
else:
start_port = end_port = self.port
@@ -64,7 +56,7 @@ def start_server(self, sock):
while 1:
client, address = sock.accept()
try:
- data = client.recv(self.buffer_size)
+ data = client.recv(SOCKET_BUFFER_SIZE)
if data:
result = self.receive(data)
client.send(result)
@@ -240,5 +232,6 @@ def manager_callback(self, status):
if not status:
raise NukeConnectionError("Cannot find port to bind to")
+
if __name__ == '__main__':
NukeInternal()

0 comments on commit 3baa585

Please sign in to comment.