NLDClient-yudde/Tool/py_tools/PreHandle.py

188 lines
8.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 传入参数 0:SDK_PATH 1:ChannelPath
import sys
import os
import yaml
def main():
if len(sys.argv) < 3:
print("Usage: python PreHandle.py <SDK_PATH> <ChannelPath>")
sys.exit(1) # 参数不足时返回错误码1
sdk_path = sys.argv[1]
channel_path = sys.argv[2]
if not os.path.exists(sdk_path):
print(f"Error: SDK path '{sdk_path}' does not exist.")
sys.exit(2) # SDK路径不存在时返回错误码2
if not os.path.exists(channel_path):
print(f"Error: Channel path '{channel_path}' does not exist.")
sys.exit(3) # Channel路径不存在时返回错误码3
# 这里可以添加更多的处理逻辑
print(f"SDK Path: {sdk_path}")
print(f"Channel Path: {channel_path}")
# 读取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("Info: PackageList is empty or not found in ChannelSetting.yaml.")
# 如果PackageList为空,则不处理
return
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', encoding='utf-8') 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, channel_path)
def ModifyDefineList(versionPath, channel_path):
# 读取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
try:
with open(other_config_path, 'r', encoding='utf-8') as file:
other_config = yaml.safe_load(file)
print(f"Other Config: {other_config}")
except (IOError, yaml.YAMLError) as e:
print(f"Error reading OtherConfig file '{other_config_path}': {e}")
return
# 检查是否有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_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)
try:
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}")
except (IOError, yaml.YAMLError) as e:
print(f"Error reading channel setting file '{channel_setting_path}': {e}")
sys.exit(11)
# 修改DefineList
if "Define_list" not in channel_setting:
channel_setting["Define_list"] = []
channel_setting["Define_list"].extend(define_list)
try:
with open(channel_setting_path, 'w', encoding='utf-8') as file:
yaml.dump(channel_setting, file, allow_unicode=True)
print(f"Channel Setting After Modification: {channel_setting}")
except IOError as e:
print(f"Error writing channel setting file '{channel_setting_path}': {e}")
sys.exit(15)
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)
print(f"Start copying assets from '{srcAssetPath}' to '{targetAssetPath}'")
# 遍历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):
# 检查目标文件是否已存在,如果存在则输出警告
if os.path.exists(target_item_path):
print(f"Warning: Target file '{target_item_path}' already exists and will be overwritten.")
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):
# 检查目标目录是否已存在,如果存在则输出警告
if os.path.exists(target_item_path):
print(f"Warning: Target directory '{target_item_path}' already exists, merging contents.")
CopyAssets(src_item_path, target_item_path)
except OSError as e:
print(f"Error: Failed to list directory '{srcAssetPath}': {e}")
sys.exit(14)
# print(f"Assets copied successfully from '{srcAssetPath}' to '{targetAssetPath}'")
if __name__ == "__main__":
main()