import fileinput import os tempelate_file_path = '.\\template.code-workspace_t' target_file_path = '.\\violet_firmware_zf.code-workspace' print("*************************************************************************************") while True: print("*************************************************************************************") mrs_riscv_gcc_path_str = input("请输入 MRS 工具链下 RISCV-GCC 路径 (例如 D:\\xpack-riscv-none-embed-gcc-8.3.0-2.3):\n") print("获取到输入:%s" %mrs_riscv_gcc_path_str) if not os.path.exists(mrs_riscv_gcc_path_str): print("GCC 路径错误,请输入有效路径") else: print("输入正确") break while True: print("*************************************************************************************") mrs_openocd_path_str = input("请输入 MRS 工具链下 OpenOCD 路径 (例如 D:\\OpenOCD\\bin\\openocd.exe):\n") print("获取到输入:%s" %mrs_openocd_path_str) if not os.path.exists(mrs_openocd_path_str): print("OpenOCD 路径错误,请输入有效路径") else: print("输入正确") break mrs_riscv_gcc_path_str = mrs_riscv_gcc_path_str.replace("\\", "/") mrs_openocd_path_str = mrs_openocd_path_str.replace("\\", "/") # 检查模板文件是否存在 if not os.path.isfile(tempelate_file_path): print(f"模板文件 {tempelate_file_path} 不存在") exit(1) # 复制模板文件到工作区文件 if os.path.exists(target_file_path): print(f"工作区文件 {target_file_path} 已存在,将会被覆盖") if_continue = input("输入 y 以继续:\n") if if_continue != 'y': print("程序退出") exit(1) with open(tempelate_file_path, "r") as f: lines = f.readlines() with open(target_file_path, "w") as f: for line in lines: # 替换字符串 YOUR_WCH_OPENOCD_PATH 和 YOUR_WCH_GCC_PATH f.write(line.replace("YOUR_WCH_OPENOCD_PATH", mrs_openocd_path_str).replace("YOUR_WCH_RISCV_GCC_PATH", mrs_riscv_gcc_path_str)) print("处理完成!")