<s> from datetime import datetime <EOL> import os <EOL> import errno <EOL> import csv <EOL> import six <EOL> import sys <EOL> class TestRunMetrics ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . total_tests = <NUM_LIT:0> <EOL> self . total_passed = <NUM_LIT:0> <EOL> self . total_errored = <NUM_LIT:0> <EOL> self . total_failed = <NUM_LIT:0> <EOL> self . timer = TestTimer ( ) <EOL> self . result = TestResultTypes . UNKNOWN <EOL> class TestResultTypes ( object ) : <EOL> """<STR_LIT>""" <EOL> PASSED = "<STR_LIT>" <EOL> FAILED = "<STR_LIT>" <EOL> SKIPPED = "<STR_LIT>" <EOL> TIMEDOUT = "<STR_LIT>" <EOL> UNKNOWN = "<STR_LIT>" <EOL> ERRORED = "<STR_LIT>" <EOL> class TestTimer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . start_time = None <EOL> self . stop_time = None <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . start_time = datetime . now ( ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stop_time = datetime . now ( ) <EOL> def get_elapsed_time ( self ) : <EOL> """<STR_LIT>""" <EOL> elapsedTime = None <EOL> if self . start_time is not None : <EOL> if self . stop_time is not None : <EOL> elapsedTime = ( self . stop_time - self . start_time ) <EOL> else : <EOL> elapsedTime = ( datetime . now ( ) - self . start_time ) <EOL> else : <EOL> rightNow = datetime . now ( ) <EOL> elapsedTime = ( rightNow - rightNow ) <EOL> return ( elapsedTime ) <EOL> class CSVWriter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , headers , file_name , log_dir = '<STR_LIT:.>' , start_clean = False ) : <EOL> self . file_mode = '<STR_LIT:a>' <EOL> self . headers = headers <EOL> try : <EOL> os . makedirs ( log_dir ) <EOL> except OSError as exception : <EOL> if exception . errno != errno . EEXIST : <EOL> raise <EOL> self . full_path = os . path . normpath ( os . path . join ( log_dir , file_name ) ) <EOL> if start_clean : <EOL> try : <EOL> os . remove ( self . full_path ) <EOL> except OSError : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> if os . path . exists ( self . full_path ) is False : <EOL> self . writerow ( self . headers ) <EOL> def writerow ( self , row_list ) : <EOL> if self . full_path : <EOL> if six . PY2 : <EOL> fp = open ( self . full_path , "<STR_LIT>" ) <EOL> else : <EOL> fp = open ( self . full_path , "<STR_LIT:a>" , newline = "<STR_LIT>" ) <EOL> csv_writer = csv . writer ( <EOL> fp , delimiter = '<STR_LIT:U+002C>' , quotechar = '<STR_LIT:">' , quoting = csv . QUOTE_MINIMAL ) <EOL> try : <EOL> csv_writer . writerow ( row_list ) <EOL> except OSError : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> fp . close ( ) <EOL> class PBStatisticsLog ( CSVWriter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file_name = None , log_dir = '<STR_LIT:.>' , start_clean = False ) : <EOL> headers = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> super ( PBStatisticsLog , self ) . __init__ ( <EOL> headers , file_name , log_dir , start_clean ) <EOL> def report ( self , test_result = TestRunMetrics ( ) ) : <EOL> self . writerow ( [ <EOL> test_result . timer . get_elapsed_time ( ) , test_result . timer . start_time , <EOL> test_result . timer . stop_time , test_result . result ] ) </s>
