<s> """<STR_LIT>""" <EOL> import sys <EOL> import traceback <EOL> from os import walk , sep <EOL> from os . path import join as join_path <EOL> from . . util . doctest import testfile , testmod , ELLIPSIS <EOL> from . . util . io import DEVNULL , print_text as text <EOL> from . . util . io import print_message as heading , print_note as note <EOL> PASSED = '<STR_LIT>' <EOL> FAILED = "<STR_LIT>" <EOL> IMPORT_ERROR = "<STR_LIT>" <EOL> PACKAGE = '<STR_LIT>' <EOL> handlers = { } <EOL> def secure_python ( ) : <EOL> from plexnet . util . secure import secure_python <EOL> secure_python ( ) <EOL> handlers [ secure_python ] = ( '<STR_LIT>' , ) <EOL> def run_tests ( argv = None , verbose = False , package = PACKAGE , handlers = handlers ) : <EOL> """<STR_LIT>""" <EOL> argv = argv or sys . argv [ <NUM_LIT:1> : ] <EOL> if ( '<STR_LIT>' in argv ) or ( '<STR_LIT>' in argv ) : <EOL> print run_tests . __doc__ % sys . argv [ <NUM_LIT:0> ] <EOL> sys . exit ( ) <EOL> while '<STR_LIT>' in argv : <EOL> verbose = True <EOL> argv . remove ( '<STR_LIT>' ) <EOL> ori_stdout = sys . stdout <EOL> while '<STR_LIT>' in argv : <EOL> sys . stdout = DEVNULL <EOL> argv . remove ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in argv : <EOL> pos = argv . index ( '<STR_LIT>' ) <EOL> del argv [ pos ] <EOL> package = argv [ pos ] <EOL> del argv [ pos ] <EOL> while '<STR_LIT>' in argv : <EOL> package = '<STR_LIT>' <EOL> argv . remove ( '<STR_LIT>' ) <EOL> suppress = True <EOL> while '<STR_LIT>' in argv : <EOL> suppress = False <EOL> argv . remove ( '<STR_LIT>' ) <EOL> global heading , note , text <EOL> _heading , _note , _text = heading , note , text <EOL> while '<STR_LIT>' in argv : <EOL> heading = lambda text , * arg , ** kw : _heading ( text , <NUM_LIT> , <NUM_LIT:0> , '<STR_LIT:null>' , '<STR_LIT:null>' ) <EOL> note = lambda text , * arg , ** kw : _note ( text , '<STR_LIT:null>' , '<STR_LIT:null>' ) <EOL> text = lambda colour , text : text <EOL> argv . remove ( '<STR_LIT>' ) <EOL> print_empty_tests = True <EOL> if package and not argv : <EOL> print_empty_tests = False <EOL> package_path = __import__ ( package ) . __path__ [ <NUM_LIT:0> ] <EOL> pp_len = len ( package_path ) + <NUM_LIT:1> <EOL> add_mod = argv . append <EOL> for root , dirs , files in walk ( package_path ) : <EOL> for file in files : <EOL> if file . endswith ( '<STR_LIT>' ) and file != '<STR_LIT>' : <EOL> add_mod ( join_path ( root , file ) [ pp_len : - <NUM_LIT:3> ] . replace ( sep , '<STR_LIT:.>' ) ) <EOL> argv . sort ( ) <EOL> if package == PACKAGE : <EOL> for handler in handlers . keys ( ) : <EOL> idx = <NUM_LIT:0> <EOL> for mod in handlers [ handler ] : <EOL> if mod in argv : <EOL> argv . remove ( mod ) <EOL> argv . insert ( <NUM_LIT:0> , mod ) <EOL> idx += <NUM_LIT:1> <EOL> else : <EOL> argv . insert ( idx , handler ) <EOL> test_data = [ ] ; out = test_data . append <EOL> for item in argv : <EOL> if item in handlers : <EOL> item ( ) <EOL> else : <EOL> out ( test_module ( item , verbose , package , suppress ) ) <EOL> print_test_summary ( test_data , ori_stdout , print_empty_tests ) <EOL> def test_module ( module_name , verbosity = None , package = '<STR_LIT>' , suppress = False ) : <EOL> """<STR_LIT>""" <EOL> if package : <EOL> module_name = '<STR_LIT>' % ( package , module_name ) <EOL> if module_name . endswith ( '<STR_LIT:.>' ) : <EOL> module_name = module_name [ : - <NUM_LIT:1> ] <EOL> print heading ( '<STR_LIT>' + module_name ) <EOL> stdout = sys . stdout <EOL> try : <EOL> if suppress : <EOL> sys . stdout = sys . stderr = DEVNULL <EOL> module = __import__ ( module_name , globals ( ) , locals ( ) , [ module_name ] ) <EOL> sys . stdout = stdout <EOL> failures , tries = testmod ( <EOL> module , verbose = verbosity , optionflags = ELLIPSIS <EOL> ) <EOL> except Exception as reason : <EOL> sys . stdout = stdout <EOL> sys . stderr = sys . __stderr__ <EOL> print <EOL> print text ( '<STR_LIT>' , "<STR_LIT>" % reason . __class__ . __name__ ) <EOL> print text ( '<STR_LIT>' , "<STR_LIT>" % str ( reason ) ) <EOL> print <EOL> traceback . print_exception ( * sys . exc_info ( ) ) <EOL> return module_name , <NUM_LIT:0> , <NUM_LIT:0> , IMPORT_ERROR <EOL> if failures : <EOL> print <EOL> print text ( <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" % ( failures , tries , module_name ) <EOL> ) <EOL> print <EOL> return module_name , failures , tries , FAILED <EOL> print note ( "<STR_LIT>" % tries ) <EOL> return module_name , failures , tries , PASSED <EOL> _marker = object ( ) <EOL> def print_test_summary ( results , stream , print_empty_tests ) : <EOL> sys . stdout = stream <EOL> print heading ( '<STR_LIT>' ) <EOL> print <EOL> _last = _marker <EOL> _total = _count = <NUM_LIT:0> <EOL> for name , failures , tries , status in results : <EOL> if not print_empty_tests and tries == <NUM_LIT:0> : <EOL> continue <EOL> _total += tries <EOL> _split = name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( _split ) == <NUM_LIT:2> : <EOL> package , module = _split <EOL> else : <EOL> package = '<STR_LIT>' <EOL> module = _split [ <NUM_LIT:0> ] <EOL> if _last is _marker or package != _last : <EOL> print <EOL> _count += <NUM_LIT:1> <EOL> _count_str = _count <EOL> _package_str = package <EOL> elif package == _last : <EOL> _count_str = '<STR_LIT>' <EOL> _package_str = '<STR_LIT>' <EOL> _last = package <EOL> if status == PASSED : <EOL> failures = of = '<STR_LIT:U+0020>' <EOL> elif status == IMPORT_ERROR : <EOL> failures = of = tries = '<STR_LIT:U+0020>' <EOL> else : <EOL> of = '<STR_LIT>' <EOL> print "<STR_LIT>" % ( <EOL> _count_str , _package_str . ljust ( <NUM_LIT:20> ) , module , failures , of , tries , status <EOL> ) <EOL> print <EOL> print "<STR_LIT>" % _total <EOL> print </s>
