<s> from datetime import datetime <EOL> from datetime import time as dtime <EOL> from dateutil import parser <EOL> import os <EOL> import threading <EOL> import zopkio . runtime as runtime <EOL> def start_threads_and_join ( commands ) : <EOL> threads = [ threading . Thread ( target = command ) for command in commands ] <EOL> for thread in threads : <EOL> thread . start ( ) <EOL> for thread in threads : <EOL> thread . join ( ) <EOL> def get_log_for_test ( test_name , log_path , dt_format ) : <EOL> """<STR_LIT>""" <EOL> word_count = len ( dt_format . split ( ) ) <EOL> start_time = datetime . fromtimestamp ( runtime . get_active_test_start_time ( test_name ) ) <EOL> end_time = datetime . fromtimestamp ( runtime . get_active_test_end_time ( test_name ) ) <EOL> start_pos = _search_log_for_datetime ( log_path , start_time , word_count ) <EOL> end_pos = _search_log_for_datetime ( log_path , end_time , word_count , reverse = True ) <EOL> with open ( log_path , '<STR_LIT:r>' ) as log : <EOL> log . seek ( start_pos ) <EOL> return log . read ( end_pos - start_pos ) <EOL> def _search_log_for_datetime ( log_path , dt , word_count , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> ONE_KILOBYTE = <NUM_LIT> <EOL> with open ( log_path , '<STR_LIT:r>' ) as log : <EOL> lbound = <NUM_LIT:0> <EOL> ubound = os . path . getsize ( log_path ) <EOL> search_area = <NUM_LIT:0> <EOL> while ubound > lbound : <EOL> if ubound - lbound < ONE_KILOBYTE : <EOL> return _linear_search_log_for_time ( log_path , dt , word_count , lbound , ubound , reverse ) <EOL> mid = ( lbound + ubound ) / <NUM_LIT:2> <EOL> log . seek ( mid ) <EOL> log . readline ( ) <EOL> is_valid_dt = False <EOL> while not is_valid_dt : <EOL> if log . tell ( ) == ubound : <EOL> return _linear_search_log_for_time ( log_path , dt , word_count , lbound , ubound , reverse ) <EOL> line_start = log . tell ( ) <EOL> line = log . readline ( ) <EOL> timestr = '<STR_LIT:U+0020>' . join ( line . split ( ) [ : word_count ] ) <EOL> try : <EOL> log_dt = parser . parse ( timestr ) <EOL> if log_dt . time ( ) != dtime ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> is_valid_dt = True <EOL> except : <EOL> is_valid_dt = False <EOL> if log_dt > dt : <EOL> ubound = log . tell ( ) <EOL> elif log_dt < dt : <EOL> lbound = line_start <EOL> else : <EOL> if not reverse : <EOL> ubound = log . tell ( ) <EOL> else : <EOL> lbound = line_start <EOL> if ubound - lbound == search_area : <EOL> return _linear_search_log_for_time ( log_path , dt , word_count , lbound , ubound , reverse ) <EOL> search_area = ubound - lbound <EOL> def _linear_search_log_for_time ( log_path , dt , word_count , lbound , ubound , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> with open ( log_path , '<STR_LIT:r>' ) as log : <EOL> log . seek ( lbound ) <EOL> prev_line_start = lbound <EOL> line_start = log . tell ( ) <EOL> while log . tell ( ) < ubound : <EOL> is_valid_dt = False <EOL> while not is_valid_dt : <EOL> line = log . readline ( ) <EOL> timestr = '<STR_LIT:U+0020>' . join ( line . split ( ) [ : word_count ] ) <EOL> try : <EOL> log_dt = parser . parse ( timestr ) <EOL> is_valid_dt = True <EOL> except : <EOL> is_valid_dt = False <EOL> if log . tell ( ) == ubound : <EOL> if not reverse : <EOL> return prev_line_start <EOL> else : <EOL> return log . tell ( ) <EOL> if not reverse : <EOL> if log_dt >= dt : <EOL> return prev_line_start <EOL> else : <EOL> prev_line_start = line_start <EOL> line_start = log . tell ( ) <EOL> if line_start == ubound : <EOL> return prev_line_start <EOL> else : <EOL> if log_dt > dt : <EOL> return log . tell ( ) <EOL> else : <EOL> if log . tell ( ) == ubound : <EOL> return log . tell ( ) <EOL> else : <EOL> line_start = log . tell ( ) </s>
