python实现文件增量内容扫描(类似shell中的tail指令)

import time
import pickle
import  os
log_file = "/var/log/syslog"
refer_file = "/home/fmr/log-scan.refer.temp"
def _get_refer(p_refer_file):
    '''
    @param: 
    p_refer_file    存放last scan的mtime、position的临时文件
    
    @function 
    从指定的refer file中,读取其中存放的refer字典
    
    @return
    a dictionary like {'time': xxxxx , 'position':xxxxx}
    '''
    refer_file = p_refer_file
    refer = {'time':time.time(), 'position':0}
    if os.path.exists(refer_file):
        with open(refer_file, 'rb') as referd:
            try:
                refer = pickle.load(referd)
            except:
                pass
    #print(refer)
    return refer

def _update_refer(p_refer,p_refer_file):
    '''
    @funciton
    1. 将输入的refer序列化
    2. 将序列化后的refer dict写入指定临时文件
    
    @param
    p_refer:           包含time、position的一个dict
    p_refer_file:  存放序列化后的dict的临时文件
    '''
    refer = p_refer
    refer_file = p_refer_file
    with open(refer_file,'wb') as referd:
        pickle.dump(refer, referd)

_update_refer({'time':time.time(), 'position':0}, refer_file) 
while(True):
    refer = _get_refer(refer_file )
    file_mtime = os.path.getmtime(log_file)
    if file_mtime <= refer['time']:
        #print("file mtime did not change since last scan")
        refer['time'] = file_mtime
        _update_refer(refer,refer_file)
        continue
    file_size = os.path.getsize(log_file)
    if file_size <= refer['position']:
        #print("file size did not change since last scan")
        refer['position'] = file_size
        _update_refer(refer,refer_file)
        continue
    
    print("the file has changed , start to scan")
    with open(log_file) as logfd:
        logfd.seek(refer['position'], os.SEEK_SET)
        #for line in logfd.readline():
            #print(line)
        line = logfd.readline()
        while(line):
            print(line)
            #此处将读取到的每行数据上传至TDengine即可
            line = logfd.readline()
        refer = {'time':time.time(), 'position':logfd.tell()}
        _update_refer(refer, refer_file)
        

你可能感兴趣的:(python实现文件增量内容扫描(类似shell中的tail指令))