123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- # -*-coding:utf-8-*-
-
- #===============================================================================
- #@desc:热更资源打包工具
- #===============================================================================
-
- import os
- import difflib
- import hashlib
- import shutil
- import zipfile
- # 主渠道号
- G_UNION_ID = 166
- # 与底包c++层保持一致,原则上每个大类别的包(主包)此处都不一样
- G_ENCRYPT_KEY = "fyLua"
- G_ENCRYPT_SIGN = "sdy5cf0"
- # 热更版本号配置
- G_HALL_VERSION = {"818": 17}
- G_GAME_VERSION = {"001": 1, "824": 7, "901": 6, "902": 6, "903": 5, "904": 5, "905": 4,"906": 3,"907": 4,"908": 4,"909": 4,"910": 4,"911": 2,"912": 5,"913": 1}
- # 版本定义
- G_MIN_VERSION = 1
- G_DIFF_VERSION = 10
- # 目录定义
- G_ROOT_PATH = os.getcwd() + "\\..\\..\\"
- G_RUN_PATH = G_ROOT_PATH + "\\hotupdate_version"
- G_NEW_PATH = G_RUN_PATH + "\\new\\"
- G_RES_PATH = G_NEW_PATH + "\\res"
- G_SRC_PATH = G_NEW_PATH + "\\src"
- G_OUT_PATH = G_RUN_PATH + "\\out\\" + str(G_UNION_ID)
- # 新版本标记
- G_NEW_VERSION = {}
- # --------------------公共方法--------------------
- # 创建文件夹
- def create_dir(dir_name, delete = True):
- if os.path.exists(os.path.dirname(dir_name)):
- if delete:
- shutil.rmtree(dir_name)
- os.makedirs(dir_name)
- else:
- os.makedirs(dir_name)
-
- # 拷贝文件夹
- def copy_files(src_path, dst_path):
- if not os.path.exists(src_path):
- return
- if not os.path.exists(dst_path):
- os.makedirs(dst_path)
- if os.path.exists(src_path):
- shutil.rmtree(dst_path)
-
- #print('copy ' + src_path + " to " + dst_path)
- shutil.copytree(src_path, dst_path)
- # 生成zip包
- def zip_file(dir_path, zip_name):
- zip = zipfile.ZipFile(dir_path + zip_name, "w", zipfile.ZIP_DEFLATED)
- for path, dir_names, file_names in os.walk(dir_path):
- # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
- fpath = path.replace(dir_path, '')
- #print("path >>> " + path)
-
- for file_name in file_names:
- if file_name == zip_name:
- continue
- zip.write(os.path.join(path, file_name), os.path.join(fpath, file_name))
- zip.close()
- # 文件md5
- def hash_md5(file_name):
- if not os.path.isfile(file_name):
- print('**file not exist: ' + file_name)
- return
- hash_obj = hashlib.md5()
- f = open(file_name, 'rb')
- while True:
- b = f.read(8096)
- if not b :
- break
- hash_obj.update(b)
- f.close()
- return hash_obj.hexdigest()
- # 获取文件列表
- def get_file_list(path):
- file_list = []
- for root, dirs, fs in os.walk(path):
- for f in fs:
- f_fullpath = os.path.join(root, f)
- f_relativepath = f_fullpath[len(path):]
- file_list.append(f_relativepath)
- return file_list
- #
- def get_diff_list(src_path, dst_path):
- src_file_list = get_file_list(src_path)
- dst_file_list = get_file_list(dst_path)
-
- set_src = set(src_file_list)
- set_dst = set(dst_file_list)
- set_comm = set_src & set_dst
- set_new = set_dst - set_src
- diff_file_list = []
- for tmp_file in sorted(set_comm):
- src_md5 = hash_md5(src_path + "\\" + tmp_file)
- dst_md5 = hash_md5(dst_path + "\\" + tmp_file)
- if src_md5 != dst_md5:
- print('>>diff file: ' + tmp_file)
- diff_file_list.append(tmp_file)
-
- for tmp_file in sorted(set_new):
- print('>>new file: ' + tmp_file)
- diff_file_list.append(tmp_file)
- return diff_file_list
-
- # --------------------公共方法--------------------
- # 拷贝资源文件
- def copy_resources():
- create_dir(G_NEW_PATH)
- copy_files(G_ROOT_PATH + "\\res", G_RES_PATH)
- copy_files(G_ROOT_PATH + "\\src", G_SRC_PATH)
- # 删除基础部分vscode配置文件
- shutil.rmtree(G_SRC_PATH + "\\.vscode")
- # 模块编译
- def compile_resources():
- # 编译lua文件
- cocos_compile_cmd = "cocos luacompile -s " + G_SRC_PATH + " -d " + G_SRC_PATH + " --disable-compile" + " -e -k " + G_ENCRYPT_KEY + " -b " + G_ENCRYPT_SIGN
- os.system(cocos_compile_cmd)
- # 删除lua文件
- for fold_name, sub_folders, file_names in os.walk(G_SRC_PATH):
- for file_name in file_names:
- if file_name.endswith('.lua'):
- os.remove(os.path.join(fold_name, file_name))
- # 处理res资源
- print(">>skip compile res...")
- # 添加版本文件
- def add_version_file(path, version):
- version_file = path + "\\version.json"
- print(">>add version file:" + version_file)
- with open(version_file,'w')as file:
- file.write("{\"version\":" + str(version) + "}")
-
- # 提取大厅资源
- def split_hall_resources():
- print(">>split hall resources...")
- for key in G_HALL_VERSION:
- hall_version = G_HALL_VERSION[key]
- print(">>dealing " + key + " version = " + str(hall_version))
- new_path = G_RUN_PATH + "\\" + key + "\\" + str(hall_version)
- if os.path.exists(new_path):
- continue
- os.makedirs(new_path)
- G_NEW_VERSION[key] = hall_version
-
- new_res_path = new_path + "\\res"
- new_src_path = new_path + "\\src"
- copy_files(G_RES_PATH, new_res_path)
- copy_files(G_SRC_PATH, new_src_path)
- add_version_file(new_res_path + "\\game_overseas", hall_version)
- # 删除游戏资源
- shutil.rmtree(new_res_path + "\\game_overseas\games")
- game_src_path = new_src_path + "\\app\games"
- for filename in os.listdir(game_src_path):
- if filename.startswith("game") and os.path.isdir(game_src_path + "\\" + filename):
- shutil.rmtree(game_src_path + "\\" + filename)
- print(">>split hall resources finish...")
-
- # 提取游戏资源
- def split_game_resources():
- print(">>split game resources...")
- for key in G_GAME_VERSION:
- game_version = G_GAME_VERSION[key]
- print(">>dealing " + key + " version = " + str(game_version))
- new_path = G_RUN_PATH + "\\" + key + "\\" + str(game_version)
- if os.path.exists(new_path):
- continue
- os.makedirs(new_path)
- G_NEW_VERSION[key] = game_version
- res_path_suffix = "\\game_overseas\games\game" + key
- src_path_suffix = "\\app\games\game" + key
- from_res_path = G_RES_PATH + res_path_suffix
- from_src_path = G_SRC_PATH + src_path_suffix
- to_res_path = new_path + "\\res\\" + res_path_suffix
- to_src_path = new_path + "\\src\\" + src_path_suffix
-
- copy_files(from_res_path, to_res_path)
- copy_files(from_src_path, to_src_path)
- add_version_file(to_res_path, game_version)
- print(">>split game resources finish...")
- # 提取资源
- def split_resources():
- split_hall_resources()
- split_game_resources()
-
- # 生成更新整包
- def gen_full_zip():
- print(">>generate full zips start...")
- for key in G_NEW_VERSION:
- str_version = str(G_NEW_VERSION[key])
- zip_path = G_RUN_PATH + "\\" + key + "\\" + str_version + "\\"
- zip_name = key + "_" + str_version + ".zip"
-
- print(">>zipping " + zip_name)
- zip_file(zip_path, zip_name)
-
- print(">>moving " + zip_name)
- src_zip = zip_path + zip_name
- dst_path = G_OUT_PATH + "\\full\\" + key + "\\"
- create_dir(dst_path)
- shutil.move(src_zip, dst_path)
- print(">>finish " + zip_name)
- print(">>generate full zips finish...")
-
- # 生成更新差分包
- def gen_diff_zip():
- print(">>generate diff zips start...")
-
- for key in G_NEW_VERSION:
- zip_path = G_OUT_PATH + "\\diff\\" + key + "\\"
- create_dir(zip_path)
-
- cur_version = G_NEW_VERSION[key]
- min_version = max(G_MIN_VERSION, cur_version - G_DIFF_VERSION)
- for tmp_version in range(min_version, cur_version):
- print(">>generate diff " + key + " " + str(tmp_version) + " -> " + str(cur_version))
- pre_path = G_RUN_PATH + "\\" + key + "\\" + str(tmp_version) + "\\"
- cur_path = G_RUN_PATH + "\\" + key + "\\" + str(cur_version) + "\\"
- tmp_path = G_RUN_PATH + "\\" + key + "\\" + str(tmp_version) + "_" + str(cur_version) + "\\"
-
- if not os.path.exists(pre_path):
- print(">>generate diff " + key + " no version " + str(tmp_version))
- continue
-
- create_dir(tmp_path)
- diff_list = get_diff_list(pre_path, cur_path)
- for diff_file in diff_list:
- dst_path = os.path.dirname(tmp_path + diff_file)
-
- if not os.path.exists(dst_path):
- os.makedirs(dst_path)
- shutil.copy(cur_path + diff_file, dst_path)
-
- zip_name = key + "_" + str(tmp_version) + "_" + str(cur_version) + ".zip"
- print(">>zipping " + zip_name)
- zip_file(tmp_path, zip_name)
-
- print(">>moving " + zip_name)
- src_zip = tmp_path + zip_name
- shutil.move(src_zip, zip_path)
- print(">>finish " + zip_name)
-
- shutil.rmtree(tmp_path)
- print(">>generate diff zips finish...")
- def gen_zip():
- print(">>find new version:")
- for key in G_NEW_VERSION:
- print(">>" + key + " => " + str(G_NEW_VERSION[key]))
-
- create_dir(G_OUT_PATH)
- gen_full_zip()
- gen_diff_zip()
- def start():
- copy_resources()
- compile_resources()
- split_resources()
-
- gen_zip()
-
- if __name__ == '__main__':
- print(">>hotupdate zip start...")
- start()
- print(">>hotupdate zip finish...")
|