<s> import os <EOL> import tractor . api . author as author <EOL> import IECore <EOL> import Gaffer <EOL> import GafferDispatch <EOL> class TractorDispatcher ( GafferDispatch . Dispatcher ) : <EOL> def __init__ ( self , name = "<STR_LIT>" ) : <EOL> GafferDispatch . Dispatcher . __init__ ( self , name ) <EOL> self [ "<STR_LIT>" ] = Gaffer . StringPlug ( defaultValue = '<STR_LIT>' ) <EOL> self [ "<STR_LIT>" ] = Gaffer . StringPlug ( ) <EOL> @ classmethod <EOL> def preSpoolSignal ( cls ) : <EOL> return cls . __preSpoolSignal <EOL> __preSpoolSignal = Gaffer . Signal2 ( ) <EOL> def _doDispatch ( self , rootBatch ) : <EOL> dispatchData = { } <EOL> dispatchData [ "<STR_LIT>" ] = rootBatch . preTasks ( ) [ <NUM_LIT:0> ] . node ( ) . scriptNode ( ) <EOL> dispatchData [ "<STR_LIT>" ] = os . path . join ( self . jobDirectory ( ) , os . path . basename ( dispatchData [ "<STR_LIT>" ] [ "<STR_LIT>" ] . getValue ( ) ) or "<STR_LIT>" ) <EOL> dispatchData [ "<STR_LIT>" ] = { } <EOL> dispatchData [ "<STR_LIT>" ] . serialiseToFile ( dispatchData [ "<STR_LIT>" ] ) <EOL> context = Gaffer . Context . current ( ) <EOL> job = author . Job ( <EOL> title = context . substitute ( self [ "<STR_LIT>" ] . getValue ( ) ) or "<STR_LIT>" , <EOL> service = context . substitute ( self [ "<STR_LIT>" ] . getValue ( ) ) , <EOL> envkey = context . substitute ( self [ "<STR_LIT>" ] . getValue ( ) ) . split ( ) , <EOL> ) <EOL> batchesToTasks = { } <EOL> for upstreamBatch in rootBatch . preTasks ( ) : <EOL> self . __buildJobWalk ( job , upstreamBatch , dispatchData ) <EOL> self . preSpoolSignal ( ) ( self , job ) <EOL> with open ( self . jobDirectory ( ) + "<STR_LIT>" , "<STR_LIT:w>" ) as alf : <EOL> alf . write ( "<STR_LIT>" + Gaffer . About . versionString ( ) + "<STR_LIT>" ) <EOL> alf . write ( job . asTcl ( ) ) <EOL> job . spool ( block = True ) <EOL> def __buildJobWalk ( self , tractorParent , batch , dispatchData ) : <EOL> task = self . __acquireTask ( batch , dispatchData ) <EOL> tractorParent . addChild ( task ) <EOL> if batch . blindData ( ) . get ( "<STR_LIT>" ) : <EOL> return <EOL> for upstreamBatch in batch . preTasks ( ) : <EOL> self . __buildJobWalk ( task , upstreamBatch , dispatchData ) <EOL> batch . blindData ( ) [ "<STR_LIT>" ] = IECore . BoolData ( True ) <EOL> def __acquireTask ( self , batch , dispatchData ) : <EOL> task = dispatchData [ "<STR_LIT>" ] . get ( batch ) <EOL> if task is not None : <EOL> return task <EOL> nodeName = batch . node ( ) . relativeName ( dispatchData [ "<STR_LIT>" ] ) <EOL> frames = str ( IECore . frameListFromList ( [ int ( x ) for x in batch . frames ( ) ] ) ) <EOL> task = author . Task ( title = nodeName + "<STR_LIT:U+0020>" + frames ) <EOL> args = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , dispatchData [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" , nodeName , <EOL> "<STR_LIT>" , frames , <EOL> ] <EOL> scriptContext = dispatchData [ "<STR_LIT>" ] . context ( ) <EOL> contextArgs = [ ] <EOL> for entry in [ k for k in batch . context ( ) . keys ( ) if k != "<STR_LIT>" and not k . startswith ( "<STR_LIT>" ) ] : <EOL> if entry not in scriptContext . keys ( ) or batch . context ( ) [ entry ] != scriptContext [ entry ] : <EOL> contextArgs . extend ( [ "<STR_LIT:->" + entry , repr ( batch . context ( ) [ entry ] ) ] ) <EOL> if contextArgs : <EOL> args . extend ( [ "<STR_LIT>" ] + contextArgs ) <EOL> command = author . Command ( argv = args ) <EOL> task . addCommand ( command ) <EOL> tractorPlug = batch . node ( ) [ "<STR_LIT>" ] . getChild ( "<STR_LIT>" ) <EOL> if tractorPlug is not None : <EOL> command . service = batch . context ( ) . substitute ( tractorPlug [ "<STR_LIT>" ] . getValue ( ) ) <EOL> command . tags = batch . context ( ) . substitute ( tractorPlug [ "<STR_LIT>" ] . getValue ( ) ) . split ( ) <EOL> dispatchData [ "<STR_LIT>" ] [ batch ] = task <EOL> return task <EOL> @ staticmethod <EOL> def _setupPlugs ( parentPlug ) : <EOL> if "<STR_LIT>" in parentPlug : <EOL> return <EOL> parentPlug [ "<STR_LIT>" ] = Gaffer . Plug ( flags = Gaffer . Plug . Flags . Default | Gaffer . Plug . Flags . Dynamic ) <EOL> parentPlug [ "<STR_LIT>" ] [ "<STR_LIT>" ] = Gaffer . StringPlug ( flags = Gaffer . Plug . Flags . Default | Gaffer . Plug . Flags . Dynamic ) <EOL> parentPlug [ "<STR_LIT>" ] [ "<STR_LIT>" ] = Gaffer . StringPlug ( flags = Gaffer . Plug . Flags . Default | Gaffer . Plug . Flags . Dynamic ) <EOL> IECore . registerRunTimeTyped ( TractorDispatcher , typeName = "<STR_LIT>" ) <EOL> GafferDispatch . Dispatcher . registerDispatcher ( "<STR_LIT>" , TractorDispatcher , TractorDispatcher . _setupPlugs ) </s>
