import os
def getIndexIncludeLine(file):
'''
description:Remove the same reference header file in file2
param:
-file: xxx.h/xxx.cpp file (is array)
return:
-res: line Index for reference header file
'''
res = []
for index,line in enumerate(file):
if(line[0:8] == "#include"):
res.append(index)
return res
def readFile(path):
with open(path, "r") as f:
content = f.readlines()
return content
def writeFile(path,content):
try:
with open(path, "w") as f:
for line in content:
f.write(line)
return True
except:
return False
def compareFileInclude(file1path,file2path):
'''
description:Remove the same reference header file in file2
param:
-file1: xxx.h file (is array)
-file2: xxx.cpp file (is array)
return:True or False
'''
print(file1path)
print(file2path)
file1 = readFile(file1path)
file2 = readFile(file2path)
file1_index = getIndexIncludeLine(file1)
file2_index = getIndexIncludeLine(file2)
if(len(file1_index) == 0 or len(file2_index) == 0):
return True
# for file2_index building a dictionary
# file2_dict[i] = true => file2[i] is remove
file2_dict = {}
# building dictionary to compare
file2head_dict = {}
for i in file2_index:
lineStr = file2[i].strip()
file2head_dict[lineStr] = i
# compare
for i in file1_index:
lineStr = file1[i].strip()
if(lineStr in file2head_dict.keys()):
# remove
print("remove line is ",file2head_dict[lineStr])
file2_dict[file2head_dict[lineStr]] = True
# get return
file = []
for i in range(len(file2)):
if(i not in file2_dict.keys()):
file.append(file2[i])
return writeFile(file2path,file)
def getDirPath(rootPath):
sonFile = os.listdir(rootPath)
if("include" in sonFile and "src" in sonFile):
return [rootPath]
else:
res = []
for folder_name in sonFile:
sonDirPath = os.path.join(rootPath, folder_name)
if os.path.isdir(sonDirPath):
temp = getDirPath(sonDirPath)
for path in temp:
res.append(path)
return res
def main(rootPath):
path_list = getDirPath(rootPath)
print(path_list)
for path in path_list:
h_file = os.listdir(path + '\include')
cpp_file = os.listdir(path + '\src')
for file_name in h_file:
arr = file_name.split('.')
suffix = arr[-1]
if(suffix == 'h' or suffix =='hpp'):
cpp_filename = ''
for index in range(len(arr)-1):
cpp_filename += arr[index]
cpp_filename += '.cpp'
#compare
compareFileInclude(path+'\\include\\'+file_name,
path+'\\src\\'+cpp_filename)
if __name__ == "__main__":
rootPath = "E:\\IDE\VS_CODE\\workspace\\vscode_python\\test\\TEST"
main(rootPath)
去除cpp文件中与h/hpp文件相同的头文件
最新推荐文章于 2024-03-26 14:11:47 发布