<s> import os <EOL> import re <EOL> import fnmatch <EOL> import testing <EOL> TESTS = None <EOL> TESTS_BY_FILENAME = { } <EOL> def automaticDependencies ( filename ) : <EOL> this_dirname = os . path . dirname ( filename ) <EOL> for test in TESTS : <EOL> other_dirname = os . path . dirname ( test . filename ) <EOL> if this_dirname . startswith ( other_dirname ) : <EOL> if os . path . sep not in other_dirname or len ( other_dirname ) < len ( this_dirname ) : <EOL> yield test <EOL> RE_DEPENDENCY = re . compile ( r"<STR_LIT>" ) <EOL> RE_FLAG = re . compile ( r"<STR_LIT>" ) <EOL> RE_IGNORE = re . compile ( r"<STR_LIT>" ) <EOL> class Test ( object ) : <EOL> def __init__ ( self , filename ) : <EOL> self . filename = filename <EOL> self . groups = [ ] <EOL> dirname = filename <EOL> while True : <EOL> dirname , basename = os . path . split ( dirname ) <EOL> if not dirname : <EOL> break <EOL> self . groups . insert ( <NUM_LIT:0> , dirname ) <EOL> self . dependencies = set ( ) <EOL> self . flags = set ( ) <EOL> has_dependency_declarations = [ ] <EOL> def process_file ( path ) : <EOL> path = os . path . join ( "<STR_LIT>" , "<STR_LIT>" , path ) <EOL> if not os . path . isfile ( path ) : <EOL> return <EOL> with open ( path ) as source_file : <EOL> for index , line in enumerate ( source_file ) : <EOL> match = RE_DEPENDENCY . match ( line ) <EOL> if match : <EOL> has_dependency_declarations . append ( True ) <EOL> dependency = match . group ( <NUM_LIT:1> ) <EOL> if dependency == "<STR_LIT:none>" : <EOL> pass <EOL> elif dependency not in TESTS_BY_FILENAME : <EOL> testing . logger . error ( <EOL> "<STR_LIT>" <EOL> % ( filename , index + <NUM_LIT:1> , dependency ) ) <EOL> else : <EOL> self . dependencies . add ( TESTS_BY_FILENAME [ dependency ] ) <EOL> continue <EOL> match = RE_FLAG . match ( line ) <EOL> if match : <EOL> self . flags . add ( match . group ( <NUM_LIT:1> ) ) <EOL> continue <EOL> match = RE_IGNORE . match ( line ) <EOL> if not match : <EOL> break <EOL> process_file ( filename ) <EOL> dirname = filename <EOL> while True : <EOL> dirname = os . path . dirname ( dirname ) <EOL> if not dirname : <EOL> break <EOL> process_file ( os . path . join ( dirname , "<STR_LIT>" ) ) <EOL> if not has_dependency_declarations : <EOL> self . dependencies . update ( automaticDependencies ( filename ) ) <EOL> TESTS . append ( self ) <EOL> TESTS_BY_FILENAME [ self . filename ] = self <EOL> def __str__ ( self ) : <EOL> return self . filename <EOL> def __hash__ ( self ) : <EOL> return hash ( self . filename ) <EOL> def __eq__ ( self , other ) : <EOL> return self . filename == str ( other ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . filename , sorted ( [ test . filename for test in self . dependencies ] ) ) <EOL> def findTests ( ) : <EOL> global TESTS <EOL> RE_TEST_FILENAME = re . compile ( r"<STR_LIT>" ) <EOL> RE_IGNORE_FILENAME = re . compile ( r"<STR_LIT>" ) <EOL> TESTS = [ ] <EOL> def traverse ( dirname ) : <EOL> for filename in sorted ( os . listdir ( dirname ) ) : <EOL> filename = os . path . join ( dirname , filename ) <EOL> if os . path . isdir ( filename ) : <EOL> traverse ( filename ) <EOL> elif RE_TEST_FILENAME . search ( filename ) : <EOL> Test ( os . path . relpath ( filename , "<STR_LIT>" ) ) <EOL> elif not RE_IGNORE_FILENAME . search ( filename ) : <EOL> testing . logger . warning ( <EOL> "<STR_LIT>" <EOL> % filename ) <EOL> traverse ( "<STR_LIT>" ) <EOL> def filterPatterns ( patterns ) : <EOL> RE_LEADING_TESTS = re . compile ( "<STR_LIT>" ) <EOL> patterns = [ RE_LEADING_TESTS . sub ( "<STR_LIT>" , pattern ) for pattern in patterns ] <EOL> patterns = [ pattern . rstrip ( "<STR_LIT:/>" ) for pattern in patterns ] <EOL> patterns = filter ( None , patterns ) <EOL> return patterns <EOL> def selectTests ( patterns , strict , flags_on = set ( ) , flags_off = set ( ) ) : <EOL> if TESTS is None : <EOL> findTests ( ) <EOL> patterns = filterPatterns ( patterns ) <EOL> if not patterns and not flags_on and not flags_off : <EOL> return TESTS , set ( ) <EOL> selected = set ( ) <EOL> dependencies = set ( ) <EOL> def select ( test , is_dependency = False ) : <EOL> if test in selected : <EOL> return <EOL> selected . add ( test . filename ) <EOL> if strict : <EOL> return <EOL> if is_dependency : <EOL> dependencies . add ( test . filename ) <EOL> for dependency in test . dependencies : <EOL> select ( dependency , True ) <EOL> for test in TESTS : <EOL> if flags_on - test . flags : <EOL> continue <EOL> if flags_off & test . flags : <EOL> continue <EOL> if patterns : <EOL> for pattern in patterns : <EOL> filename = test . filename <EOL> while filename : <EOL> if fnmatch . fnmatch ( filename , pattern ) : <EOL> select ( test ) <EOL> break <EOL> if strict : <EOL> break <EOL> filename = os . path . dirname ( filename ) <EOL> if test in selected : <EOL> break <EOL> else : <EOL> select ( test ) <EOL> return [ test for test in TESTS if test in selected ] , dependencies </s>
