自动添加所缺头文件脚本

该脚本通过读取make命令的错误输出,检测缺失的头文件,并自动从/usr3/code/VRP目录中查找并复制到指定源代码目录,以解决编译错误。它使用grep和正则表达式定位头文件,然后使用find命令找到文件路径并进行复制。
摘要由CSDN通过智能技术生成
import os

def check_error(log_path):
    with open(log_path, 'r', encoding='utf-8') as temp_f:
        datafile = temp_f.readlines()
        for line in datafile:
            if '.h: No such file or directory' in line:
                return True
        return False

# 在make命令下报缺少头文件的头文件依次添加进去,直到没有报缺头文件的错误
def add_header():
    while 1:
        res = os.system("make -j20 SRC_DIR=../../src/ipbase/hrs/src/common/session_lib > ./temp_file/log.txt 2>&1")   # make命令以及需要重定向的文件,2>&1是把错误信息也打印进去
        print(res)
        log_path = './temp_file/log.txt'
        if not check_error(log_path): #判断跳出条件
            break
        os.system("cat ./temp_file/log.txt | grep \#include > ./temp_file/log1.txt") #
        fname = './temp_file/log1.txt'
        with open(fname, 'r', encoding='utf-8') as f:  # 打开文件
            first_line = f.readline()
        header =first_line.split("\"")[-2]
        print(header)
        cmd = f'find /usr3/code/VRP/ -name {header} > ./temp_file/header_path.txt'    # 拼接命令
        res1 = os.system(cmd)
        print(res1)
        path_file = './temp_file/header_path.txt'
        with open(path_file, 'r', encoding='utf-8') as f1:  # 打开文件
            first_line1 = f1.readline().strip()
        copy_cmd = f'cp {first_line1} ./../../src/ipbase/hrs/src/common/session_lib'
        res2 = os.system(copy_cmd)
        print(res2)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # print_hi('PyCharm')
    add_header()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值