IAR 工程自动导入SourceInsight
引言
- 最近用公司的代码框架,准备使用SourceInsight进行代码编辑阅读,发现框架的东西分散的太多,一个个导入太耗时,如果全部文件导入,又会有文件的冲突。所以开发一个python工具来进行自动的导入。
IAR工程文件的分析
- IAR工程文件有5个,我们只需要使用dep文件,这其实是一个XML格式文件,我们从里面取出工程使用到的.C.H.S.CPP文件的路径,并生成txt文本格式。
- 这里有个注意点,$TOOLKIT_DIR$和$PROJ_DIR$需要用绝对路径替换。
导入SourceInsight
-
将生成的txt文件导入SourceInsight,大功告成。
Python源码实现
- 运行下列代码,会在IAR的项目目录下生成txt文件,如果项目有多个模式,txt文件也会有多个,名称会包含如 debug,release之类。
- 如果有用请记得5星好评
from xml.dom.minidom import parse
import regex as re
import os,sys
def user_comments():
return '"***********Welcom to use the IAR to SourceInsight tool***********"\
\nUser Notes:\
\n 1.If no arg like "PROJ_DIR=D:\Project\\1350\ide" pass, the tool will run in the current folder\
\n 2.If no arg like "TOOLKIT_DIR=D:\IAR\\arm" pass, the output file will not include IAR lib files\
\n 3.The tool will convet all dep files\
\n 4.The output file will include this file types:.c .h .s\
\nVersion:1.0\
\nAutor:Leon Hu\
\nRelease Data:5/20/2020'
'''IAR exe path'''
#TOOLKIT_DIR=r'D:\IAR\arm'
TOOLKIT_DIR=''
'''Creat mode
* 0:不输出TOOLKIT_DIR的文件 (default)
* 1:输出包含TOOLKIT_DIR的文件(TOOLKIT_DIR set)
'''
model=0
def data_process(data):
pattern = re.compile(r'\.c|\.h|\.s|\.cpp',re.I) # .h.c.s .cpp 文件
if re.search(pattern,data) != None:
data=data.replace(r'$PROJ_DIR$',project_dir)
if model ==0 and data.find(r'$TOOLKIT_DIR$') != -1:
data=None
else:
data=data.replace(r'$TOOLKIT_DIR$',TOOLKIT_DIR)
return data
else:
return None
def creat_file_list(filename,filepath):
DOMTree=parse(filepath+'/'+filename)
allnodes=DOMTree.documentElement
configuration_nodes=allnodes.getElementsByTagName('configuration')
for configNode in configuration_nodes:
output_nodes=configNode.getElementsByTagName('outputs')
if len(output_nodes) >1:
name=configNode.getElementsByTagName('name')[0].firstChild.nodeValue
#print(name)
savefile=open(filepath+'/'+filename[:-4]+'_'+name+'.txt','w+')
file_nodes=output_nodes[0].getElementsByTagName('file')
for file in file_nodes:
value=file.childNodes[0].nodeValue
data=data_process(value)
if data != None:
savefile.writelines(data_process(value))
savefile.write('\n')
savefile.close()
print(filepath+'/'+filename[:-4]+'_'+name+'.txt'+' saved')
if __name__ == "__main__":
print(user_comments())
print()
print('*******arg config:*********')
root_path=sys.argv[0]
project_dir,filename=os.path.split(root_path)
for arg in sys.argv[1:]:
if arg.find('TOOLKIT_DIR') !=-1:
TOOLKIT_DIR=arg[arg.find('=')+1:]
model=1
print('"TOOLKIT_DIR" has set as '+TOOLKIT_DIR)
if arg.find('PROJ_DIR') !=-1:
project_dir=arg[arg.find('=')+1:]
print('"PROJ_DIR" has set as '+project_dir)
print()
file_list=[]
for i in os.listdir(project_dir):
if re.search(r'\.dep',i) != None:
file_list.append(i)
print('*******convet start*******')
for file in file_list:
print(file)
creat_file_list(file,project_dir)
print()
print('*******convet end*********')