123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- # -*-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": 6}
- G_GAME_VERSION = {"001": 1, "824": 4, "901": 2, "902": 2, "903": 1, "904": 1}
- # 版本定义
- G_MIN_VERSION = 1
- G_DIFF_VERSION = 5
- # 目录定义
- 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)
- # --------------------公共方法--------------------
- # 创建文件夹
- 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 gen_full_zip(version_list):
- for key in version_list:
- dst_path = G_OUT_PATH + "\\full\\" + key + "\\"
- create_dir(dst_path)
-
- version = version_list[key]
- for tmp_version in range(G_MIN_VERSION, version + 1):
- str_version = str(tmp_version)
- zip_path = G_RUN_PATH + "\\" + key + "\\" + str_version + "\\"
- if not os.path.exists(zip_path):
- print(">>generate full zips " + key + " no version " + str_version)
- continue
-
- 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
-
- shutil.move(src_zip, dst_path)
- print(">>finish " + zip_name)
- # 生成大厅更新整包
- def gen_hall_full_zip():
- print(">>generate hall full zips start...")
- gen_full_zip(G_HALL_VERSION)
- print(">>generate hall full zips finish...")
-
- # 生成游戏更新整包
- def gen_game_full_zip():
- print(">>generate game full zips start...")
- gen_full_zip(G_GAME_VERSION)
- print(">>generate game full zips finish...")
- # 生成更新差分包
- def gen_diff_zip(version_list):
- for key in version_list:
- dst_path = G_OUT_PATH + "\\diff\\" + key + "\\"
- create_dir(dst_path)
-
- max_version = version_list[key]
- for cur_version in range(max_version, G_MIN_VERSION, -1):
- 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:
- file_dir = os.path.dirname(tmp_path + diff_file)
-
- if not os.path.exists(file_dir):
- os.makedirs(file_dir)
- shutil.copy(cur_path + diff_file, file_dir)
-
- 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, dst_path)
- print(">>finish " + zip_name)
-
- shutil.rmtree(tmp_path)
- # 生成大厅更新差分包
- def gen_hall_diff_zip():
- print(">>generate hall diff zips start...")
- gen_diff_zip(G_HALL_VERSION)
- print(">>generate hall diff zips finish...")
- # 生成游戏更新差分包
- def gen_game_diff_zip():
- print(">>generate game diff zips start...")
- gen_diff_zip(G_GAME_VERSION)
- print(">>generate game diff zips finish...")
- def gen_zip():
- gen_hall_full_zip()
- gen_game_full_zip()
- gen_hall_diff_zip()
- gen_game_diff_zip()
- def start():
- gen_zip()
-
- if __name__ == '__main__':
- print(">>hotupdate zip start...")
- start()
- print(">>hotupdate zip finish...")
|