将components下的文件夹复制到别一个文件夹下,并重新生成CMakelist.txt文件

一、将components下的文件夹复制到别一个文件夹下,并重新生成CMakelist.txt文件

二、python程序

python 复制代码
import os
from pathlib import Path
import shutil

# 获取当前文件夹
current_directory = Path.cwd()
# 目标文件夹
target_root = Path(r"C:\***\esp32practise\01_led\components\BSP")
target_root_main = Path(r"C:\***\esp32practise\01_led\main")
# 获取当前文件夹下所有有效文件夹,并取出components的文件夹的集合
foler_3_name_set = set()
for folder_1 in current_directory.iterdir():
    # 过滤非文件夹和系统文件夹
    if not folder_1.is_dir() or folder_1.name.startswith('.'):
        continue 
    folder_components = folder_1/'components'/'BSP'
    folder_main = folder_1/'main'
    if not folder_components.is_dir():
        continue
    for foler_3 in folder_components.iterdir():
        if not foler_3.is_dir():
            continue
        foler_3_name_set.add(foler_3.name)
        dest = target_root / foler_3.name
        # shutil.copytree(foler_3,dest,dirs_exist_ok=True)
        # 获取当前文件夹下main的文件夹下的main.c文件,并更名为main_components模块名.c
        for file_main in folder_main.iterdir():
            if file_main.name == 'main.c':
                file_new_name = 'main_'+foler_3.name+'.c'
                dest_file = target_root_main/file_new_name
                shutil.copy2(file_main,dest_file)
                print('main_'+foler_3.name+'.c')

# 重新生成CMakelist.txt文件
CMakelist_file = target_root/'CMakeLists_1.txt'
with open(CMakelist_file,'w',encoding='utf-8') as file:
    add_str = ''
    for src_dirs in foler_3_name_set:
        add_str += ('    '+src_dirs+'\n')
    file.write("set(src_dirs\n")
    file.write(add_str)
    file.write(')\n')
    file.write("set(include_dirs\n")
    file.write(add_str)
    file.write(')\n')
    file.write('set(requires\n    driver)\n')
    file.write('idf_component_register(SRC_DIRS ${src_dirs} INCLUDE_DIRS ${include_dirs} REQUIRES ${requires})\n')
    file.write('component_compile_options(-ffast-math -O3 -Wno-error=format=-Wno-format)\n')
print('out:',src_dirs)
相关推荐
AC赳赳老秦2 小时前
风控岗应用:OpenClaw 采集公开司法与经营异常数据,自动生成企业风险评估报告
大数据·c语言·数据库·人工智能·python·php·openclaw
databook2 小时前
如何对稀疏数据的场景进行分析
python·数据挖掘·数据分析
Java&Develop3 小时前
DeltaFStation 宝塔 Linux 部署教程
python
小柯南敲键盘3 小时前
跨境电商图片翻译工具推荐:批量AI翻译+视频字幕+智能抠图
人工智能·python·音视频
北斗落凡尘3 小时前
LangGraph 入门实战(8)--绑定工具
python·langchain
ningmengjing_3 小时前
MCP 通讯方式与实现指南
python·agent·mcp
淼澄研学3 小时前
Kimi API黑产倒卖技术解析与Python合规接入指南
开发语言·网络·python
何以解忧,唯有..3 小时前
Python 元组(tuple)详解:使用、遍历与排序
开发语言·python
KANGBboy4 小时前
Python eval安全隐患
开发语言·python