def testRequirements(self): n1 = Gaffer.ExecutableOpHolder() n2 = Gaffer.ExecutableOpHolder() n2a = Gaffer.ExecutableOpHolder() n2b = Gaffer.ExecutableOpHolder() r1 = Gaffer.Plug(name="r1") n1['requirements'].addChild(r1) r1.setInput(n2['requirement']) r1 = Gaffer.Plug(name="r1") n2['requirements'].addChild(r1) r1.setInput(n2a['requirement']) r2 = Gaffer.Plug(name="r2") n2['requirements'].addChild(r2) r2.setInput(n2b['requirement']) c = Gaffer.Context() self.assertEqual(n2a.executionRequirements(c), []) self.assertEqual(n2b.executionRequirements(c), []) n2Requirements = n2.executionRequirements(c) self.assertEqual(n2Requirements[0].node, n2a) self.assertEqual(n2Requirements[0].context, c) self.assertEqual(n2Requirements[1].node, n2b) self.assertEqual(n2Requirements[1].context, c) t1 = Gaffer.ExecutableNode.Task(n2a, c) t2 = Gaffer.ExecutableNode.Task(n2b, c) self.assertEqual(n2Requirements[0], t1) self.assertEqual(n2Requirements[1], t2) self.assertEqual( len(set(n2.executionRequirements(c)).difference([t1, t2])), 0) self.assertEqual(n1.executionRequirements(c), [Gaffer.ExecutableNode.Task(n2, c)])
def testDispatchIdenticalTasks(self): dispatcher = Gaffer.Dispatcher.create("testDispatcher") # Create a tree of dependencies for execution: # n1 requires: # - n2 requires: # -n2a # -n2b op1 = TestOp("1", dispatcher.log) s = Gaffer.ScriptNode() s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised(op1) s["n2"] = Gaffer.ExecutableOpHolder() s["n2"].setParameterised(op1) s["n2a"] = Gaffer.ExecutableOpHolder() s["n2a"].setParameterised(op1) s["n2b"] = Gaffer.ExecutableOpHolder() s["n2b"].setParameterised(op1) s["n1"]['requirements'][0].setInput(s["n2"]['requirement']) s["n2"]['requirements'][0].setInput(s["n2a"]['requirement']) s["n2"]['requirements'][1].setInput(s["n2b"]['requirement']) # even though all tasks are identical, we still execute them all dispatcher.dispatch([s["n1"]]) shutil.rmtree(dispatcher.jobDirectory()) self.assertEqual(op1.counter, 4) self.assertEqual(dispatcher.log, [op1, op1, op1, op1]) # Executing them all should do the same, with no duplicates dispatcher.dispatch([s["n2"], s["n2b"], s["n1"], s["n2a"]]) shutil.rmtree(dispatcher.jobDirectory()) self.assertEqual(op1.counter, 8) self.assertEqual(dispatcher.log, [op1, op1, op1, op1])
def testDispatchIdenticalTasks( self ) : dispatcher = Gaffer.Dispatcher.dispatcher( "testDispatcher" ) # Create a tree of dependencies for execution: # n1 requires: # - n2 requires: # -n2a # -n2b op1 = TestOp("1", dispatcher.log) s = Gaffer.ScriptNode() s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) s["n2"] = Gaffer.ExecutableOpHolder() s["n2"].setParameterised( op1 ) s["n2a"] = Gaffer.ExecutableOpHolder() s["n2a"].setParameterised( op1 ) s["n2b"] = Gaffer.ExecutableOpHolder() s["n2b"].setParameterised( op1 ) s["n1"]['requirements'][0].setInput( s["n2"]['requirement'] ) s["n2"]['requirements'][0].setInput( s["n2a"]['requirement'] ) s["n2"]['requirements'][1].setInput( s["n2b"]['requirement'] ) # Executing n1 should only execute once, because all tasks are identical dispatcher.dispatch( [ s["n1"] ] ) self.assertEqual( op1.counter, 1 ) self.assertEqual( dispatcher.log, [ op1 ] ) # Executing them all should still only execute one, because all tasks are identical dispatcher.dispatch( [ s["n2"], s["n2b"], s["n1"], s["n2a"] ] ) self.assertEqual( op1.counter, 2 ) self.assertEqual( dispatcher.log, [ op1 ] )
def testTaskSet( self ) : # an empty ExecutableOpHolder doesn't actually compute anything, so all tasks are the same c = Gaffer.Context() n = Gaffer.ExecutableOpHolder() t1 = Gaffer.ExecutableNode.Task( n, c ) t2 = Gaffer.ExecutableNode.Task( n, c ) self.assertEqual( t1, t2 ) c2 = Gaffer.Context() c2["a"] = 2 t3 = Gaffer.ExecutableNode.Task( n, c2 ) self.assertEqual( t1, t3 ) n2 = Gaffer.ExecutableOpHolder() t4 = Gaffer.ExecutableNode.Task( n2, c2 ) self.assertEqual( t1, t4 ) t5 = Gaffer.ExecutableNode.Task( n2, c ) self.assertEqual( t1, t5 ) s = set( [ t1, t2, t3, t4, t4, t4, t1, t2, t4, t3, t2 ] ) # there should only be 1 task because they all have identical results self.assertEqual( len(s), 1 ) self.assertEqual( s, set( [ t1 ] ) ) self.assertTrue( t1 in s ) self.assertTrue( t2 in s ) self.assertTrue( t3 in s ) self.assertTrue( t4 in s ) # even t5 is in there, because it's really the same task self.assertTrue( t5 in s ) # MyNode.hash() depends on the context time, so tasks will vary my = ExecutableNodeTest.MyNode( True ) c.setFrame( 1 ) t1 = Gaffer.ExecutableNode.Task( my, c ) t2 = Gaffer.ExecutableNode.Task( my, c ) self.assertEqual( t1, t2 ) c2 = Gaffer.Context() c2.setFrame( 2 ) t3 = Gaffer.ExecutableNode.Task( my, c2 ) self.assertNotEqual( t1, t3 ) my2 = ExecutableNodeTest.MyNode( True ) t4 = Gaffer.ExecutableNode.Task( my2, c2 ) self.assertNotEqual( t1, t4 ) self.assertEqual( t3, t4 ) t5 = Gaffer.ExecutableNode.Task( my2, c ) self.assertEqual( t1, t5 ) self.assertNotEqual( t3, t5 ) s = set( [ t1, t2, t3, t4, t4, t4, t1, t2, t4, t3, t2 ] ) # t1 and t3 are the only distinct tasks self.assertEqual( len(s), 2 ) self.assertEqual( s, set( [ t1, t3 ] ) ) # but they still all have equivalent tasks in the set self.assertTrue( t1 in s ) self.assertTrue( t2 in s ) self.assertTrue( t3 in s ) self.assertTrue( t4 in s ) self.assertTrue( t5 in s )
def testPlugs( self ) : n = Gaffer.ExecutableOpHolder() self.assertEqual( n['dispatcher'].getChild( 'testDispatcherPlug' ), None ) Gaffer.Dispatcher.registerDispatcher( "testDispatcherWithCustomPlugs", DispatcherTest.MyDispatcher, setupPlugsFn = DispatcherTest.MyDispatcher._doSetupPlugs ) n2 = Gaffer.ExecutableOpHolder() self.assertTrue( isinstance( n2['dispatcher'].getChild( 'testDispatcherPlug' ), Gaffer.IntPlug ) ) self.assertEqual( n2['dispatcher']['testDispatcherPlug'].direction(), Gaffer.Plug.Direction.In )
def testType(self): n = Gaffer.ExecutableOpHolder() self.assertEqual(n.typeName(), "Gaffer::ExecutableOpHolder") self.failUnless( n.isInstanceOf(Gaffer.ParameterisedHolderNode.staticTypeId())) self.failUnless(n.isInstanceOf(Gaffer.Node.staticTypeId()))
def testPlugs(self): n = Gaffer.ExecutableOpHolder() n['dispatcher'].direction() n['dispatcher']['testDispatcherPlug'].direction() self.assertEqual(n['dispatcher']['testDispatcherPlug'].direction(), Gaffer.Plug.Direction.In)
def testExecutablePlugs(self): n = Gaffer.ExecutableOpHolder() self.assertEqual(n['requirement'].direction(), Gaffer.Plug.Direction.Out) self.assertEqual(n['requirements'].direction(), Gaffer.Plug.Direction.In)
def testCancelDispatch( self ) : def onlyRunOnce( dispatcher, nodes ) : if len(dispatcher.log) : return True return False connection = Gaffer.Dispatcher.preDispatchSignal().connect( onlyRunOnce ) dispatcher = DispatcherTest.MyDispatcher() op1 = TestOp( "1", dispatcher.log ) s = Gaffer.ScriptNode() s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) # never run self.assertEqual( len(dispatcher.log), 0 ) self.assertEqual( op1.counter, 0 ) # runs the first time dispatcher.dispatch( [ s["n1"] ] ) self.assertEqual( len(dispatcher.log), 1 ) self.assertEqual( op1.counter, 1 ) # never runs again dispatcher.dispatch( [ s["n1"] ] ) self.assertEqual( len(dispatcher.log), 1 ) self.assertEqual( op1.counter, 1 )
def testContextSubstitutions(self): n = Gaffer.ExecutableOpHolder() op = TestOp() n.setParameterised(op) self.assertEqual(op.counter, 0) self.assertEqual(op.stringValue, "") c = Gaffer.Context() c.setFrame(1) n.execute([c]) self.assertEqual(op.counter, 1) self.assertEqual(op.stringValue, "") n["parameters"]["stringParm"].setValue("${frame}") n.execute([c]) self.assertEqual(op.counter, 2) self.assertEqual(op.stringValue, "1") # variable outside the context (and environment) get removed n["parameters"]["stringParm"].setValue("${test}") n.execute([c]) self.assertEqual(op.counter, 3) self.assertEqual(op.stringValue, "") c["test"] = "passed" n.execute([c]) self.assertEqual(op.counter, 4) self.assertEqual(op.stringValue, "passed")
def testDispatcherSignals( self ) : class CapturingSlot2( list ) : def __init__( self, *signals ) : self.__connections = [] for s in signals : self.__connections.append( s.connect( Gaffer.WeakMethod( self.__slot ) ) ) def __slot( self, d, nodes ) : self.append( (d,nodes) ) preCs = CapturingSlot2( Gaffer.Dispatcher.preDispatchSignal() ) self.assertEqual( len( preCs ), 0 ) postCs = GafferTest.CapturingSlot( Gaffer.Dispatcher.postDispatchSignal() ) self.assertEqual( len( postCs ), 0 ) log = list() op1 = TestOp("1", log) s = Gaffer.ScriptNode() s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) dispatcher = DispatcherTest.MyDispatcher() dispatcher.dispatch( [ s["n1"] ] ) self.assertEqual( len( preCs ), 1 ) self.failUnless( preCs[0][0].isSame( dispatcher ) ) self.assertEqual( preCs[0][1], [ s["n1"] ] ) self.assertEqual( len( postCs ), 1 ) self.failUnless( postCs[0][0].isSame( dispatcher ) ) self.assertEqual( postCs[0][1], [ s["n1"] ] )
def testExecutionHash(self): c = Gaffer.Context() c.setFrame(1) c2 = Gaffer.Context() c2.setFrame(2) n = Gaffer.ExecutableOpHolder() op = TestOp() # output doesn't vary until we set an op self.assertEqual(n.executionHash(c), IECore.MurmurHash()) # output varies if any op is set n.setParameterised(op) self.assertNotEqual(n.executionHash(c), IECore.MurmurHash()) # output doesn't vary by time unless ${frame} is used by the parameters self.assertEqual(n.executionHash(c), n.executionHash(c2)) # output varies by time because ${frame} is used by the parameters n["parameters"]["stringParm"].setValue("${frame}") self.assertNotEqual(n.executionHash(c), n.executionHash(c2)) # output varies any context entry used by the parameters n["parameters"]["stringParm"].setValue("${test}") self.assertEqual(n.executionHash(c), n.executionHash(c2)) c["test"] = "a" self.assertNotEqual(n.executionHash(c), n.executionHash(c2)) c2["test"] = "b" self.assertNotEqual(n.executionHash(c), n.executionHash(c2)) c2["test"] = "a" self.assertEqual(n.executionHash(c), n.executionHash(c2))
def testDifferentScripts( self ) : dispatcher = DispatcherTest.MyDispatcher() op1 = TestOp("1", dispatcher.log) s = Gaffer.ScriptNode() s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) op2 = TestOp("2", dispatcher.log) s2 = Gaffer.ScriptNode() s2["n2"] = Gaffer.ExecutableOpHolder() s2["n2"].setParameterised( op2 ) self.assertRaises( RuntimeError, dispatcher.dispatch, [ s["n1"], s2["n2"] ] ) self.assertEqual( op1.counter, 0 ) self.assertEqual( op2.counter, 0 )
def testExecute(self): n = Gaffer.ExecutableOpHolder() op = TestOp() n.setParameterised(op) script = n.scriptNode() self.assertEqual(op.counter, 0) n.execute([Gaffer.Context()]) self.assertEqual(op.counter, 1)
def testExecutableMethods(self): n = Gaffer.ExecutableOpHolder() opSpec = GafferTest.ParameterisedHolderTest.classSpecification( "primitive/renameVariables", "IECORE_OP_PATHS")[:-1] n.setOp(*opSpec) c = Gaffer.Context() h = n.executionHash(c) self.assertEqual(n.executionHash(c), h)
def testNoScript( self ) : dispatcher = DispatcherTest.MyDispatcher() op1 = TestOp("1", dispatcher.log) n1 = Gaffer.ExecutableOpHolder() n1.setParameterised( op1 ) self.assertRaises( RuntimeError, dispatcher.dispatch, [ n1 ] ) self.assertEqual( op1.counter, 0 )
def testLocalDespatcher( self ) : log = list() op1 = TestOp("1", log) n1 = Gaffer.ExecutableOpHolder() n1.setParameterised( op1 ) Gaffer.Despatcher.despatcher('local').despatch( [ n1 ] ) self.assertEqual( op1.counter, 1 )
def testTaskComparison( self ) : c = Gaffer.Context() n = Gaffer.ExecutableOpHolder() t1 = Gaffer.ExecutableNode.Task( n, c ) t2 = Gaffer.ExecutableNode.Task( n, c ) c2 = Gaffer.Context() c2["a"] = 2 t3 = Gaffer.ExecutableNode.Task( n, c2 ) n2 = Gaffer.ExecutableOpHolder() t4 = Gaffer.ExecutableNode.Task( n2, c2 ) self.assertEqual( t1, t1 ) self.assertEqual( t1, t2 ) self.assertEqual( t2, t1 ) self.assertNotEqual( t1, t3 ) self.assertNotEqual( t3, t1 ) self.assertNotEqual( t3, t4 ) self.assertNotEqual( t4, t3 )
def testNoTask( self ) : s = Gaffer.ScriptNode() s["n1"] = Gaffer.ExecutableOpHolder() self.assertEqual( s["n1"].hash( s.context() ), IECore.MurmurHash() ) # It doesn't execute, because the executionHash is null dispatcher = Gaffer.Dispatcher.dispatcher( "testDispatcher" ) dispatcher.dispatch( [ s["n1"] ] ) self.assertEqual( dispatcher.log, [] )
def testNoScript(self): dispatcher = Gaffer.Dispatcher.create("testDispatcher") op1 = TestOp("1", dispatcher.log) n1 = Gaffer.ExecutableOpHolder() n1.setParameterised(op1) self.assertRaises(RuntimeError, dispatcher.dispatch, [n1]) self.assertEqual(dispatcher.jobDirectory(), "") self.assertEqual(op1.counter, 0)
def testDerivedClass( self ) : despatcher = DespatcherTest.MyDespatcher() op1 = TestOp("1", despatcher.log) n1 = Gaffer.ExecutableOpHolder() n1.setParameterised( op1 ) despatcher.despatch( [ n1 ] ) self.assertEqual( op1.counter, 1 )
def testDerivedClass( self ) : dispatcher = DispatcherTest.MyDispatcher() s = Gaffer.ScriptNode() op1 = TestOp("1", dispatcher.log) s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) dispatcher.dispatch( [ s["n1"] ] ) self.assertEqual( op1.counter, 1 )
def testDerivedClass(self): dispatcher = Gaffer.Dispatcher.create("testDispatcher") s = Gaffer.ScriptNode() op1 = TestOp("1", dispatcher.log) s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised(op1) dispatcher.dispatch([s["n1"]]) shutil.rmtree(dispatcher.jobDirectory()) self.assertEqual(op1.counter, 1)
def testBadJobDirectory( self ) : dispatcher = Gaffer.Dispatcher.create( "testDispatcher" ) self.assertEqual( dispatcher["jobName"].getValue(), "" ) self.assertEqual( dispatcher["jobsDirectory"].getValue(), "/tmp/dispatcherTest" ) s = Gaffer.ScriptNode() op1 = TestOp("1", dispatcher.log) s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) dispatcher.dispatch( [ s["n1"] ] ) jobDir = dispatcher.jobDirectory() self.assertEqual( jobDir, "/tmp/dispatcherTest/000000" ) self.assertTrue( os.path.exists( jobDir ) ) shutil.rmtree( jobDir )
def testSerialise(self): s = Gaffer.ScriptNode() s["n"] = Gaffer.ExecutableOpHolder() opSpec = GafferTest.ParameterisedHolderTest.classSpecification( "primitive/renameVariables", "IECORE_OP_PATHS")[:-1] s["n"].setOp(*opSpec) s2 = Gaffer.ScriptNode() s2.execute(s.serialise()) self.assertEqual(s["n"]["parameters"].keys(), s2["n"]["parameters"].keys())
def testDispatchBadCustomRange( self ) : dispatcher = DispatcherTest.MyDispatcher() dispatcher["framesMode"].setValue( Gaffer.Dispatcher.FramesMode.CustomRange ) dispatcher["frameRange"].setValue( "notAFrameRange" ) s = Gaffer.ScriptNode() op1 = TestOp("1", dispatcher.log) s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) self.assertRaises( RuntimeError, dispatcher.dispatch, [ s["n1"] ] ) self.assertEqual( op1.counter, 0 ) self.assertEqual( op1.frames, [] )
def testTaskConstructors( self ) : c = Gaffer.Context() n = Gaffer.ExecutableOpHolder() t = Gaffer.ExecutableNode.Task( n, c ) t2 = Gaffer.ExecutableNode.Task( n, c ) t3 = Gaffer.ExecutableNode.Task( t2 ) self.assertEqual( t.node(), n ) self.assertEqual( t.context(), c ) self.assertEqual( t2.node(), n ) self.assertEqual( t2.context(), c ) self.assertEqual( t3.node(), n ) self.assertEqual( t3.context(), c )
def testDispatchScriptRange( self ) : dispatcher = DispatcherTest.MyDispatcher() dispatcher["framesMode"].setValue( Gaffer.Dispatcher.FramesMode.ScriptRange ) s = Gaffer.ScriptNode() op1 = TestOp("1", dispatcher.log) s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) dispatcher.dispatch( [ s["n1"] ] ) frames = IECore.FrameRange( s["frameRange"]["start"].getValue(), s["frameRange"]["end"].getValue() ).asList() self.assertEqual( op1.counter, len(frames) ) self.assertEqual( op1.frames, frames )
def testDispatchCustomRange( self ) : dispatcher = DispatcherTest.MyDispatcher() dispatcher["framesMode"].setValue( Gaffer.Dispatcher.FramesMode.CustomRange ) frameList = IECore.FrameList.parse( "2-6x2" ) dispatcher["frameRange"].setValue( str(frameList) ) s = Gaffer.ScriptNode() op1 = TestOp("1", dispatcher.log) s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised( op1 ) dispatcher.dispatch( [ s["n1"] ] ) frames = frameList.asList() self.assertEqual( op1.counter, len(frames) ) self.assertEqual( op1.frames, frames )
def testFrameRangeOverride(self): class BinaryDispatcher(DispatcherTest.MyDispatcher): def frameRange(self, script, context): frameRange = Gaffer.Dispatcher.frameRange( self, script, context) if self["framesMode"].getValue( ) == Gaffer.Dispatcher.FramesMode.CurrentFrame: return frameRange return IECore.BinaryFrameList(frameRange) IECore.registerRunTimeTyped(BinaryDispatcher) dispatcher = BinaryDispatcher() dispatcher["jobsDirectory"].setValue("/tmp/dispatcherTest") frameList = IECore.FrameList.parse("1-10") dispatcher["frameRange"].setValue(str(frameList)) s = Gaffer.ScriptNode() op1 = TestOp("1", dispatcher.log) s["n1"] = Gaffer.ExecutableOpHolder() s["n1"].setParameterised(op1) dispatcher["framesMode"].setValue( Gaffer.Dispatcher.FramesMode.CurrentFrame) self.assertEqual( dispatcher.frameRange(s, s.context()), IECore.frameListFromList([int(s.context().getFrame())])) dispatcher["framesMode"].setValue( Gaffer.Dispatcher.FramesMode.FullRange) self.assertEqual(dispatcher.frameRange(s, s.context()), IECore.FrameList.parse("1-100b")) dispatcher["framesMode"].setValue( Gaffer.Dispatcher.FramesMode.CustomRange) binaryFrames = IECore.FrameList.parse("1-10b") self.assertEqual(dispatcher.frameRange(s, s.context()), binaryFrames) dispatcher.dispatch([s["n1"]]) self.assertEqual(op1.counter, len(frameList.asList())) self.assertNotEqual(op1.frames, frameList.asList()) self.assertEqual(op1.frames, binaryFrames.asList())