<s> import time <EOL> import os <EOL> import re <EOL> from . import log <EOL> def file_rotated ( file_name , orig_stat ) : <EOL> while True : <EOL> try : <EOL> new_stat = os . stat ( file_name ) <EOL> if orig_stat == None : <EOL> return True , new_stat <EOL> elif orig_stat . st_ino != new_stat . st_ino : <EOL> return True , new_stat <EOL> else : <EOL> return False , orig_stat <EOL> except OSError : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> def tail_lines ( file_name ) : <EOL> _ , orig_stat = file_rotated ( file_name , None ) <EOL> log_file = open ( file_name ) <EOL> log_file . seek ( <NUM_LIT:0> , os . SEEK_END ) <EOL> while True : <EOL> line = log_file . readline ( ) <EOL> if line : <EOL> yield line <EOL> else : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> rotated , orig_stat = file_rotated ( file_name , orig_stat ) <EOL> if rotated : <EOL> log . info ( "<STR_LIT>" % file_name ) <EOL> log_file . close ( ) <EOL> log_file = open ( file_name ) <EOL> def convert_patterns ( patterns ) : <EOL> results = { } <EOL> for pattern , target in patterns . items ( ) : <EOL> matcher = re . compile ( pattern ) <EOL> results [ matcher ] = target <EOL> return results <EOL> def scan_lines ( file_name , patterns ) : <EOL> patterns = convert_patterns ( patterns ) <EOL> for line in tail_lines ( file_name ) : <EOL> for pattern , target in patterns . items ( ) : <EOL> matches = pattern . match ( line ) <EOL> if matches : <EOL> yield target , line , matches . groupdict ( ) </s>
