<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from __future__ import division <EOL> import sys <EOL> import os <EOL> import shutil <EOL> from errno import EEXIST <EOL> from pycopia import logging <EOL> from pycopia import timelib <EOL> from pycopia . QA import core <EOL> from pycopia . QA import constants <EOL> from pycopia import reports <EOL> ModuleType = type ( sys ) <EOL> TypeType = type <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class TestRunnerError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class TestRunner ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> self . config . options_override = { } <EOL> self . config . arguments = [ ] <EOL> if config . flags . DEBUG : <EOL> logging . loglevel_debug ( ) <EOL> else : <EOL> logging . loglevel_warning ( ) <EOL> def set_options ( self , opts ) : <EOL> if isinstance ( opts , dict ) : <EOL> self . config . options_override = opts <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def run_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> cf = self . config <EOL> basename = "<STR_LIT:_>" . join ( obj . test_name . split ( "<STR_LIT:.>" ) ) <EOL> cf . reportfilename = basename <EOL> cf . logbasename = basename + "<STR_LIT:->" + cf . runnertimestamp + "<STR_LIT>" <EOL> cf . resultsdir = os . path . join ( <EOL> os . path . expandvars ( cf . get ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" % ( cf . reportfilename , cf . username , cf . runnertimestamp ) ) <EOL> cf . evalupdate ( cf . options_override ) <EOL> self . _create_results_dir ( ) <EOL> self . _set_report_url ( ) <EOL> cf . report . logfile ( cf . logfilename ) <EOL> return obj . run ( cf ) <EOL> def run_objects ( self , objects ) : <EOL> """<STR_LIT>""" <EOL> rv = <NUM_LIT:0> <EOL> testcases = [ ] <EOL> for obj in objects : <EOL> objecttype = type ( obj ) <EOL> if objecttype is ModuleType and hasattr ( obj , "<STR_LIT>" ) : <EOL> rv = self . run_module ( obj ) <EOL> elif objecttype is TypeType and issubclass ( obj , core . Test ) : <EOL> testcases . append ( obj ) <EOL> elif isinstance ( obj , core . TestSuite ) : <EOL> rv = self . run_object ( obj ) <EOL> elif objecttype is type and hasattr ( obj , "<STR_LIT>" ) : <EOL> rv = self . run_class ( obj ) <EOL> else : <EOL> logging . warn ( "<STR_LIT>" % ( obj , ) ) <EOL> if testcases : <EOL> if len ( testcases ) > <NUM_LIT:1> : <EOL> rv = self . run_tests ( testcases ) <EOL> else : <EOL> args = [ ] <EOL> kwargs = { } <EOL> opts = self . config . options_override . copy ( ) <EOL> for name , value in opts . items ( ) : <EOL> if name . startswith ( "<STR_LIT>" ) : <EOL> try : <EOL> index = int ( name [ <NUM_LIT:3> ] ) <EOL> except ( ValueError , IndexError ) : <EOL> logging . warn ( "<STR_LIT>" . format ( name ) ) <EOL> else : <EOL> try : <EOL> args [ index ] = value <EOL> except IndexError : <EOL> need = index - len ( args ) <EOL> while need : <EOL> args . append ( None ) <EOL> need -= <NUM_LIT:1> <EOL> args . append ( value ) <EOL> del self . config . options_override [ name ] <EOL> del self . config [ name ] <EOL> elif name . startswith ( "<STR_LIT>" ) : <EOL> kwargs [ name [ <NUM_LIT:6> : ] ] = value <EOL> del self . config . options_override [ name ] <EOL> del self . config [ name ] <EOL> rv = self . run_test ( testcases [ <NUM_LIT:0> ] , * args , ** kwargs ) <EOL> return rv <EOL> def run_class ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> rpt = self . config . report <EOL> cls . test_name = "<STR_LIT:.>" . join ( [ cls . __module__ , cls . __name__ ] ) <EOL> ID = get_module_version ( sys . modules [ cls . __module__ ] ) <EOL> rpt . add_message ( "<STR_LIT>" , ID ) <EOL> rpt . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> try : <EOL> rv = self . run_object ( cls ) <EOL> except KeyboardInterrupt : <EOL> rpt . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> rpt . incomplete ( "<STR_LIT>" ) <EOL> raise <EOL> except : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> if self . config . flags . DEBUG : <EOL> from pycopia import debugger <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> rpt . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> rpt . incomplete ( "<STR_LIT>" % ( ex , val ) ) <EOL> return constants . INCOMPLETE <EOL> else : <EOL> rpt . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> return rv <EOL> def run_module ( self , mod ) : <EOL> """<STR_LIT>""" <EOL> cf = self . config <EOL> mod . test_name = mod . __name__ <EOL> ID = get_module_version ( mod ) <EOL> cf . report . add_message ( "<STR_LIT>" , ID ) <EOL> cf . report . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> try : <EOL> rv = self . run_object ( mod ) <EOL> except KeyboardInterrupt : <EOL> cf . report . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> cf . report . incomplete ( "<STR_LIT>" ) <EOL> raise <EOL> except : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> if cf . flags . DEBUG : <EOL> from pycopia import debugger <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> rv = constants . FAILED <EOL> cf . report . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> cf . report . failed ( "<STR_LIT>" % ( ex , val ) ) <EOL> else : <EOL> cf . report . add_message ( "<STR_LIT>" , timelib . now ( ) ) <EOL> if rv is None : <EOL> return <NUM_LIT:1> <EOL> elif type ( rv ) is core . TestResult : <EOL> return bool ( rv ) <EOL> elif rv : <EOL> return cf . report . passed ( "<STR_LIT>" ) <EOL> else : <EOL> return cf . report . failed ( "<STR_LIT>" ) <EOL> def run_suite ( self , suite ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( suite , core . TestSuite ) : <EOL> raise TestRunnerError ( "<STR_LIT>" ) <EOL> return self . run_object ( suite ) <EOL> def run_test ( self , testclass , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> suite = core . TestSuite ( self . config , name = "<STR_LIT>" % testclass . __name__ ) <EOL> suite . add_test ( testclass , * args , ** kwargs ) <EOL> return self . run_object ( suite ) <EOL> def run_tests ( self , testclasses ) : <EOL> """<STR_LIT>""" <EOL> suite = core . TestSuite ( self . config , name = "<STR_LIT>" ) <EOL> suite . add_tests ( testclasses ) <EOL> return self . run_object ( suite ) <EOL> def _create_results_dir ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . mkdir ( self . config . resultsdir ) <EOL> except OSError as error : <EOL> if error [ <NUM_LIT:0> ] == EEXIST : <EOL> pass <EOL> else : <EOL> raise <EOL> def _set_report_url ( self ) : <EOL> """<STR_LIT>""" <EOL> cf = self . config <EOL> baseurl = cf . get ( "<STR_LIT>" ) <EOL> documentroot = cf . get ( "<STR_LIT>" ) <EOL> resultsdir = cf . resultsdir <EOL> if baseurl and documentroot : <EOL> cf . report . add_url ( "<STR_LIT>" , baseurl + resultsdir [ len ( documentroot ) : ] ) <EOL> else : <EOL> cf . report . add_url ( "<STR_LIT>" , "<STR_LIT>" + resultsdir ) <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> cf = self . config <EOL> cf . username = os . environ [ "<STR_LIT>" ] <EOL> os . chdir ( cf . logfiledir ) <EOL> cf . runnerstarttime = starttime = timelib . now ( ) <EOL> cf . runnertimestamp = timelib . strftime ( "<STR_LIT>" , <EOL> timelib . localtime ( cf . runnerstarttime ) ) <EOL> try : <EOL> rpt = cf . get_report ( ) <EOL> except reports . ReportFindError as err : <EOL> cf . UI . error ( str ( err ) ) <EOL> cf . UI . printf ( "<STR_LIT>" ) <EOL> cf . UI . print_list ( cf . reports . keys ( ) ) <EOL> cf . UI . Print ( "<STR_LIT:\n>" ) <EOL> raise TestRunnerError ( "<STR_LIT>" ) <EOL> rpt . initialize ( cf ) <EOL> cf . reportfilenames = rpt . filenames <EOL> rpt . add_title ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( cf . get ( "<STR_LIT>" , [ "<STR_LIT>" ] ) ) ) <EOL> arguments = cf . get ( "<STR_LIT>" ) <EOL> if arguments : <EOL> rpt . add_message ( "<STR_LIT>" , "<STR_LIT:U+0020>" . join ( arguments ) ) <EOL> comment = cf . get ( "<STR_LIT>" ) <EOL> if comment : <EOL> rpt . add_message ( "<STR_LIT>" , comment ) <EOL> build = cf . get ( "<STR_LIT>" ) <EOL> if build : <EOL> rpt . add_message ( "<STR_LIT>" , build ) <EOL> rpt . add_message ( "<STR_LIT>" , starttime , <NUM_LIT:0> ) <EOL> def finalize ( self ) : <EOL> """<STR_LIT>""" <EOL> cf = self . config <EOL> rpt = cf . report <EOL> rpt . add_message ( "<STR_LIT>" , timelib . now ( ) , <NUM_LIT:0> ) <EOL> rpt . finalize ( ) <EOL> del rpt <EOL> del cf . report <EOL> del cf . logfile <EOL> del cf . UI <EOL> del cf . environment <EOL> if cf . has_key ( "<STR_LIT>" ) : <EOL> for fname in cf . reportfilenames : <EOL> if not fname . startswith ( "<STR_LIT:<>" ) : <EOL> if os . path . isfile ( fname ) : <EOL> shutil . move ( fname , cf . resultsdir ) <EOL> for suffix in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> fname = cf . logfilename + suffix <EOL> if os . path . isfile ( fname ) : <EOL> if os . path . getsize ( fname ) > <NUM_LIT:0> : <EOL> shutil . move ( fname , cf . resultsdir ) <EOL> else : <EOL> os . unlink ( fname ) <EOL> if not os . listdir ( cf . resultsdir ) : <EOL> os . rmdir ( cf . resultsdir ) <EOL> def report_global ( self ) : <EOL> """<STR_LIT>""" <EOL> from pycopia . db import models <EOL> cf = self . config <EOL> ui = cf . UI <EOL> ui . printf ( "<STR_LIT>" ) <EOL> ui . print_list ( sorted ( cf . reports . keys ( ) ) ) <EOL> ui . Print ( "<STR_LIT:\n>" ) <EOL> ui . printf ( "<STR_LIT>" ) <EOL> db = cf . session <EOL> ui . print_list ( sorted ( [ env . name for env in db . query ( models . Environment ) . all ( ) ] ) ) <EOL> def get_module_version ( mod ) : <EOL> try : <EOL> return mod . __version__ [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( "<STR_LIT::>" ) [ - <NUM_LIT:1> ] . strip ( ) <EOL> except ( AttributeError , IndexError ) : <EOL> return "<STR_LIT>" </s>
