<s> """<STR_LIT>""" <EOL> import optparse <EOL> import os <EOL> import re <EOL> import subprocess <EOL> import sys <EOL> import types <EOL> import unittest <EOL> import plistlib <EOL> def AttachDiskImage ( path ) : <EOL> """<STR_LIT>""" <EOL> command = [ "<STR_LIT>" , "<STR_LIT>" , path , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> task = subprocess . Popen ( command , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> ( stdout , stderr ) = task . communicate ( ) <EOL> if task . returncode : <EOL> print "<STR_LIT>" % path <EOL> print stderr <EOL> return False <EOL> else : <EOL> mountpoint = False <EOL> dmg_plist = plistlib . readPlistFromString ( stdout ) <EOL> entities = dmg_plist [ "<STR_LIT>" ] <EOL> for entity in entities : <EOL> if "<STR_LIT>" in entity : <EOL> mountpoint = entity [ "<STR_LIT>" ] <EOL> return mountpoint <EOL> def DetachDiskImage ( path ) : <EOL> """<STR_LIT>""" <EOL> command = [ "<STR_LIT>" , "<STR_LIT>" , path ] <EOL> returncode = subprocess . call ( command ) <EOL> if returncode : <EOL> command = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , path ] <EOL> returncode = subprocess . call ( command ) <EOL> if returncode : <EOL> raise StandardError ( "<STR_LIT>" % path ) <EOL> return True <EOL> def TestClasses ( module ) : <EOL> """<STR_LIT>""" <EOL> classes = [ ] <EOL> pattern = re . compile ( "<STR_LIT>" ) <EOL> for name in dir ( module ) : <EOL> obj = getattr ( module , name ) <EOL> if type ( obj ) in ( types . ClassType , types . TypeType ) : <EOL> if pattern . match ( name ) : <EOL> classes . append ( name ) <EOL> return classes <EOL> def GetTestSuite ( path , mountpoint , options ) : <EOL> """<STR_LIT>""" <EOL> dirname = os . path . dirname ( path ) <EOL> filename = os . path . basename ( path ) <EOL> if not dirname in sys . path : <EOL> sys . path . append ( dirname ) <EOL> modulename = re . sub ( "<STR_LIT>" , "<STR_LIT>" , filename ) <EOL> module = __import__ ( modulename ) <EOL> for classname in TestClasses ( module ) : <EOL> test_loader = unittest . TestLoader ( ) <EOL> test_suite = test_loader . loadTestsFromName ( classname , module ) <EOL> for test in test_suite . _tests : <EOL> test . SetMountpoint ( mountpoint ) <EOL> test . SetOptions ( options ) <EOL> return test_suite <EOL> def ListTests ( path ) : <EOL> """<STR_LIT>""" <EOL> pattern = re . compile ( "<STR_LIT>" , re . IGNORECASE ) <EOL> tests = [ ] <EOL> for test in os . listdir ( path ) : <EOL> if pattern . match ( test ) : <EOL> tests . append ( test ) <EOL> tests . sort ( ) <EOL> return tests <EOL> def SummarizeResults ( result ) : <EOL> """<STR_LIT>""" <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % result . testsRun <EOL> print "<STR_LIT>" % len ( result . errors ) <EOL> print "<STR_LIT>" % len ( result . failures ) <EOL> print <EOL> def ParseCLIArgs ( ) : <EOL> """<STR_LIT>""" <EOL> cli = optparse . OptionParser ( ) <EOL> cli . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT:.>" , <EOL> type = "<STR_LIT:string>" , help = "<STR_LIT>" ) <EOL> cli . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> cli . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> cli . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:root>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> cli . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT>" , <EOL> type = "<STR_LIT:string>" , help = "<STR_LIT>" ) <EOL> cli . add_option ( "<STR_LIT>" , "<STR_LIT>" , type = "<STR_LIT:int>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , default = <NUM_LIT:0> ) <EOL> ( options , args ) = cli . parse_args ( ) <EOL> return ( options , args ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> ( options , unused_args ) = ParseCLIArgs ( ) <EOL> dmg = options . dmg <EOL> verbosity = options . verbosity <EOL> tests_dir = options . testdir <EOL> config_dir = options . configdir <EOL> root = options . root <EOL> if not dmg and not root : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if dmg : <EOL> print "<STR_LIT>" <EOL> mountpoint = AttachDiskImage ( dmg ) <EOL> if not mountpoint : <EOL> print "<STR_LIT>" % dmg <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> elif root : <EOL> if not os . path . isdir ( root ) : <EOL> print "<STR_LIT>" % root <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> mountpoint = root <EOL> print "<STR_LIT>" % mountpoint <EOL> print <EOL> dirname = os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) <EOL> os . chdir ( dirname ) <EOL> tests = ListTests ( tests_dir ) <EOL> test_results = { } <EOL> combo_suite = unittest . TestSuite ( ) <EOL> for test in tests : <EOL> test_path = os . path . join ( tests_dir , test ) <EOL> combo_suite . addTests ( GetTestSuite ( test_path , mountpoint , options ) ) <EOL> test_results = unittest . TextTestRunner ( verbosity = verbosity ) . run ( combo_suite ) <EOL> if dmg : <EOL> DetachDiskImage ( mountpoint ) <EOL> if test_results . wasSuccessful ( ) : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> else : <EOL> SummarizeResults ( test_results ) <EOL> bad = len ( test_results . errors ) + len ( test_results . failures ) <EOL> sys . exit ( bad ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
