<s> """<STR_LIT>""" <EOL> from optparse import OptionParser <EOL> import os <EOL> import random <EOL> import shutil <EOL> from sonLib . bioio import getTempFile <EOL> from jobTree . scriptTree . target import Target <EOL> from jobTree . scriptTree . stack import Stack <EOL> from jobTree . test . sort . lib import merge , sort , copySubRangeOfFile , getMidPoint <EOL> def setup ( target , inputFile , N ) : <EOL> """<STR_LIT>""" <EOL> tempOutputFile = getTempFile ( rootDir = target . getGlobalTempDir ( ) ) <EOL> target . addChildTargetFn ( down , ( inputFile , <NUM_LIT:0> , os . path . getsize ( inputFile ) , N , tempOutputFile ) ) <EOL> target . setFollowOnFn ( cleanup , ( tempOutputFile , inputFile ) ) <EOL> def down ( target , inputFile , fileStart , fileEnd , N , outputFile ) : <EOL> """<STR_LIT>""" <EOL> if random . random ( ) > <NUM_LIT:0.5> : <EOL> raise RuntimeError ( ) <EOL> length = fileEnd - fileStart <EOL> target . logToMaster ( "<STR_LIT>" % ( length , inputFile ) ) <EOL> assert length >= <NUM_LIT:0> <EOL> if length > N : <EOL> midPoint = getMidPoint ( inputFile , fileStart , fileEnd ) <EOL> assert midPoint >= fileStart <EOL> assert midPoint + <NUM_LIT:1> < fileEnd <EOL> tempFile1 = getTempFile ( rootDir = target . getGlobalTempDir ( ) ) <EOL> tempFile2 = getTempFile ( rootDir = target . getGlobalTempDir ( ) ) <EOL> target . addChildTargetFn ( down , ( inputFile , fileStart , midPoint + <NUM_LIT:1> , N , tempFile1 ) ) <EOL> target . addChildTargetFn ( down , ( inputFile , midPoint + <NUM_LIT:1> , fileEnd , N , tempFile2 ) ) <EOL> target . setFollowOnTargetFn ( up , ( tempFile1 , tempFile2 , outputFile ) ) <EOL> else : <EOL> copySubRangeOfFile ( inputFile , fileStart , fileEnd , outputFile ) <EOL> sort ( outputFile ) <EOL> def up ( target , inputFile1 , inputFile2 , outputFile ) : <EOL> """<STR_LIT>""" <EOL> if random . random ( ) > <NUM_LIT:0.5> : <EOL> raise RuntimeError ( ) <EOL> merge ( inputFile1 , inputFile2 , outputFile ) <EOL> target . logToMaster ( "<STR_LIT>" % ( inputFile1 , inputFile2 ) ) <EOL> def cleanup ( tempOutputFile , outputFile ) : <EOL> """<STR_LIT>""" <EOL> if random . random ( ) > <NUM_LIT:0.5> : <EOL> raise RuntimeError ( ) <EOL> shutil . copyfile ( tempOutputFile , outputFile ) <EOL> def main ( ) : <EOL> parser = OptionParser ( ) <EOL> Stack . addJobTreeOptions ( parser ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT:N>" , <EOL> help = "<STR_LIT>" , <EOL> default = <NUM_LIT> ) <EOL> options , args = parser . parse_args ( ) <EOL> if options . fileToSort == None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if not os . path . exists ( options . fileToSort ) : <EOL> raise RuntimeError ( "<STR_LIT>" % options . fileToSort ) <EOL> if int ( options . N ) <= <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" % options . N ) <EOL> if len ( args ) != <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( args ) ) <EOL> i = Stack ( Target . makeTargetFn ( setup , ( options . fileToSort , int ( options . N ) ) ) ) . startJobTree ( options ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from jobTree . test . sort . scriptTreeTest_Sort import * <EOL> main ( ) </s>
