Python脚本开发-统计Rte中未连接的Port

文章目录

前言

项目开发过程中,经常会遇到应用层中Rte接口未连接导致的信号传递异常,本文介绍利用Python脚本对未连接的Rte port进行提取并导出到excel中

Autosar规范

此处我们主要针对于Rport,且是Explicit 类型的,RTE规范中定义如下:

对于未连接的Read Port,接口会返回RTE_E_UNCONNECTED,后面我们通过该特征来对此类接口进行提取

Python脚本开发

思路:

1.利用正则表达式提取文件中含RTE_E_UNCONNECTED的字符,并匹配对应的Rte接口名

复制代码
def find_rte_unconnected_in_file(file_path):
    """
    在单个文件中查找RTE_E_UNCONNECTED接口
    """
    interfaces = []
    
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
            
        # 查找RTE_E_UNCONNECTED相关的定义
        pattern = r'#define\s+(\w+).*RTE_E_UNCONNECTED'
        matches = re.findall(pattern, content)
        
        interfaces.extend(matches)
        
    except Exception as e:
        print(f"处理文件 {file_path} 时出错: {e}")
        
    return interfaces

2.遍历对应Rte文件夹下的.h文件

复制代码
def scan_directory_for_rte_unconnected(directory_path, file_extensions=None):
    """
    扫描目录中所有文件,查找RTE_E_UNCONNECTED接口
    """
    if file_extensions is None:
        file_extensions = ['.h']
        
    unconnected_interfaces = {}
    
    # 遍历目录中的所有文件
    for root, dirs, files in os.walk(directory_path):
        for file in files:
            if any(file.endswith(ext) for ext in file_extensions):
                file_path = os.path.join(root, file)
                interfaces = find_rte_unconnected_in_file(file_path)
                if interfaces:
                    unconnected_interfaces[file_path] = interfaces
                    
    return unconnected_interfaces

3.将匹配的结果导出到csv中

复制代码
def export_to_csv(results, output_file="unconnected_interfaces.csv"):
    """
    将未连接的接口导出到CSV文件,按不同文件分类
    """
    with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
        fieldnames = ['File', 'Interface']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        
        writer.writeheader()
        
        # 按文件分组写入
        for file_path, interfaces in results.items():
            for interface in interfaces:
                writer.writerow({
                    'File': file_path,
                    'Interface': interface
                })
    
    print(f"结果已导出到 {output_file}")

使用示例:

复制代码
# 使用示例
if __name__ == "__main__":
    directory_path = "./BasicSoftware/src/rte/gen"
    results = scan_directory_for_rte_unconnected(directory_path)
    
    # 打印结果到控制台
    print("未连接的接口:")
    for file_path, interfaces in results.items():
        print(f"\n文件: {file_path}")
        for interface in interfaces:
            print(f"  - {interface}")
    
    # 导出到CSV文件
    if results:
        export_to_csv(results)
    else:
        print("未找到未连接的接口")

实现效果

导出到csv中的文件路径及Port:

总结

对于未连接的port,需要充分评估,避免因port未连而导致的信号传递不正常,导致实际功能失效

提示:完整脚本可以通过《汽车电子学习笔记》-回复《 PY RTE》获取~

相关推荐
用户83562907805111 分钟前
用Python轻松管理Word页脚:批量处理与多节文档技巧
后端·python
进击的松鼠22 分钟前
LangChain 实战 | 快速搭建 Python 开发环境
python·langchain·llm
小北方城市网28 分钟前
第1课:架构设计核心认知|从0建立架构思维(架构系列入门课)
大数据·网络·数据结构·python·架构·数据库架构
我的offer在哪里1 小时前
Hugging Face:让大模型触手可及的魔法工厂
人工智能·python·语言模型·开源·ai编程
汤姆yu1 小时前
基于python大数据的协同过滤音乐推荐系统
大数据·开发语言·python
爱学习的小道长1 小时前
Python Emoji库的使用教程
开发语言·python
Data_agent1 小时前
Cssbuy 模式淘宝 / 1688 代购系统南美市场搭建指南
大数据·python
xyt11722281772 小时前
宗地四至提取工具
python·arcgis
程序员三藏2 小时前
接口自动化测试之 pytest 接口关联框架封装
自动化测试·软件测试·python·测试工具·测试用例·pytest·接口测试
江湖yi山人2 小时前
生产环境的log,上传到开发者的本地服务器
javascript·python