目录文件操作脚本对应

#-------------------------------------------------------------------------------        
# Author:      cuixin
# Created:     9/1/2023
# File:        main.py
# Version:     1.0
# Desc:
#-------------------------------------------------------------------------------

import sys, os, shutil

# ------------------------------common  utils------------------------------ #
def create_dir(file_path):
    if os.path.exists(file_path) is False:
        os.makedirs(file_path)

def list_allfile(path,all_files=[]):    
    if os.path.exists(path):
        files=os.listdir(path)
    else:
        print('this path not exist')
    for file in files:
        if os.path.isdir(os.path.join(path,file)):
            list_allfile(os.path.join(path,file),all_files)
        else:
            all_files.append(os.path.join(path,file))
    return all_files

def copy_dir(dir1, dir2):
    dlist = os.listdir(dir1)
    if not os.path.exists(dir2):
        os.makedirs(dir2)
    for f in dlist:
        file1 = os.path.join(dir1, f)  # 源文件
        file2 = os.path.join(dir2, f)  # 目标文件
        if os.path.isfile(file1):
            shutil.copyfile(file1, file2)
        if os.path.isdir(file1):
            copy_dir(file1, file2)

def copy_h_files(dir1, dir2):
    dlist = os.listdir(dir1)
    if not os.path.exists(dir2):
        os.makedirs(dir2)
    for f in dlist:
        file1 = os.path.join(dir1, f)
        file2 = os.path.join(dir2, f)
        if f.endswith('.h'):
            if os.path.isfile(file1):
                shutil.copyfile(file1, file2)

def copy_a_files(file1, file2):
    if os.path.isfile(file1):
        shutil.copyfile(file1, file2)    

def handle_c(dir1, dst_path, new_path):
    '''
    OS_EE_FAWAPP_CFG_SRCS += $(wildcard ../../AppCode/FAW_ASW/Sf_VM_L3_DrivingControl1/*.c)
    '''
    dlist = os.listdir(dir1)
    for f in dlist:
        file1 = os.path.join(dir1, f)
        if os.path.isfile(file1):
            f = open(new_path, 'a')
            content = "OS_EE_FAWAPP_CFG_SRCS += $(wildcard ../../AppCode/FAW_ASW" + dst_path + "*.c)" + "\n"
            f.write("{}".format(content))
            f.close

        if os.path.isdir(file1):
            dst_path_tem = os.path.join(dst_path, f)
            dst_path_tem = dst_path_tem + "/"
            handle_c(file1, dst_path_tem, new_path)


# -------------------------------function---------------------------------- #
def create():
    current_dir = os.getcwd()
    root_dir = os.path.abspath(os.path.dirname(os.getcwd()))
    old_dir_name =  current_dir.split('\\')[1]
    new_dir_name = old_dir_name + '_FAW'
    project = new_dir_name

    # copy: OS_Config,Tools,UserModify,Rebuild.bat,build
    # OS_Config
    file_path = root_dir+"\\"+project +"\\"+'OS_Config'
    faw_path = current_dir+"\\"+'OS_Config'
    if os.path.exists(faw_path):
        copy_dir(faw_path, file_path)
    # Tools
    file_path = root_dir+"\\"+project +"\\"+'Tools'
    faw_path = current_dir+"\\"+'Tools'
    if os.path.exists(faw_path):
        copy_dir(faw_path, file_path)
    # UserModify
    file_path = root_dir+"\\"+project +"\\"+'UserModify'
    faw_path = current_dir+"\\"+'UserModify'
    if os.path.exists(faw_path):
        copy_dir(faw_path, file_path)
    # Rebuild.bat
    old_path = os.path.join(current_dir, 'Rebuild.bat')
    new_path = root_dir+"\\"+project+"\\"+'Rebuild.bat'
    copy_a_files(old_path,new_path)
    # build
    file_path = root_dir+"\\"+project +"\\"+'build'
    faw_path = current_dir+"\\"+'build'
    if os.path.exists(faw_path):
        copy_dir(faw_path, file_path)

    # AppCode
    appcode = 'AppCode'
    faw_asw = 'FAW_ASW'
    file_path = root_dir+"\\"+project+"\\"+appcode+"\\"+faw_asw
    create_dir(file_path)
    faw_path = current_dir+"\\"+appcode+"\\"+faw_asw
    if os.path.exists(faw_path):
        copy_dir(faw_path, file_path)

    ft_asw = 'FT_ASW'
    include = 'include'
    file_path = root_dir+"\\"+project+"\\"+appcode+"\\"+ft_asw+"\\"+include
    create_dir(file_path)

    lib = 'lib'
    file_path = root_dir+"\\"+project+"\\"+appcode+"\\"+lib
    create_dir(file_path)

    # CDD
    cdd = 'CDD'
    include = 'include'
    file_path = root_dir+"\\"+project+"\\"+cdd+"\\"+include
    create_dir(file_path)

    lib = 'lib'
    file_path = root_dir+"\\"+project+"\\"+cdd+"\\"+lib
    create_dir(file_path)

    # Intergration
    intergration = 'Intergration'
    include = 'include'
    file_path = root_dir+"\\"+project+"\\"+intergration+"\\"+include
    create_dir(file_path)

    lib = 'lib'
    file_path = root_dir+"\\"+project+"\\"+intergration+"\\"+lib
    create_dir(file_path)

    # MCAL
    mcal = 'MCAL'
    include = 'include'
    file_path = root_dir+"\\"+project+"\\"+mcal+"\\"+include
    create_dir(file_path)

    lib = 'lib'
    file_path = root_dir+"\\"+project+"\\"+mcal+"\\"+lib
    create_dir(file_path)

    # VOS
    vos = 'VOS'
    bsw = 'BSW'
    include = 'include'
    file_path = root_dir+"\\"+project+"\\"+vos+"\\"+bsw+"\\"+include
    create_dir(file_path)

    rte = 'Rte'
    include = 'include'
    file_path = root_dir+"\\"+project+"\\"+vos+"\\"+rte+"\\"+include
    create_dir(file_path)

    os_ = 'OS'
    include = 'include'
    file_path = root_dir+"\\"+project+"\\"+vos+"\\"+os_+"\\"+include
    create_dir(file_path)

    lib = 'lib'
    file_path = root_dir+"\\"+project+"\\"+vos+"\\"+lib
    create_dir(file_path)

    # Out
    out = 'Out'
    file_path = root_dir+"\\"+project+"\\"+out
    create_dir(file_path)

    print('create end......end')

def cp_h_file():
    current_dir = os.getcwd()
    root_dir = os.path.abspath(os.path.dirname(os.getcwd()))
    old_dir_name =  current_dir.split('\\')[1]
    new_dir_name = old_dir_name + '_FAW'
    appdirfile = r".\UserModify\mkfiles\app_dir.mk"

    for line in open(appdirfile).readlines():
        if "CDD_DIR +" in line and line.startswith('CDD_DIR'):
            currname = line.split(',')[1].split(')')[1]
            cdd_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"CDD"+"\\"+"include"
            copy_h_files(cdd_path,new_path)
        elif "MCAL_DIR +" in line and line.startswith('MCAL_DIR'):
            currname = line.split(',')[1].split(')')[1]
            mcal_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"MCAL"+"\\"+"include"
            copy_h_files(mcal_path,new_path)
        elif "BSW_DIR +" in line and line.startswith('BSW_DIR'):
            currname = line.split(',')[1].split(')')[1]
            bsw_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"VOS"+"\\"+"BSW"+"\\"+"include"
            copy_h_files(bsw_path,new_path)
        elif "LIB_DIR +" in line and line.startswith('LIB_DIR'):
            currname = line.split(',')[1].split(')')[1]
            lib_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"VOS"+"\\"+"BSW"+"\\"+"include"
            copy_h_files(lib_path,new_path)
        elif "RTE_DIR +" in line and line.startswith('RTE_DIR'):
            currname = line.split(',')[1].split(')')[1]
            rte_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"VOS"+"\\"+"Rte"+"\\"+"include"
            copy_h_files(rte_path,new_path)
        elif "OS_DIR +" in line and line.startswith('OS_DIR'):
            currname = line.split(',')[1].split(')')[1]
            if currname == '/src':
                currname = '/OS_Config/erika/src'
            os_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"VOS"+"\\"+"OS"+"\\"+"include"
            copy_h_files(os_path,new_path)
        elif "CFG_DIR +" in line and line.startswith('CFG_DIR'):
            currname = line.split(',')[1].split(')')[1]
            if currname == '/cfg':
                currname = '/OS_Config/cfg'
            cfg_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"VOS"+"\\"+"OS"+"\\"+"include"
            copy_h_files(cfg_path,new_path)
        elif "STUB_DIR +" in line and line.startswith('STUB_DIR'):
            currname = line.split(',')[1].split(')')[1]
            stub_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"Intergration"+"\\"+"include"
            copy_h_files(stub_path,new_path)
        elif "FTAPP_DIR +" in line and line.startswith('FTAPP_DIR'):
            currname = line.split(',')[1].split(')')[1]
            stub_path = current_dir + currname
            new_path = root_dir+"\\"+new_dir_name+"\\"+"AppCode"+"\\"+"FT_ASW"+"\\"+"include"
            if os.path.exists(stub_path):
                copy_h_files(stub_path,new_path)

    print('cp_h_file......end')

def cp_a_file():
    current_dir = os.getcwd()
    root_dir = os.path.abspath(os.path.dirname(os.getcwd()))
    old_dir_name =  current_dir.split('\\')[1]
    new_dir_name = old_dir_name + '_FAW'
    appdirfile = r".\OS_Config\out\lib"
    if os.path.exists(appdirfile):
        files=os.listdir(appdirfile)
        for i in files:
            if i == 'libCDD.a':
                old_path = os.path.join(appdirfile, i)
                new_path = root_dir+"\\"+new_dir_name+"\\"+"CDD"+"\\"+"lib"+"\\"+i
                copy_a_files(old_path,new_path)
            elif i == 'libee.a':
                old_path = os.path.join(appdirfile, i)
                new_path = root_dir+"\\"+new_dir_name+"\\"+"VOS"+"\\"+"lib"+"\\"+i
                copy_a_files(old_path,new_path)
            elif i == 'libFAW_ASW.a':
                old_path = os.path.join(appdirfile, i)
                new_path = root_dir+"\\"+new_dir_name+"\\"+"AppCode"+"\\"+"lib"+"\\"+i
                copy_a_files(old_path,new_path)
            elif i == 'libFT_ASW.a':
                old_path = os.path.join(appdirfile, i)
                new_path = root_dir+"\\"+new_dir_name+"\\"+"AppCode"+"\\"+"lib"+"\\"+i
                copy_a_files(old_path,new_path)
            elif i == 'libMCAL.a':
                old_path = os.path.join(appdirfile, i)
                new_path = root_dir+"\\"+new_dir_name+"\\"+"MCAL"+"\\"+"lib"+"\\"+i
                copy_a_files(old_path,new_path)
            elif i == 'libVOS.a':
                old_path = os.path.join(appdirfile, i)
                new_path = root_dir+"\\"+new_dir_name+"\\"+"VOS"+"\\"+"lib"+"\\"+i
                copy_a_files(old_path,new_path)

        print('cp_a_file......end')

def update_app_dir():
    current_dir = os.getcwd()
    root_dir = os.path.abspath(os.path.dirname(os.getcwd()))
    old_dir_name =  current_dir.split('\\')[1]
    new_dir_name = old_dir_name + '_FAW'
    old_path = r".\Tools\AutoScripts\TargetMkfiles\app_dir.mk"
    new_path = root_dir+"\\"+new_dir_name+"\\"+"UserModify"+"\\"+"mkfiles"+"\\"+"app_dir.mk"

    if os.path.exists(new_path):
        copy_a_files(old_path,new_path)

    print('update_app_dir......end')

def update_app_src():
    current_dir = os.getcwd()
    root_dir = os.path.abspath(os.path.dirname(os.getcwd()))
    old_dir_name =  current_dir.split('\\')[1]
    new_dir_name = old_dir_name + '_FAW'
    old_path = r".\Tools\AutoScripts\TargetMkfiles\app_src.mk"
    new_path = root_dir+"\\"+new_dir_name+"\\"+"UserModify"+"\\"+"mkfiles"+"\\"+"app_src.mk"
    
    if os.path.exists(new_path):
        copy_a_files(old_path,new_path)

        # 处理.c文件
        old_dir = root_dir+"\\"+new_dir_name+"\\"+"AppCode"+"\\"+"FAW_ASW"
        dst_path = "/"
        handle_c(old_dir, dst_path, new_path)

    print('update_app_src......end')

def update_app_obj():
    current_dir = os.getcwd()
    root_dir = os.path.abspath(os.path.dirname(os.getcwd()))
    old_dir_name =  current_dir.split('\\')[1]
    new_dir_name = old_dir_name + '_FAW'
    old_path = r".\Tools\AutoScripts\TargetMkfiles\app_obj.mk"
    new_path = root_dir+"\\"+new_dir_name+"\\"+"UserModify"+"\\"+"mkfiles"+"\\"+"app_obj.mk"
    
    if os.path.exists(new_path):
        copy_a_files(old_path,new_path)

    print('update_app_obj......end')

def update_rebuild():
    current_dir = os.getcwd()
    root_dir = os.path.abspath(os.path.dirname(os.getcwd()))
    old_dir_name =  current_dir.split('\\')[1]
    new_dir_name = old_dir_name + '_FAW'
    old_path = r".\Tools\AutoScripts\TargetMkfiles\Rebuild.bat"
    new_path = root_dir+"\\"+new_dir_name+"\\"+"Rebuild.bat"
    
    if os.path.exists(new_path):
        copy_a_files(old_path,new_path)

    print('update_rebuild......end')

# 拉取最新的.c文件
def get_c_file():
    pass


def init():
    try:
        # 创建新project工程目录
        create()

        # 解析app_dir.mk,将h文件cp到新的文件里面
        cp_h_file()

        # cp 老的.a文件
        cp_a_file()

        # 更新app_dir.mk
        update_app_dir()

        # 更新app_src.mk
        update_app_src()

        # 更新app_obj.mk
        update_app_obj()

        # 删除Rebuild.bat 71
        update_rebuild()

        print('init end......')


    except Exception as e:
        print("e=%s" % e)
        sys.exit(-1)


if __name__ == '__main__':
    init()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值