<s> import sys <EOL> import os <EOL> import time <EOL> from optparse import OptionParser <EOL> try : <EOL> import cPickle <EOL> except ImportError : <EOL> import pickle as cPickle <EOL> import xml . etree . cElementTree as ET <EOL> from sonLib . bioio import logger <EOL> from sonLib . bioio import setLogLevel <EOL> from sonLib . bioio import setLoggingFromOptions <EOL> from sonLib . bioio import getTempFile <EOL> from sonLib . bioio import getTempDirectory <EOL> from sonLib . bioio import system , absSymPath <EOL> from sonLib . bioio import getTotalCpuTimeAndMemoryUsage , getTotalCpuTime <EOL> from jobTree . src . jobTreeRun import addOptions <EOL> from jobTree . src . jobTreeRun import createJobTree <EOL> from jobTree . src . jobTreeRun import reloadJobTree <EOL> from jobTree . src . jobTreeRun import createFirstJob <EOL> from jobTree . src . jobTreeRun import loadEnvironment <EOL> from jobTree . src . master import mainLoop <EOL> from jobTree . scriptTree . target import Target <EOL> class Stack ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , target ) : <EOL> self . target = target <EOL> self . verifyTargetAttributesExist ( target ) <EOL> @ staticmethod <EOL> def getDefaultOptions ( ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( ) <EOL> Stack . addJobTreeOptions ( parser ) <EOL> options , args = parser . parse_args ( args = [ ] ) <EOL> assert len ( args ) == <NUM_LIT:0> <EOL> return options <EOL> @ staticmethod <EOL> def addJobTreeOptions ( parser ) : <EOL> """<STR_LIT>""" <EOL> addOptions ( parser ) <EOL> def startJobTree ( self , options ) : <EOL> """<STR_LIT>""" <EOL> self . verifyJobTreeOptions ( options ) <EOL> setLoggingFromOptions ( options ) <EOL> options . jobTree = absSymPath ( options . jobTree ) <EOL> if os . path . isdir ( options . jobTree ) : <EOL> config , batchSystem = reloadJobTree ( options . jobTree ) <EOL> else : <EOL> config , batchSystem = createJobTree ( options ) <EOL> command = self . makeRunnable ( options . jobTree ) <EOL> memory = self . getMemory ( ) <EOL> cpu = self . getCpu ( ) <EOL> createFirstJob ( command , config , memory = memory , cpu = cpu ) <EOL> loadEnvironment ( config ) <EOL> return mainLoop ( config , batchSystem ) <EOL> def makeRunnable ( self , tempDir ) : <EOL> pickleFile = getTempFile ( "<STR_LIT>" , tempDir ) <EOL> fileHandle = open ( pickleFile , '<STR_LIT:w>' ) <EOL> cPickle . dump ( self , fileHandle , cPickle . HIGHEST_PROTOCOL ) <EOL> fileHandle . close ( ) <EOL> i = set ( ) <EOL> for importString in self . target . importStrings : <EOL> i . add ( importString ) <EOL> classNames = "<STR_LIT:U+0020>" . join ( i ) <EOL> return "<STR_LIT>" % ( pickleFile , classNames ) <EOL> def getMemory ( self , defaultMemory = sys . maxint ) : <EOL> memory = self . target . getMemory ( ) <EOL> if memory == sys . maxint : <EOL> return defaultMemory <EOL> return memory <EOL> def getCpu ( self , defaultCpu = sys . maxint ) : <EOL> cpu = self . target . getCpu ( ) <EOL> if cpu == sys . maxint : <EOL> return defaultCpu <EOL> return cpu <EOL> def getLocalTempDir ( self ) : <EOL> self . tempDirAccessed = True <EOL> return self . localTempDir <EOL> def getGlobalTempDir ( self ) : <EOL> return getTempDirectory ( rootDir = self . globalTempDir ) <EOL> def execute ( self , job , stats , localTempDir , globalTempDir , <EOL> memoryAvailable , cpuAvailable , <EOL> defaultMemory , defaultCpu , depth ) : <EOL> self . tempDirAccessed = False <EOL> self . localTempDir = localTempDir <EOL> self . globalTempDir = globalTempDir <EOL> if stats != None : <EOL> startTime = time . time ( ) <EOL> startClock = getTotalCpuTime ( ) <EOL> baseDir = os . getcwd ( ) <EOL> self . target . setStack ( self ) <EOL> targetMemory = self . target . getMemory ( ) <EOL> if targetMemory != sys . maxint : <EOL> assert targetMemory <= memoryAvailable <EOL> targetCpu = self . target . getCpu ( ) <EOL> if targetCpu != sys . maxint : <EOL> assert targetCpu <= cpuAvailable <EOL> self . target . run ( ) <EOL> if os . getcwd ( ) != baseDir : <EOL> os . chdir ( baseDir ) <EOL> if self . tempDirAccessed : <EOL> system ( "<STR_LIT>" % self . localTempDir ) <EOL> self . tempDirAccessed = False <EOL> followOn = self . target . getFollowOn ( ) <EOL> if followOn is not None : <EOL> if self . target . isGlobalTempDirSet ( ) : <EOL> followOn . setGlobalTempDir ( self . target . getGlobalTempDir ( ) ) <EOL> followOnStack = Stack ( followOn ) <EOL> job . followOnCommands . append ( ( followOnStack . makeRunnable ( self . globalTempDir ) , <EOL> followOnStack . getMemory ( defaultMemory ) , <EOL> followOnStack . getCpu ( defaultCpu ) , <EOL> depth ) ) <EOL> newChildren = self . target . getChildren ( ) <EOL> newChildren . reverse ( ) <EOL> assert len ( job . children ) == <NUM_LIT:0> <EOL> while len ( newChildren ) > <NUM_LIT:0> : <EOL> childStack = Stack ( newChildren . pop ( ) ) <EOL> job . children . append ( ( childStack . makeRunnable ( self . globalTempDir ) , <EOL> childStack . getMemory ( defaultMemory ) , <EOL> childStack . getCpu ( defaultCpu ) ) ) <EOL> for childCommand , runTime in self . target . getChildCommands ( ) : <EOL> job . children . append ( ( childCommand , defaultMemory , defaultCpu ) ) <EOL> for message in self . target . getMasterLoggingMessages ( ) : <EOL> job . messages . append ( message ) <EOL> if stats != None : <EOL> stats = ET . SubElement ( stats , "<STR_LIT:target>" ) <EOL> stats . attrib [ "<STR_LIT:time>" ] = str ( time . time ( ) - startTime ) <EOL> totalCpuTime , totalMemoryUsage = getTotalCpuTimeAndMemoryUsage ( ) <EOL> stats . attrib [ "<STR_LIT>" ] = str ( totalCpuTime - startClock ) <EOL> stats . attrib [ "<STR_LIT:class>" ] = "<STR_LIT:.>" . join ( ( self . target . __class__ . __name__ , ) ) <EOL> stats . attrib [ "<STR_LIT>" ] = str ( totalMemoryUsage ) <EOL> def verifyJobTreeOptions ( self , options ) : <EOL> """<STR_LIT>""" <EOL> required = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for r in required : <EOL> if r not in vars ( options ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % r ) <EOL> if options . jobTree is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def verifyTargetAttributesExist ( self , target ) : <EOL> """<STR_LIT>""" <EOL> required = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for r in required : <EOL> if r not in vars ( target ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( r , target . __class__ . __name__ , <EOL> target . __class__ . __name__ ) ) </s>
