<s> from __future__ import print_function <EOL> from inspect import isclass , ismethod <EOL> import importlib <EOL> import pkgutil <EOL> import unittest <EOL> import uuid <EOL> from cafe . drivers . base import print_exception , get_error <EOL> from cafe . drivers . unittest . suite import OpenCafeUnittestTestSuite <EOL> from cafe . drivers . unittest . decorators import PARALLEL_TAGS_LIST_ATTR <EOL> class SuiteBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , testrepos , tags = None , all_tags = False , regex_list = None , <EOL> file_ = None , dry_run = False , exit_on_error = False ) : <EOL> self . testrepos = testrepos <EOL> self . tags = tags or [ ] <EOL> self . all_tags = all_tags <EOL> self . regex_list = regex_list or [ ] <EOL> self . exit_on_error = exit_on_error <EOL> self . dry_run = dry_run <EOL> self . file_ = file_ or { } <EOL> def get_suites ( self ) : <EOL> """<STR_LIT>""" <EOL> test_suites = self . load_file ( ) <EOL> for class_ in self . _get_classes ( self . _get_modules ( ) ) : <EOL> suite = OpenCafeUnittestTestSuite ( ) <EOL> for test in self . _get_tests ( class_ ) : <EOL> suite . addTest ( class_ ( test ) ) <EOL> if suite . _tests : <EOL> test_suites . append ( suite ) <EOL> if self . dry_run : <EOL> for suite in test_suites : <EOL> for test in suite : <EOL> print ( test ) <EOL> exit ( <NUM_LIT:0> ) <EOL> for suite in test_suites : <EOL> suite . cafe_uuid = uuid . uuid4 ( ) <EOL> return test_suites <EOL> def load_file ( self ) : <EOL> """<STR_LIT>""" <EOL> suites = [ ] <EOL> for key , value in self . file_ . items ( ) : <EOL> suite = OpenCafeUnittestTestSuite ( ) <EOL> module , class_ = key . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> module = importlib . import_module ( module ) <EOL> class_ = getattr ( module , class_ ) <EOL> for test in value : <EOL> suite . addTest ( class_ ( test ) ) <EOL> suites . append ( suite ) <EOL> return suites <EOL> def _get_modules ( self ) : <EOL> """<STR_LIT>""" <EOL> modules = [ ] <EOL> error = False <EOL> for repo in self . testrepos : <EOL> if "<STR_LIT>" not in getattr ( repo , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> modules . append ( repo ) <EOL> continue <EOL> prefix = "<STR_LIT>" . format ( repo . __name__ ) <EOL> for _ , modname , is_pkg in pkgutil . walk_packages ( <EOL> path = repo . __path__ , prefix = prefix , onerror = lambda x : None ) : <EOL> if not is_pkg : <EOL> try : <EOL> modules . append ( importlib . import_module ( modname ) ) <EOL> except Exception as exception : <EOL> print_exception ( <EOL> "<STR_LIT>" , "<STR_LIT>" , modname , <EOL> exception ) <EOL> error = True <EOL> if self . exit_on_error and error : <EOL> exit ( get_error ( exception ) ) <EOL> return modules <EOL> @ staticmethod <EOL> def _get_classes ( modules ) : <EOL> """<STR_LIT>""" <EOL> classes = [ ] <EOL> for loaded_module in modules : <EOL> for objname in dir ( loaded_module ) : <EOL> obj = getattr ( loaded_module , objname , None ) <EOL> if ( isclass ( obj ) and issubclass ( obj , unittest . TestCase ) and <EOL> "<STR_LIT>" not in obj . __name__ . lower ( ) ) : <EOL> classes . append ( obj ) <EOL> return classes <EOL> def _get_tests ( self , class_ ) : <EOL> """<STR_LIT>""" <EOL> tests = [ ] <EOL> for name in dir ( class_ ) : <EOL> if name . startswith ( "<STR_LIT>" ) and self . _check_test ( class_ , name ) : <EOL> tests . append ( name ) <EOL> return tests <EOL> def _check_test ( self , class_ , test_name ) : <EOL> """<STR_LIT>""" <EOL> test = getattr ( class_ , test_name ) <EOL> full_path = "<STR_LIT>" . format ( <EOL> class_ . __module__ , class_ . __name__ , test_name ) <EOL> ret_val = ismethod ( test ) and self . _check_tags ( test ) <EOL> regex_val = not self . regex_list <EOL> for regex in self . regex_list : <EOL> regex_val |= bool ( regex . search ( full_path ) ) <EOL> return ret_val & regex_val <EOL> def _check_tags ( self , test ) : <EOL> """<STR_LIT>""" <EOL> test_tags = getattr ( test , PARALLEL_TAGS_LIST_ATTR , [ ] ) <EOL> if self . all_tags : <EOL> return all ( [ tag in test_tags for tag in self . tags ] ) <EOL> else : <EOL> return any ( [ tag in test_tags for tag in self . tags ] or [ True ] ) </s>
