From 5d44acc07350f723732610185b85b6adbca6dbfb Mon Sep 17 00:00:00 2001 From: liutao <821580467@qq.com> Date: Fri, 25 Jul 2025 15:38:57 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=89=93=E5=8C=85=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=89=93=E5=8C=85=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tool/py_tools/PreHandle.py | 135 ++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/Tool/py_tools/PreHandle.py b/Tool/py_tools/PreHandle.py index 97456101a03..845840236ea 100644 --- a/Tool/py_tools/PreHandle.py +++ b/Tool/py_tools/PreHandle.py @@ -1,6 +1,8 @@ # 传入参数 0:SDK_PATH 1:ChannelPath import sys import os +import yaml + def main(): if len(sys.argv) < 3: print("Usage: python PreHandle.py ") @@ -21,8 +23,137 @@ def main(): print(f"SDK Path: {sdk_path}") print(f"Channel Path: {channel_path}") - # 临时返回错误,中断后续流程 - sys.exit(-1) + # 读取Channel Path/ChannelSetting.yaml + channelSettingPath = os.path.join(channel_path, "ChannelSetting.yaml") + if not os.path.exists(channelSettingPath): + print(f"Error: Channel setting file '{channelSettingPath}' does not exist.") + sys.exit(4) # Channel设置文件不存在时返回错误码4 + with open(channelSettingPath, 'r', encoding='utf-8') as file: + channel_setting = file.read() + print(f"Channel Setting: {channel_setting}") + # 使用yaml解析 + try: + channel_data = yaml.safe_load(channel_setting) + print(f"Parsed Channel Data: {channel_data}") + except yaml.YAMLError as e: + print(f"Error parsing YAML: {e}") + sys.exit(5) + + # 检查Packages是否存在 + # 这里假设Packages是一个列表 内容是 packageName@version + # 例如: ["package1@1.0.0", "package2@latest"] + packageList = channel_data.get("Packages", []) + if not packageList: + print("Error: PackageList is empty or not found in ChannelSetting.yaml.") + sys.exit(6) + for package in packageList: + HandlePackage(package, sdk_path, channel_path) + + # 处理完成,正常退出 + print("All packages processed successfully.") + sys.exit(0) # 改为成功退出 + +def HandlePackage(packageDefineName, sdk_path, channel_path): + # 处理包定义名称 + if '@' not in packageDefineName: + print(f"Error: Invalid package format '{packageDefineName}'. Expected format 'packageName@version'.") + sys.exit(8) # 返回错误码8 + packageName, version = packageDefineName.split('@', 1) + print(f"Package Name: {packageName}, Version: {version}") + # 检查sdk_path/packageName 是否存在 + package_path = os.path.join(sdk_path, packageName) + if not os.path.exists(package_path): + print(f"Error: Package path '{package_path}' does not exist.") + sys.exit(7) # 包路径不存在时返回错误码7 + + # 如果version是latest, 则读取package_path/latest.txt + if version == "latest": + latest_file_path = os.path.join(package_path, "latest.txt") + if not os.path.exists(latest_file_path): + print(f"Error: Latest file '{latest_file_path}' does not exist.") + sys.exit(9) + with open(latest_file_path, 'r') as file: + latest_version = file.read().strip() + print(f"Latest Version: {latest_version}") + version = latest_version # 添加这行:更新version变量 + + #判断package_path/version文件夹是否存在 + version_path = os.path.join(package_path, version) + if not os.path.exists(version_path): + print(f"Error: Version path '{version_path}' does not exist.") + sys.exit(10) + # 把version_path/Assets下所有的文件和文件夹复制到channel_path/Assets + srcAssetPath = os.path.join(version_path, "Assets") + targetAssetPath = os.path.join(channel_path, "Assets") + CopyAssets(srcAssetPath, targetAssetPath) + ModifyDefineList(version_path) + +def ModifyDefineList(versionPath): + # 读取versionPath/OtherConfig.yaml + other_config_path = os.path.join(versionPath, "OtherConfig.yaml") + if not os.path.exists(other_config_path): + print(f"Info: OtherConfig file '{other_config_path}' does not exist.") + return + with open(other_config_path, 'r', encoding='utf-8') as file: + other_config = yaml.safe_load(file) + print(f"Other Config: {other_config}") + # 检查是否有DefineList + define_list = other_config.get("Define_list", []) + if not define_list: + print("Info: Define_list is empty or not found in OtherConfig.yaml.") + return + # 修改channel_path/ChannelSetting.yaml中的DefineList + channel_path = sys.argv[2] + channel_setting_path = os.path.join(channel_path, "ChannelSetting.yaml") + if not os.path.exists(channel_setting_path): + print(f"Error: Channel setting file '{channel_setting_path}' does not exist.") + sys.exit(11) + with open(channel_setting_path, 'r', encoding='utf-8') as file: + channel_setting = yaml.safe_load(file) + print(f"Channel Setting Before Modification: {channel_setting}") + # 修改DefineList + if "Define_list" not in channel_setting: + channel_setting["Define_list"] = [] + channel_setting["Define_list"].extend(define_list) + with open(channel_setting_path, 'w', encoding='utf-8') as file: + yaml.dump(channel_setting, file) + print(f"Channel Setting After Modification: {channel_setting}") + +def CopyAssets(srcAssetPath, targetAssetPath): + # 如果srcAssetPath不存在,则返回 + if not os.path.exists(srcAssetPath): + print(f"Info: Source asset path '{srcAssetPath}' does not exist.") + return + # 如果targetAssetPath不存在,则创建 + if not os.path.exists(targetAssetPath): + try: + os.makedirs(targetAssetPath) + print(f"Created target asset path: {targetAssetPath}") + except OSError as e: + print(f"Error: Failed to create directory '{targetAssetPath}': {e}") + sys.exit(12) + + # 遍历srcAssetPath下的所有文件和文件夹 + try: + for item in os.listdir(srcAssetPath): + src_item_path = os.path.join(srcAssetPath, item) + target_item_path = os.path.join(targetAssetPath, item) + # 如果是文件,则复制 + if os.path.isfile(src_item_path): + try: + with open(src_item_path, 'rb') as src_file: + with open(target_item_path, 'wb') as target_file: + target_file.write(src_file.read()) + print(f"Copied file: {src_item_path} to {target_item_path}") + except IOError as e: + print(f"Error: Failed to copy file '{src_item_path}': {e}") + sys.exit(13) + # 如果是文件夹,则递归调用CopyAssets + elif os.path.isdir(src_item_path): + CopyAssets(src_item_path, target_item_path) + except OSError as e: + print(f"Error: Failed to list directory '{srcAssetPath}': {e}") + sys.exit(14) if __name__ == "__main__": main() \ No newline at end of file