auto_build_launcher/scripts/project_init.py

139 lines
6.3 KiB
Python

import os
import time
from git import Repo, RemoteProgress
from utils import FileUtils
from utils.logger_utils import app_logger
from .context import Context
from .task import Task
def progress(op_code, cur_count, max_count=None, message=''):
if op_code == RemoteProgress.END:
print()
print(f"操作: {op_code}, 进度: {cur_count}/{max_count}, 消息: {message}")
def check_config_exists(*file_paths):
# game_config/com.diy.emoticon.free.zcaqf.game.launcher/com.diy.emoticon.free.zcaqf.game.launcher_android.zip
# game_config/com.emoticon.diy.znfav.launcher.free/com.emoticon.diy.znfav.launcher.free_android.zip
# 遍历检查每个文件
if file_paths is None:
file_paths = []
for file_path in file_paths:
if not os.path.exists(file_path):
# 抛出异常并说明缺失的文件路径
raise FileNotFoundError(f"配置文件不存在: {file_path}")
class ProjectInit(Task):
def __init__(self, context: Context):
super().__init__(context)
self.context.temp_project_path = self.context.project_original_path.replace("original",
"V1_" + self.context.package_name.replace(
".", "_"))
self.context.temp_project_config_path = self.context.temp_project_path + "_config"
if not os.path.exists(self.context.temp_project_config_path):
os.makedirs(self.context.temp_project_config_path)
config_md5 = context.get_cache_config_from_key("config", "")
keystore_md5 = context.get_cache_config_from_key("keystore", "")
res_img_md5 = context.get_cache_config_from_key("res_img", "")
res_icon_md5 = context.get_cache_config_from_key("res_icon", "")
res_unity_md5 = context.get_cache_config_from_key("res_unity", "")
app_logger().debug(f"temp project res md5 , "
f"config : {config_md5} , "
f"keystore : {keystore_md5} , "
f"res_img : {res_img_md5} , "
f"res_icon : {res_icon_md5} , "
f"res_unity : {res_unity_md5}")
config_root_path = os.path.join("game_config", self.context.package_name)
self.context.config_path = os.path.join(config_root_path, f"{self.context.package_name}_android.zip")
self.context.keystore_path = ""
for i in os.listdir(config_root_path):
if i.endswith(".keystore"):
self.context.keystore_path = os.path.join(config_root_path, i)
self.context.res_img_path = os.path.join(config_root_path, "drawable-xxhdpi.zip")
self.context.res_icon_path = os.path.join(config_root_path, "icon.zip")
self.context.res_unity_path = os.path.join(config_root_path, "unityLibrary.zip")
check_config_exists(self.context.config_path,
self.context.keystore_path,
self.context.res_img_path,
self.context.res_icon_path,
self.context.res_unity_path)
self.context.config_config_md5 = FileUtils.get_md5(self.context.config_path)
self.context.config_keystore_md5 = FileUtils.get_md5(self.context.keystore_path)
self.context.config_res_img_md5 = FileUtils.get_md5(self.context.res_img_path)
self.context.config_res_icon_md5 = FileUtils.get_md5(self.context.res_icon_path)
self.context.config_res_unity_md5 = FileUtils.get_md5(self.context.res_unity_path)
self.context.update_config = config_md5 != self.context.config_config_md5
self.context.update_keystore = keystore_md5 != self.context.config_keystore_md5
self.context.update_res_img = res_img_md5 != self.context.config_res_img_md5
self.context.update_res_icon = res_icon_md5 != self.context.config_res_icon_md5
self.context.update_res_unity = res_unity_md5 != self.context.config_res_unity_md5
def execute(self):
try:
repo = Repo(self.context.project_original_path)
except Exception:
repo = Repo.clone_from(self.context.repo_url, self.context.project_original_path,
recursive=True,
progress=progress)
# for submodule in repo.submodules:
# print(f"子模块 '{submodule.name}' 路径: {submodule.path}")
# # print(f"Commit ID: {submodule.module().head.commit}")
if self.context.repo_commit:
pass
elif self.context.repo_branch:
# 克隆仓库
branch_name = self.context.repo_branch
remote_name = "origin" # 远程仓库默认名称
repo.git.fetch(remote_name)
if repo.active_branch.name != branch_name:
# 检查本地是否已存在该分支
if branch_name in repo.heads:
# 本地分支已存在,直接切换
repo.heads[branch_name].checkout()
else:
# 2. 创建本地分支并跟踪远程分支
remote_branch_ref = f"{remote_name}/{branch_name}"
local_branch = repo.create_head(branch_name, remote_branch_ref) # 创建本地分支指向远程
local_branch.set_tracking_branch(repo.remotes[remote_name].refs[branch_name]) # 设置跟踪
local_branch.checkout() # 切换到该分支
# 拉取最新代码
repo.remotes.origin.pull()
self.context.local_repo_branch = repo.active_branch.name
self.context.local_repo_commit = repo.head.commit.hexsha[:10]
print("当前分支:" + repo.active_branch.name, self.context.local_repo_commit)
pass
else:
raise Exception(f"No commit to {self.context.repo_commit}")
repo.git.submodule('update', '--init', '--recursive')
for submodule in repo.submodules:
print(submodule.url)
print(submodule.name)
print(submodule.hexsha)
print(submodule.path)
sub_repo = submodule.module()
sub_repo.git.reset("--hard", submodule.hexsha)
print(f"Reset {submodule.name} to {submodule.hexsha[:7]}")