使用 Python 删除 FPGA 工程的多余文件
删除 FPGA 工程的多余文件,仅保留 xxx.srcs、xxx.xpr、xxx.runs 三个文件,xxx.runs 文件夹中仅保留 impl_1 文件夹中的 bit 和 bin。
示例代码如下:
注意:需要安装 send2trash 库(pip install send2trash)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from send2trash import send2trash
def ClearPrefix(folder):
if(folder[:2] == '.\\'):
folder = folder[2:]
if(folder[-1:] == '\\'):
folder = folder[:-1]
return folder
def DeleteToTrash(path):
if not os.path.exists(path):
return "The destination folder does not exist!"
try:
if os.path.exists(path):
send2trash(path)
print("Files send to trash: ", path)
else:
print("The destination files does not exist!")
except Exception as e:
print("An error occurred while deleting the folder: ", str(e))
def main():
argv = sys.argv
arg_num = len(argv)
# 判断输入参数,处理路径前缀
if(arg_num == 1):
print("Error: Please enter directory you want to delete!\n")
return
elif(arg_num > 2):
print("Error: Too many arguments!\n")
elif(arg_num == 2):
print("# Prepare to delete useless files in the following foldes:")
argv[1] = ClearPrefix(argv[1])
print("# %d. %s \n" % (1, argv[1]))
# 工程根目录
prj_paths = os.path.join(os.getcwd(), argv[1])
# 判断目录是否为Xilinx工程文件夹
is_fpga_folder = os.listdir(prj_paths)
if ".Xil" not in is_fpga_folder:
print("Please make sure the folder is fpga project!")
return
for prj_path in os.listdir(prj_paths):
# .runs 文件夹
if(prj_path[-5:] == ".runs"):
runs_path = os.path.join(prj_paths, prj_path)
for cur_folder in os.listdir(runs_path):
# 删除.runs中的多余文件
if(cur_folder == "impl_1"):
cur_path = os.path.join(runs_path, cur_folder)
# 删除impl_1中的多余文件
for files in os.listdir(cur_path):
if(files[-4:] == ".bit" or files[-4:] == ".bin"):
continue
DeleteToTrash(os.path.join(cur_path, files))
else:
DeleteToTrash(os.path.join(runs_path, cur_folder))
# .srcs 文件夹
elif(prj_path[-5:] == ".srcs"):
continue
# .xpr 文件
elif(prj_path[-4:] == ".xpr"):
continue
# 工程根目录其他文件删除
else:
# print("Debug: ", os.path.join(prj_paths, prj_path))
DeleteToTrash(os.path.join(prj_paths, prj_path))
if __name__ == '__main__':
main()