一个解析日志的脚本

#!/usr/bin/env python3
#coding=utf-8
import unicodedata
import os
import re
import sys
import subprocess


current_path = os.getcwd()
#print(current_path)
with os.popen('find $PWD  -maxdepth 1 -type d -name "*chi-cdk"') as chi_fd:
    chi_path = chi_fd.read().strip()
with os.popen('find $PWD -maxdepth 1 -type d -name "*camx"') as camx_fd:
    camx_path = camx_fd.read().strip()

#print(chi_path)
#print(camx_path)

#def writeCommitToCSV(inFile):
def writeCommitToCSV(inFile, outFile):
    '''
    # write header
    to_write = "序号,TagName,Count,来源,优化措施,备注\n"
    out_fd = open(outFile, 'w')
    out_fd.write(to_write)
    out_fd.close()
    '''
    # write to csv output file
    out_fd = open(outFile, 'w+', encoding='utf-8')
    #header = "序号,TagName,Count,来源,优化措施,备注\n"
    header = "NO.,TagName,Count,CommitID,Method,Note\n"
    #out_fd.write(header)

    line_num = 1  # line num
    target_src = "" # target file for parsing who commit it.
    line_src = 0 # line of src for parsing
    with open(inFile, 'r', encoding='utf-8') as in_fd:
        line = in_fd.readline()
        while line:
            # add `()` for group to get src file to parse
            #line_group = re.search(r'(.*\] )(.*(?=:))', line)
            #line_group = re.search(r'(.*\] )(.*(?=:)):((?<=:)\d+(?= ))(.*\(\)).*((?<=)\d+(?=$))', line)
            #line_group = re.search(r'(.*\] )(.*(?=:)):((?<=:)\d+(?= ))(.*\(\))\s+((?<=\s)\d+(?=$))', line)
            line_group = re.search(r'(.*\] )(.*(?=:)):((?<=:)\d+(?= ))(.*\(\))\s+((?<=\s)\d+)', line)
            if not line_group:
                if(re.match(r'^Time.*', line)):
                    out_fd.write('"' + line.strip() + '"\n')
                if(re.match(r'^TagName.*', line)):
                    out_fd.write(header)
                line_num = 1
                line = in_fd.readline()
                continue
            print(line_group.group())
            target_src = line_group.group(2)
            line_src = line_group.group(3)
            count = line_group.group(5)
            tagName = line_group.group(1) + line_group.group(2) + line_group.group(3) + line_group.group(4)
            '''
            print(target_src)
            print(line_src)
            print(count)
            print(tagName)
            '''

            # scan chi-cdk and camx
            os.chdir(chi_path)
            #print("chi111: Current Working Directory " , os.getcwd())
            find_cmd = 'find ./ -type f -name ' + target_src
            with os.popen(find_cmd) as find_chi_fd:
                relative_src_path = find_chi_fd.read().strip()
                if not relative_src_path:
                    os.chdir(camx_path)
                    #print("camx222: Current Working Directory " , os.getcwd())
                    with os.popen(find_cmd) as find_camx_fd:
                        relative_src_path = find_camx_fd.read().strip()
            #print("Current Working Directory " , os.getcwd())
            #print(relative_src_path)
            blame_cmd='git blame ' + relative_src_path + ' -L ' +  line_src+','+line_src
            #print(blame_cmd)
            with os.popen(blame_cmd) as blame_fd:
                commit_search = re.search(r'.*(?= \d\d:)', blame_fd.read().strip())
                if commit_search:
                    commit_owner = commit_search.group()
                else:
                    commit_owner = 'warning: commit not found'

                #print(commit_owner)

            line_to_csv = str(line_num) + ',' +  tagName + ',' + count + ',' + commit_owner + ', ' + ', ' + '\n'
            out_fd.write(line_to_csv)

            '''
            '''
            line_num += 1
            os.chdir(current_path)
            line = in_fd.readline()
    out_fd.close()
    print("\nGenerated:  " + outFile)
    print("Please open csv by excel and copy all of table to KM.")

inputFile = current_path + '/out_limit'
if len(sys.argv) == 1:
    print("Use default out_limit file to be parsed.")
else:
    para_list = sys.argv
    inputFile = current_path + '/' + para_list[1]
outputFile = inputFile + '_parse.csv'
'''
print(current_path)
print(inputFile)
print(outputFile)
'''

#writeCommitToCSV(inputFile)
writeCommitToCSV(inputFile, outputFile)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值