import unicodedata
import os
import re
import sys
import subprocess
current_path = os.getcwd()
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()
def writeCommitToCSV(inFile, outFile):
'''
# write header
to_write = "序号,TagName,Count,来源,优化措施,备注\n"
out_fd = open(outFile, 'w')
out_fd.write(to_write)
out_fd.close()
'''
out_fd = open(outFile, 'w+', encoding='utf-8')
header = "NO.,TagName,Count,CommitID,Method,Note\n"
line_num = 1
target_src = ""
line_src = 0
with open(inFile, 'r', encoding='utf-8') as in_fd:
line = in_fd.readline()
while 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)
'''
os.chdir(chi_path)
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)
with os.popen(find_cmd) as find_camx_fd:
relative_src_path = find_camx_fd.read().strip()
blame_cmd='git blame ' + relative_src_path + ' -L ' + line_src+','+line_src
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'
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, outputFile)