python
import os
import re
import math
import shutil
import xlsxwriter
import pandas as pd
source_path = os.path.abspath(r'H:\ALM\model\rxengine-v0.2_0414\rxengine\RxEngine')
target_path = os.path.abspath(r'I:\临时\RxEngine')
try:
os.rmdir(target_path)
except:
pass
if not os.path.exists(target_path):
# 如果目标路径不存在原文件夹的话就创建
os.makedirs(target_path)
if os.path.exists(source_path):
# 如果目标路径存在原文件夹的话就先删除
shutil.rmtree(target_path)
shutil.copytree(source_path, target_path)
print('copy dir finished!')
path=r'I:\临时\RxEngine'
alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
def searchlist(Str):
key_list = ['(','+','-','[',']','/','|','!']
if any(key in Str for key in key_list):
return True
def code_deal(path,file_name,type):
name=file_name[:-len(type)]
space_name=name+'::'
file_h=name+'.h'
with open(path[:-3]+'head\\'+file_h, "r", encoding="gbk") as f_read:
content = f_read.readlines()
length=len(content)
class_names=[]
for i in range(length):
line=content[i]
if len(line)>1:
num1=line.find('class')
num3=line.find('\n')
if num1!=-1:
num2=line[num1+6:].find(' ')
if num2>0:
class_name=line[num1+6:num1+6+num2]
elif num3>0:
class_name=line[num1+6:num3]
class_names.append(class_name)
with open(path+'\\'+file_name, "r", encoding="gbk") as f_read:
content = f_read.readlines()
length=len(content)
df_function=pd.DataFrame(columns=['function_names','start_line','end_line'])
rank=-1
for class_name in class_names:
space_name=class_name+'::'
lenofword=len(space_name)
for i in range(length):
line=content[i]
if len(line)>1:
num1=line.find(space_name)
num2=searchlist(line)
num3=line.find('(')
if num1!=-1:
if line[num1:].find('(')!=-1:
function_name=line[num1+lenofword:num3]
rank+=1
v1=i
if line[-2]==')':
v2=i
df_function=df_function.append(pd.DataFrame({
'function_names':[function_name],'start_line':[v1],'end_line':[v2]}),ignore_index=True)
elif line[-8:]==') const\n':
v2=i
df_function=df_function.append(pd.DataFrame({
'function_names':[function_name],'start_line':[v1],'end_line':[v2]}),ignore_index=True)
elif line[-2]==')' and not num2:
try:
df_function['end_line'].loc[rank]
except:
v2=i
df_function=df_function.append(pd.DataFrame({
'function_names':[function_name],'start_line':[v1],'end_line':[v2]}),ignore_index=True)
elif line[-8:]==') const\n':
try:
df_function['end_line'].loc[rank]
except:
v2=i
df_function=df_function.append(pd.DataFrame({
'function_names':[function_name],'start_line':[v1],'end_line':[v2]}),ignore_index=True)
result=[]
for i in range(len(df_function)):
function=''
for j in range(df_function['start_line'][i],df_function['end_line'][i]+1):
function=function+content[j]
result.append(function)
Result=[]
for i in range(len(result)):
num2=result[i].find('(')
num3=result[i].find(')')
variable=result[i][num2+1:num3]
xx=variable.replace(',\n',',').replace(', \n',',').replace(',',',\n').replace('\n','').replace('\t','')
Result.append(xx)
data=[]
for i in range(len(Result)):
var_before=Result[i].split(',')
Del=[]
for i in range(len(var_before)):
if "map<" in var_before[i]:
none=var_before[i+1].find(' ')
var_before[i]=var_before[i]+','+var_before[i+1][none+1:]
Del.append(i+1)
if Del:
lendel=len(Del)
for j in range(lendel):
del(var_before[Del[lendel-j-1]])
else:
pass
var_after=[]
for j in var_before:
if len(j)>1:
if j[1]==' ':
var_after.append(j[2:])
elif j[0]==' ':
var_after.append(j[1:])
else:
var_after.append(j)
data.append(var_after)
workbook = xlsxwriter.Workbook('{}\{}.xlsx'.format(path,name))
worksheet = workbook.add_worksheet(name)
# heading
headings=[]
for i in df_function['function_names']:
headings.append('函数名称:')
headings.append(i)
headings.append('')
headings.append('')
worksheet.write_row('A1',headings)
# body
try:
for i in range(len(data)):
varcom=data[i]
for j in range(len(varcom)):
varsp=varcom[j].split(' ')
lenvarsp=len(varsp)
if lenvarsp==2:
pass
elif lenvarsp==3:
varsp=varsp[1:]
else:
print('*'*20,'数据处理异常(变量)')
print('{}\{}'.format(path,name))
if math.floor(i*4/26)==0:
worksheet.write_row('{}{}'.format(alphabet[i*4],j+2),varsp)
else:
worksheet.write_row('{}{}{}'.format(alphabet[math.floor(i*4/26)-1],alphabet[(i*4)%26],j+2),varsp)
workbook.close()
except:
print('*'*20,'写入excel文件失败!')
print('{}\{}'.format(path,name))
def search_file_type(path1,file,type):
if file.endswith(type):
code_deal(path1,file,type)
def search_file_path(path2):
for file in os.listdir(path2):
try:
search_file_type(path2,file,'.cpp')
except:
continue
log_path="{}\{}".format(path2,file)
if os.path.isdir(log_path):
search_file_path(log_path)
search_file_path(path)