<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import subprocess <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def __str__ ( self ) : <EOL> return str ( self . message ) <EOL> class ClusterError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message ) : <EOL> self . message = message <EOL> class Cluster : <EOL> def __init__ ( self , options ) : <EOL> try : <EOL> self . mPriority = options . cluster_priority <EOL> except AttributeError : <EOL> self . mPriority = None <EOL> try : <EOL> self . mQueue = options . cluster_queue <EOL> except AttributeError : <EOL> self . mCluster = None <EOL> try : <EOL> self . mNumJobs = options . cluster_num_jobs <EOL> except AttributeError : <EOL> self . mNumJobs = None <EOL> def addJob ( self , statement ) : <EOL> self . mJobs . append ( statement ) <EOL> def runJobs ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mResults = [ ] <EOL> if self . mNumJobs : <EOL> chunks = range ( <NUM_LIT:0> , len ( self . mJobs ) , self . mNumJobs ) <EOL> chunks . append ( len ( self . mJobs ) ) <EOL> else : <EOL> chunks = [ <NUM_LIT:0> , len ( self . mJobs ) ] <EOL> for x in range ( len ( chunks ) - <NUM_LIT:1> ) : <EOL> pids = [ ] <EOL> for job in self . mJobs [ chunks [ x ] : chunks [ x + <NUM_LIT:1> ] ] : <EOL> pid = os . fork ( ) <EOL> if not pid : <EOL> os . nice ( <NUM_LIT:20> ) <EOL> out , err = self . __runJob ( job ) <EOL> self . mResults . append ( ( out , err ) ) <EOL> break <EOL> else : <EOL> pids . append ( pid ) <EOL> for pid in pids : <EOL> os . waitpid ( pid , <NUM_LIT:0> ) <EOL> return self . mResults <EOL> def runJob ( self , job ) : <EOL> """<STR_LIT>""" <EOL> s = subprocess . Popen ( self . getStatement ( job ) , <EOL> shell = True , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> cwd = os . curdir , <EOL> close_fds = True ) <EOL> ( out , err ) = s . communicate ( ) <EOL> if s . returncode != <NUM_LIT:0> : <EOL> raise ClusterError ( <EOL> "<STR_LIT>" % ( job , out , err ) ) <EOL> return out , err <EOL> def getStatement ( self , job ) : <EOL> return job <EOL> def setPriority ( self , priority ) : <EOL> self . mPriority = priority <EOL> def setQueue ( self , queue ) : <EOL> self . mQueue = queue <EOL> class ClusterSGE ( Cluster ) : <EOL> def __init__ ( self , * args ) : <EOL> Cluster . __init__ ( self , * args ) <EOL> if not self . mQueue : <EOL> self . mQueue = "<STR_LIT>" <EOL> if not self . mPriority : <EOL> self . mPriority = - <NUM_LIT:10> <EOL> self . mExportedVariables = ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . mVariables = [ ] <EOL> for x in self . mExportedVariables : <EOL> try : <EOL> self . mVariables . append ( ( "<STR_LIT>" % ( x , os . environ [ x ] ) ) ) <EOL> except KeyError : <EOL> pass <EOL> self . mVariables = "<STR_LIT:U+0020>" . join ( self . mVariables ) <EOL> self . mOptions = "<STR_LIT>" <EOL> self . mName = os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> if not self . mName : <EOL> self . mName = "<STR_LIT>" <EOL> def getStatement ( self , job ) : <EOL> return "<STR_LIT>" % ( self . mQueue , <EOL> self . mPriority , <EOL> self . mName , <EOL> self . mVariables , <EOL> self . mOptions , <EOL> job ) </s>
