Python 真是个好工具

Python 真是个好工具

想将一个文件按照配置文件内容进行命名。先选择了Linux shell,最后发现KEIL调用的命令行没有办法识别wsl,兼容性还是不太行。

#ifndef _SYSTEM_CONFIG_H_
#define _SYSTEM_CONFIG_H_


#define DEVICE_SW           1  // 软件版本
#define DEVICE_HW           1  // 硬件版本
#define DEVICE_TYPE         2  // 设备类型,1:网关 2:终端


#endif

generate_sw.sh

#!/bin/bash

path="../"
fwSrcName="Nordic_52840_firmware.bin"
baseName="Nordic_52840_firmware_sw"
configFile="../../config/system_config.h"
config1="DEVICE_SW"
config2="DEVICE_HW"
config3="DEVICE_TYPE"

softwareID=$(cat $configFile | grep $config1 | tr '\r' ' ' |  awk -F " " '{print $3}')
hardwareID=$(cat $configFile | grep $config2 | tr '\r' ' ' |  awk -F " " '{print $3}')
deviceType=$(cat $configFile | grep $config3 | tr '\r' ' ' |  awk -F " " '{print $3}')
currentTime=$(date +"%Y%m%d_%H%M")
fullName=$baseName$softwareID"_hw"$hardwareID"_dt"$deviceType"_"$currentTime".bin"
echo $fullName
mv $path$fwSrcName $path$fullName

modify_fw_name.bat

wsl ../script/generate_sw.sh
pause

直接双击是可以运行的。但是在KEIL中调用,会不识别wsl

最后改用Python,一下就搞定了。Python,牛逼。

# coding:utf-8
import os
import re
import time

f1 = open('../../config/system_config.h', 'r')

data1 = f1.readlines()

f1.close()

data1_len = len(data1)

# print(data1_len)

# print(data1)

config1 = 'DEVICE_SW'
config2 = 'DEVICE_HW'
config3 = 'DEVICE_TYPE'

baseName = "Nordic_52840_firmware_sw"
configFile="../../config/system_config.h"

path = "../"
binFile1_name = "Nordic_52840_firmware.bin"

currentDate = time.strftime("%Y%m%d_%H%M", time.localtime())
#print(currentDate)

for line_data in data1:
    t = line_data.find(config1)
    if t != -1:
        device_sw = re.findall(r"\d+\.?\d*", line_data[t:])
        #print(int(device_sw[0]))

    t = line_data.find(config2)
    if t != -1:
        device_hw = re.findall(r"\d+\.?\d*", line_data[t:])
        #print(int(device_hw[0]))
    
    t = line_data.find(config3)
    if t != -1:
        device_type = re.findall(r"\d+\.?\d*", line_data[t:])
        #print(int(device_type[0]))


fullName = baseName + device_sw[0] + "_hw" + device_hw[0] + "_dt" + device_type[0] + "_" + currentDate + ".bin"

#print(fullName)

binFile1 = path + binFile1_name
binFile2 = path + fullName
os.rename(binFile1, binFile2)

#currentPath = os.getcwd()
print("生成升级文件:" + os.path.abspath(os.path.dirname(binFile2))+ "\\" + fullName)

在这里插入图片描述

posted @   duapple  阅读(6)  评论(0编辑  收藏  举报  
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示