<s> """<STR_LIT>""" <EOL> from fnmatch import fnmatch <EOL> import logging <EOL> import os <EOL> import sys <EOL> from nose2 import events , util <EOL> __unittest = True <EOL> log = logging . getLogger ( __name__ ) <EOL> class DirectoryHandler ( object ) : <EOL> def __init__ ( self , session ) : <EOL> self . session = session <EOL> self . event_handled = False <EOL> def handle_dir ( self , event , full_path , top_level ) : <EOL> dirname = os . path . basename ( full_path ) <EOL> pattern = self . session . testFilePattern <EOL> evt = events . HandleFileEvent ( <EOL> event . loader , dirname , full_path , pattern , top_level ) <EOL> result = self . session . hooks . handleDir ( evt ) <EOL> if evt . extraTests : <EOL> for test in evt . extraTests : <EOL> yield test <EOL> if evt . handled : <EOL> if result : <EOL> yield result <EOL> self . event_handled = True <EOL> return <EOL> evt = events . MatchPathEvent ( dirname , full_path , pattern ) <EOL> result = self . session . hooks . matchDirPath ( evt ) <EOL> if evt . handled and not result : <EOL> self . event_handled = True <EOL> class Discoverer ( object ) : <EOL> def loadTestsFromName ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . module : <EOL> return <EOL> name = event . name <EOL> module = None <EOL> _ , top_level_dir = self . _getStartDirs ( ) <EOL> try : <EOL> __import__ ( name ) <EOL> module = sys . modules [ name ] <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> event . extraTests . extend ( <EOL> self . _find_tests ( event , name , top_level_dir ) ) <EOL> else : <EOL> event . extraTests . extend ( <EOL> self . _find_tests_in_module ( event , module , top_level_dir ) ) <EOL> def loadTestsFromNames ( self , event ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( "<STR_LIT>" , event ) <EOL> if event . names or event . module : <EOL> return <EOL> event . handled = True <EOL> return self . _discover ( event ) <EOL> def _checkIfPathIsOK ( self , start_dir ) : <EOL> if not os . path . isdir ( os . path . abspath ( start_dir ) ) : <EOL> raise OSError ( "<STR_LIT>" % os . path . abspath ( start_dir ) ) <EOL> def _getStartDirs ( self ) : <EOL> start_dir = self . session . startDir <EOL> top_level_dir = self . session . topLevelDir <EOL> if start_dir is None : <EOL> start_dir = '<STR_LIT:.>' <EOL> if top_level_dir is None : <EOL> top_level_dir = start_dir <EOL> self . _checkIfPathIsOK ( start_dir ) <EOL> is_not_importable = False <EOL> start_dir = os . path . abspath ( start_dir ) <EOL> top_level_dir = os . path . abspath ( top_level_dir ) <EOL> if start_dir != top_level_dir : <EOL> is_not_importable = not os . path . isfile ( <EOL> os . path . join ( start_dir , '<STR_LIT>' ) ) <EOL> if is_not_importable : <EOL> raise ImportError ( <EOL> '<STR_LIT>' % start_dir ) <EOL> self . session . prepareSysPath ( ) <EOL> return start_dir , top_level_dir <EOL> def _discover ( self , event ) : <EOL> loader = event . loader <EOL> try : <EOL> start_dir , top_level_dir = self . _getStartDirs ( ) <EOL> except ( OSError , ImportError ) : <EOL> return loader . suiteClass ( <EOL> loader . failedLoadTests ( self . session . startDir , sys . exc_info ( ) ) ) <EOL> log . debug ( "<STR_LIT>" , start_dir , top_level_dir ) <EOL> tests = list ( self . _find_tests ( event , start_dir , top_level_dir ) ) <EOL> return loader . suiteClass ( tests ) <EOL> def _find_tests ( self , event , start , top_level ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' , start , top_level ) <EOL> if start == top_level : <EOL> full_path = start <EOL> else : <EOL> full_path = os . path . join ( top_level , start ) <EOL> if os . path . isdir ( start ) : <EOL> for test in self . _find_tests_in_dir ( <EOL> event , full_path , top_level ) : <EOL> yield test <EOL> elif os . path . isfile ( start ) : <EOL> for test in self . _find_tests_in_file ( <EOL> event , start , full_path , top_level ) : <EOL> yield test <EOL> def _find_tests_in_dir ( self , event , full_path , top_level ) : <EOL> if not os . path . isdir ( full_path ) : <EOL> return <EOL> log . debug ( "<STR_LIT>" , full_path , top_level ) <EOL> dir_handler = DirectoryHandler ( self . session ) <EOL> for test in dir_handler . handle_dir ( event , full_path , top_level ) : <EOL> yield test <EOL> if dir_handler . event_handled : <EOL> return <EOL> for path in os . listdir ( full_path ) : <EOL> entry_path = os . path . join ( full_path , path ) <EOL> if os . path . isfile ( entry_path ) : <EOL> for test in self . _find_tests_in_file ( <EOL> event , path , entry_path , top_level ) : <EOL> yield test <EOL> elif os . path . isdir ( entry_path ) : <EOL> if ( '<STR_LIT:test>' in path . lower ( ) <EOL> or util . ispackage ( entry_path ) <EOL> or path in self . session . libDirs ) : <EOL> for test in self . _find_tests ( event , entry_path , top_level ) : <EOL> yield test <EOL> def _find_tests_in_file ( self , event , filename , full_path , top_level , module_name = None ) : <EOL> log . debug ( "<STR_LIT>" , full_path , top_level ) <EOL> pattern = self . session . testFilePattern <EOL> loader = event . loader <EOL> evt = events . HandleFileEvent ( <EOL> loader , filename , full_path , pattern , top_level ) <EOL> result = self . session . hooks . handleFile ( evt ) <EOL> if evt . extraTests : <EOL> yield loader . suiteClass ( evt . extraTests ) <EOL> if evt . handled : <EOL> if result : <EOL> yield result <EOL> return <EOL> if not util . valid_module_name ( filename ) : <EOL> return <EOL> evt = events . MatchPathEvent ( filename , full_path , pattern ) <EOL> result = self . session . hooks . matchPath ( evt ) <EOL> if evt . handled : <EOL> if not result : <EOL> return <EOL> elif not self . _match_path ( filename , full_path , pattern ) : <EOL> return <EOL> if module_name is None : <EOL> module_name , package_path = util . name_from_path ( full_path ) <EOL> util . ensure_importable ( package_path ) <EOL> try : <EOL> module = util . module_from_name ( module_name ) <EOL> except : <EOL> yield loader . failedImport ( module_name ) <EOL> else : <EOL> mod_file = os . path . abspath ( <EOL> getattr ( module , '<STR_LIT>' , full_path ) ) <EOL> realpath = os . path . splitext ( mod_file ) [ <NUM_LIT:0> ] <EOL> fullpath_noext = os . path . splitext ( full_path ) [ <NUM_LIT:0> ] <EOL> if realpath . lower ( ) != fullpath_noext . lower ( ) : <EOL> module_dir = os . path . dirname ( realpath ) <EOL> mod_name = os . path . splitext ( os . path . basename ( full_path ) ) [ <NUM_LIT:0> ] <EOL> expected_dir = os . path . dirname ( full_path ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> raise ImportError ( <EOL> msg % ( mod_name , module_dir , expected_dir ) ) <EOL> yield loader . loadTestsFromModule ( module ) <EOL> def _find_tests_in_module ( self , event , module , top_level_dir ) : <EOL> yield event . loader . loadTestsFromModule ( module ) <EOL> pkgpath = getattr ( module , '<STR_LIT>' , None ) <EOL> if pkgpath : <EOL> for entry in pkgpath : <EOL> full_path = os . path . abspath ( os . path . join ( top_level_dir , entry ) ) <EOL> for test in self . _find_tests_in_dir ( <EOL> event , full_path , top_level_dir ) : <EOL> yield test <EOL> def _match_path ( self , path , full_path , pattern ) : <EOL> return fnmatch ( path , pattern ) <EOL> class DiscoveryLoader ( events . Plugin , Discoverer ) : <EOL> """<STR_LIT>""" <EOL> alwaysOn = True <EOL> configSection = '<STR_LIT>' <EOL> def registerInSubprocess ( self , event ) : <EOL> event . pluginClasses . append ( self . __class__ ) <EOL> def loadTestsFromName ( self , event ) : <EOL> """<STR_LIT>""" <EOL> return Discoverer . loadTestsFromName ( self , event ) <EOL> def loadTestsFromNames ( self , event ) : <EOL> """<STR_LIT>""" <EOL> return Discoverer . loadTestsFromNames ( self , event ) </s>
