<s> from __future__ import absolute_import <EOL> import sys <EOL> import os <EOL> import logging <EOL> import resource <EOL> import logging . handlers <EOL> import tempfile <EOL> import random <EOL> import math <EOL> import shutil <EOL> from argparse import ArgumentParser <EOL> from optparse import OptionContainer , OptionGroup <EOL> import subprocess <EOL> import xml . etree . cElementTree as ET <EOL> from xml . dom import minidom <EOL> defaultLogLevel = logging . INFO <EOL> loggingFormatter = logging . Formatter ( '<STR_LIT>' ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> rootLogger = logging . getLogger ( ) <EOL> def getLogLevelString ( ) : <EOL> return logging . getLevelName ( rootLogger . getEffectiveLevel ( ) ) <EOL> __loggingFiles = [ ] <EOL> def addLoggingFileHandler ( fileName , rotatingLogging = False ) : <EOL> if fileName in __loggingFiles : <EOL> return <EOL> __loggingFiles . append ( fileName ) <EOL> if rotatingLogging : <EOL> handler = logging . handlers . RotatingFileHandler ( fileName , maxBytes = <NUM_LIT> , backupCount = <NUM_LIT:1> ) <EOL> else : <EOL> handler = logging . FileHandler ( fileName ) <EOL> rootLogger . addHandler ( handler ) <EOL> return handler <EOL> def setLogLevel ( level , logger = rootLogger ) : <EOL> """<STR_LIT>""" <EOL> level = level . upper ( ) <EOL> if level == "<STR_LIT>" : level = "<STR_LIT>" <EOL> numericLevel = logging . getLevelName ( level ) <EOL> assert logging . getLevelName ( numericLevel ) == level <EOL> logger . setLevel ( numericLevel ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . CRITICAL ) <EOL> def logFile ( fileName , printFunction = logger . info ) : <EOL> """<STR_LIT>""" <EOL> printFunction ( "<STR_LIT>" % fileName ) <EOL> shortName = fileName . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> fileHandle = open ( fileName , '<STR_LIT:r>' ) <EOL> line = fileHandle . readline ( ) <EOL> while line != '<STR_LIT>' : <EOL> if line [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> printFunction ( "<STR_LIT>" % ( shortName , line ) ) <EOL> line = fileHandle . readline ( ) <EOL> fileHandle . close ( ) <EOL> def logStream ( fileHandle , shortName , printFunction = logger . info ) : <EOL> """<STR_LIT>""" <EOL> printFunction ( "<STR_LIT>" % shortName ) <EOL> line = fileHandle . readline ( ) <EOL> while line != '<STR_LIT>' : <EOL> if line [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> printFunction ( "<STR_LIT>" % ( shortName , line ) ) <EOL> line = fileHandle . readline ( ) <EOL> fileHandle . close ( ) <EOL> def addLoggingOptions ( parser ) : <EOL> if isinstance ( parser , ArgumentParser ) : <EOL> group = parser . add_argument_group ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> _addLoggingOptions ( group . add_argument ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % parser . __class__ ) <EOL> supportedLogLevels = ( logging . CRITICAL , logging . ERROR , logging . WARNING , logging . INFO , logging . DEBUG ) <EOL> def _addLoggingOptions ( addOptionFn ) : <EOL> """<STR_LIT>""" <EOL> defaultLogLevelName = logging . getLevelName ( defaultLogLevel ) <EOL> addOptionFn ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> for level in supportedLogLevels : <EOL> levelName = logging . getLevelName ( level ) <EOL> levelNameCapitalized = levelName . capitalize ( ) <EOL> addOptionFn ( "<STR_LIT>" + levelNameCapitalized , dest = "<STR_LIT>" , <EOL> action = "<STR_LIT>" , const = levelName , <EOL> help = "<STR_LIT>" % ( levelName , defaultLogLevelName ) ) <EOL> addOptionFn ( "<STR_LIT>" , dest = "<STR_LIT>" , default = defaultLogLevelName , <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" % defaultLogLevelName ) ) <EOL> addOptionFn ( "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> addOptionFn ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> def setLoggingFromOptions ( options ) : <EOL> """<STR_LIT>""" <EOL> logging . basicConfig ( format = "<STR_LIT>" ) <EOL> rootLogger . setLevel ( defaultLogLevel ) <EOL> if options . logLevel is not None : <EOL> setLogLevel ( options . logLevel ) <EOL> logger . info ( "<STR_LIT>" % getLogLevelString ( ) ) <EOL> if options . logFile is not None : <EOL> addLoggingFileHandler ( options . logFile , options . logRotating ) <EOL> logger . info ( "<STR_LIT>" % options . logFile ) <EOL> def system ( command ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( '<STR_LIT>' , command ) <EOL> subprocess . check_call ( command , shell = isinstance ( command , basestring ) , bufsize = - <NUM_LIT:1> ) <EOL> def getTotalCpuTimeAndMemoryUsage ( ) : <EOL> """<STR_LIT>""" <EOL> me = resource . getrusage ( resource . RUSAGE_SELF ) <EOL> childs = resource . getrusage ( resource . RUSAGE_CHILDREN ) <EOL> totalCPUTime = me . ru_utime + me . ru_stime + childs . ru_utime + childs . ru_stime <EOL> totalMemoryUsage = me . ru_maxrss + me . ru_maxrss <EOL> return totalCPUTime , totalMemoryUsage <EOL> def getTotalCpuTime ( ) : <EOL> """<STR_LIT>""" <EOL> return getTotalCpuTimeAndMemoryUsage ( ) [ <NUM_LIT:0> ] <EOL> def getTotalMemoryUsage ( ) : <EOL> """<STR_LIT>""" <EOL> return getTotalCpuTimeAndMemoryUsage ( ) [ <NUM_LIT:1> ] <EOL> def absSymPath ( path ) : <EOL> """<STR_LIT>""" <EOL> curr_path = os . getcwd ( ) <EOL> return os . path . normpath ( os . path . join ( curr_path , path ) ) <EOL> class TestStatus : <EOL> TEST_SHORT = <NUM_LIT:0> <EOL> TEST_MEDIUM = <NUM_LIT:1> <EOL> TEST_LONG = <NUM_LIT:2> <EOL> TEST_VERY_LONG = <NUM_LIT:3> <EOL> TEST_STATUS = TEST_SHORT <EOL> SAVE_ERROR_LOCATION = None <EOL> def getTestStatus ( ) : <EOL> return TestStatus . TEST_STATUS <EOL> getTestStatus = staticmethod ( getTestStatus ) <EOL> def setTestStatus ( status ) : <EOL> assert status in ( TestStatus . TEST_SHORT , TestStatus . TEST_MEDIUM , TestStatus . TEST_LONG , TestStatus . TEST_VERY_LONG ) <EOL> TestStatus . TEST_STATUS = status <EOL> setTestStatus = staticmethod ( setTestStatus ) <EOL> def getSaveErrorLocation ( ) : <EOL> """<STR_LIT>""" <EOL> return TestStatus . SAVE_ERROR_LOCATION <EOL> getSaveErrorLocation = staticmethod ( getSaveErrorLocation ) <EOL> def setSaveErrorLocation ( dir ) : <EOL> """<STR_LIT>""" <EOL> logger . info ( "<STR_LIT>" % dir ) <EOL> assert os . path . isdir ( dir ) <EOL> TestStatus . SAVE_ERROR_LOCATION = dir <EOL> setSaveErrorLocation = staticmethod ( setSaveErrorLocation ) <EOL> def getTestSetup ( shortTestNo = <NUM_LIT:1> , mediumTestNo = <NUM_LIT:5> , longTestNo = <NUM_LIT:100> , veryLongTestNo = <NUM_LIT:0> ) : <EOL> if TestStatus . TEST_STATUS == TestStatus . TEST_SHORT : <EOL> return shortTestNo <EOL> elif TestStatus . TEST_STATUS == TestStatus . TEST_MEDIUM : <EOL> return mediumTestNo <EOL> elif TestStatus . TEST_STATUS == TestStatus . TEST_LONG : <EOL> return longTestNo <EOL> else : <EOL> return veryLongTestNo <EOL> getTestSetup = staticmethod ( getTestSetup ) <EOL> def getPathToDataSets ( ) : <EOL> """<STR_LIT>""" <EOL> assert "<STR_LIT>" in os . environ <EOL> return os . environ [ "<STR_LIT>" ] <EOL> getPathToDataSets = staticmethod ( getPathToDataSets ) <EOL> def getBasicOptionParser ( parser = None ) : <EOL> if parser is None : <EOL> parser = ArgumentParser ( ) <EOL> addLoggingOptions ( parser ) <EOL> parser . add_argument ( "<STR_LIT>" , dest = "<STR_LIT>" , type = str , <EOL> help = "<STR_LIT>" , <EOL> default = tempfile . gettempdir ( ) ) <EOL> return parser <EOL> def parseBasicOptions ( parser ) : <EOL> """<STR_LIT>""" <EOL> options = parser . parse_args ( ) <EOL> setLoggingFromOptions ( options ) <EOL> if options . tempDirRoot == "<STR_LIT:None>" : <EOL> options . tempDirRoot = tempfile . gettempdir ( ) <EOL> return options <EOL> def getRandomAlphaNumericString ( length = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . join ( [ random . choice ( '<STR_LIT>' ) for i in xrange ( <NUM_LIT:0> , length ) ] ) <EOL> def makePublicDir ( dirName ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( dirName ) : <EOL> os . mkdir ( dirName ) <EOL> os . chmod ( dirName , <NUM_LIT:0> <NUM_LIT> ) <EOL> return dirName <EOL> def getTempFile ( suffix = "<STR_LIT>" , rootDir = None ) : <EOL> """<STR_LIT>""" <EOL> if rootDir is None : <EOL> handle , tmpFile = tempfile . mkstemp ( suffix ) <EOL> os . close ( handle ) <EOL> return tmpFile <EOL> else : <EOL> tmpFile = os . path . join ( rootDir , "<STR_LIT>" + getRandomAlphaNumericString ( ) + suffix ) <EOL> open ( tmpFile , '<STR_LIT:w>' ) . close ( ) <EOL> os . chmod ( tmpFile , <NUM_LIT:0> <NUM_LIT> ) <EOL> return tmpFile </s>
