# encoding: utf-8 import os import sys import stat from pathlib import Path import time import re import socket import task import tools import log_tail class CIProcess(): def __init__(self, unity_exe_path, buld_player_name, build_type, p4path, local_name, local_cient_name, upload_name_prex, upload_path, notice_webhook, build_version, suffix): self.unity_exe_path = unity_exe_path self.buld_player_name = buld_player_name self.build_type = build_type self.unity_method_name = 'H3DBuild.ContentPipeline.BuildPlayerCmd' self.unity_log_path = os.getcwd() + "/build-ios-output/archive_log.txt" self.unity_proj_path = os.getcwd() + "/.." self.build_path = self.unity_proj_path + "/XcodeProject" self.build_ipa_py = "ipaExportor.py" self.p4path = p4path self.local_name = local_name self.local_cient_name = local_cient_name self.upload_name_prex = upload_name_prex self.upload_path = upload_path self.notice_webhook = notice_webhook self.build_version = build_version self.suffix = suffix # 编译unity生成xcode工程 def BuildUnity(self): os.system("rm -Rf %s" % "../Library/ScriptAssemblies") os.system("rm -Rf %s" % "../H3DLog") local_client_name = self.local_cient_name if 0 != len(local_client_name) : export_file = os.getcwd() + "/../../" + local_client_name + ".ipa" if Path(export_file).is_file(): print("%s exist, delete" % export_file) os.remove(export_file) # 结束正在运行的unity进程; TODO: 暂屏蔽 # os.system('pkill -9 Unity') # os.system('pkill -9 Xcode') # os.system("rm -Rf %s" % self.unity_log_path) # f=open(self.unity_log_path,'wb+') os.chdir("../..") run_cmd = '"{}" -quit -batchmode -logFile -projectPath {} -executeMethod {} -buildName {} -buildTarget iOS'\ .format(self.unity_exe_path, self.unity_proj_path, self.unity_method_name, self.buld_player_name) print(run_cmd) sys.stdout.flush() ret = os.system(run_cmd) # time.sleep(1) # content = f.read() # content = content.decode("utf8","ignore") # print(content) # f.close() # if re.search("Build Failed", content): # print("build failed!") # sys.stdout.flush() # ret = -1 if ret < 0: ret = -ret return ret # 编译unity生成ipa包 def BuildXcode(self): if os.path.exists(self.build_path) is False: print("build_path not exist, %s" % self.build_path) return -1 print(self.build_path) os.chdir(self.build_path) print(os.getcwd()) if os.path.exists(self.build_ipa_py) is False: print("build_ipa_py not exist, %s" % self.build_ipa_py) return -2 if (self.build_type != "Debug") and (self.build_type != "Release"): print("build_type not Debug or Relaeae, %s" % self.build_type) return -3 os.system("chmod +x ./%s" % self.build_ipa_py) return os.system("./%s %s %s" % (self.build_ipa_py, self.build_type, self.local_cient_name)) # 清理工作目录 def CleanDir(self): os.system("rm -Rf %s" % self.build_path) cache="~/Library/Developer/Xcode/DerivedData/Unity-iPhone-*" os.system("rm -Rf %s" % cache) def Upload(self, now, p4_number, p4_author): if False == Path(self.local_name).is_file(): print("local_name is not exist, %s" % self.local_name) sys.stdout.flush() return -1 upload_name = self.upload_name_prex + "_" + self.build_version + "_" + now + "_" + "p4:" + p4_number + "_" + p4_author + "." + self.suffix tools.UploadNexus(self.upload_path, self.local_name, upload_name) hint_dSYM = False if self.build_type == "Release": dSYM_local_name = "Unity-iPhone.dSYM.zip" if Path(dSYM_local_name).is_file(): dSYM_upload_name = self.upload_name_prex + "_" + self.build_version + "_" + now + "_" + "p4:" + p4_number + "_" + p4_author + ".dSYM.zip" tools.UploadNexus(self.upload_path, dSYM_local_name, dSYM_upload_name) hint_dSYM = True else: print("dSYM is not exist, upload dSYM failed!") tools.NoticeUpload(self.notice_webhook, self.upload_path, upload_name, p4_number, p4_author, '.ipa', hint_dSYM) return 0 def Run(self): now = tools.GetTime() p4_number, p4_author = tools.GetP4Info(self.p4path) self.CleanDir() ret = self.BuildUnity() if (0 != ret): print("BuildUnity fail, ret = ", ret) sys.stdout.flush() return ret ret = self.BuildXcode() if (0 != ret): print("BuildXcode fail, ret = ", ret) return ret ret = self.Upload(now, p4_number, p4_author) if (0 != ret): print("Upload fail, ret = ", ret) return ret return ret if __name__ == '__main__': if (12 > len(sys.argv)): print("args err! not unity_exe_path and buld_player_name") os._exit(-1) unity_exe_path = sys.argv[1] buld_player_name = sys.argv[2] build_type = sys.argv[3] p4path = sys.argv[4] local_name = sys.argv[5] local_cient_name = sys.argv[6] upload_name_prex = sys.argv[7] upload_path = sys.argv[8] notice_webhook = sys.argv[9] build_version = sys.argv[10] suffix = sys.argv[11] print("unity_exe_path : %s" % unity_exe_path) print("buld_player_name : %s" % buld_player_name) print("build_type : %s" % build_type) print("p4path : %s" % p4path) print("local_name : %s" % local_name) print("local_cient_name : %s" % local_cient_name) print("upload_name_prex : %s" % upload_name_prex) print("upload_path : %s" % upload_path) print("notice_webhook : %s" % notice_webhook) print("build_version : %s" % build_version) print("suffix : %s" % suffix) sys.stdout.flush() if False == Path(unity_exe_path).is_file(): print("unity_exe_path is not exist, %s" % unity_exe_path) os._exit(-2) process = CIProcess(unity_exe_path, buld_player_name, build_type, p4path, local_name, local_cient_name, upload_name_prex, upload_path, notice_webhook, build_version, suffix) ret = process.Run() print("ret:%d" % ret) sys.stdout.flush() os._exit(ret)