IAR 工程自动导入SourceInsight

本文介绍了一款Python工具,用于自动将IAR工程文件导入SourceInsight,提高代码编辑和阅读效率。通过解析IAR的dep文件,提取.C、.H、.S、.CPP文件路径,生成txt文件供SourceInsight导入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言

  • 最近用公司的代码框架,准备使用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*********')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值