hotupdate_script_all.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # -*-coding:utf-8-*-
  2. #===============================================================================
  3. #@desc:热更资源打包工具
  4. #===============================================================================
  5. import os
  6. import difflib
  7. import hashlib
  8. import shutil
  9. import zipfile
  10. # 主渠道号
  11. G_UNION_ID = 166
  12. # 与底包c++层保持一致,原则上每个大类别的包(主包)此处都不一样
  13. G_ENCRYPT_KEY = "fyLua"
  14. G_ENCRYPT_SIGN = "sdy5cf0"
  15. # 热更版本号配置
  16. G_HALL_VERSION = {"818": 6}
  17. G_GAME_VERSION = {"001": 1, "824": 4, "901": 2, "902": 2, "903": 1, "904": 1}
  18. # 版本定义
  19. G_MIN_VERSION = 1
  20. G_DIFF_VERSION = 5
  21. # 目录定义
  22. G_ROOT_PATH = os.getcwd() + "\\..\\..\\"
  23. G_RUN_PATH = G_ROOT_PATH + "\\hotupdate_version"
  24. G_NEW_PATH = G_RUN_PATH + "\\new\\"
  25. G_RES_PATH = G_NEW_PATH + "\\res"
  26. G_SRC_PATH = G_NEW_PATH + "\\src"
  27. G_OUT_PATH = G_RUN_PATH + "\\out\\" + str(G_UNION_ID)
  28. # --------------------公共方法--------------------
  29. # 创建文件夹
  30. def create_dir(dir_name, delete = True):
  31. if os.path.exists(os.path.dirname(dir_name)):
  32. if delete:
  33. shutil.rmtree(dir_name)
  34. os.makedirs(dir_name)
  35. else:
  36. os.makedirs(dir_name)
  37. # 拷贝文件夹
  38. def copy_files(src_path, dst_path):
  39. if not os.path.exists(src_path):
  40. return
  41. if not os.path.exists(dst_path):
  42. os.makedirs(dst_path)
  43. if os.path.exists(src_path):
  44. shutil.rmtree(dst_path)
  45. #print('copy ' + src_path + " to " + dst_path)
  46. shutil.copytree(src_path, dst_path)
  47. # 生成zip包
  48. def zip_file(dir_path, zip_name):
  49. zip = zipfile.ZipFile(dir_path + zip_name, "w", zipfile.ZIP_DEFLATED)
  50. for path, dir_names, file_names in os.walk(dir_path):
  51. # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
  52. fpath = path.replace(dir_path, '')
  53. #print("path >>> " + path)
  54. for file_name in file_names:
  55. if file_name == zip_name:
  56. continue
  57. zip.write(os.path.join(path, file_name), os.path.join(fpath, file_name))
  58. zip.close()
  59. # 文件md5
  60. def hash_md5(file_name):
  61. if not os.path.isfile(file_name):
  62. print('**file not exist: ' + file_name)
  63. return
  64. hash_obj = hashlib.md5()
  65. f = open(file_name, 'rb')
  66. while True:
  67. b = f.read(8096)
  68. if not b :
  69. break
  70. hash_obj.update(b)
  71. f.close()
  72. return hash_obj.hexdigest()
  73. # 获取文件列表
  74. def get_file_list(path):
  75. file_list = []
  76. for root, dirs, fs in os.walk(path):
  77. for f in fs:
  78. f_fullpath = os.path.join(root, f)
  79. f_relativepath = f_fullpath[len(path):]
  80. file_list.append(f_relativepath)
  81. return file_list
  82. #
  83. def get_diff_list(src_path, dst_path):
  84. src_file_list = get_file_list(src_path)
  85. dst_file_list = get_file_list(dst_path)
  86. set_src = set(src_file_list)
  87. set_dst = set(dst_file_list)
  88. set_comm = set_src & set_dst
  89. set_new = set_dst - set_src
  90. diff_file_list = []
  91. for tmp_file in sorted(set_comm):
  92. src_md5 = hash_md5(src_path + "\\" + tmp_file)
  93. dst_md5 = hash_md5(dst_path + "\\" + tmp_file)
  94. if src_md5 != dst_md5:
  95. print('>>diff file: ' + tmp_file)
  96. diff_file_list.append(tmp_file)
  97. for tmp_file in sorted(set_new):
  98. print('>>new file: ' + tmp_file)
  99. diff_file_list.append(tmp_file)
  100. return diff_file_list
  101. # --------------------公共方法--------------------
  102. # 生成更新整包
  103. def gen_full_zip(version_list):
  104. for key in version_list:
  105. dst_path = G_OUT_PATH + "\\full\\" + key + "\\"
  106. create_dir(dst_path)
  107. version = version_list[key]
  108. for tmp_version in range(G_MIN_VERSION, version + 1):
  109. str_version = str(tmp_version)
  110. zip_path = G_RUN_PATH + "\\" + key + "\\" + str_version + "\\"
  111. if not os.path.exists(zip_path):
  112. print(">>generate full zips " + key + " no version " + str_version)
  113. continue
  114. zip_name = key + "_" + str_version + ".zip"
  115. print(">>zipping " + zip_name)
  116. zip_file(zip_path, zip_name)
  117. print(">>moving " + zip_name)
  118. src_zip = zip_path + zip_name
  119. shutil.move(src_zip, dst_path)
  120. print(">>finish " + zip_name)
  121. # 生成大厅更新整包
  122. def gen_hall_full_zip():
  123. print(">>generate hall full zips start...")
  124. gen_full_zip(G_HALL_VERSION)
  125. print(">>generate hall full zips finish...")
  126. # 生成游戏更新整包
  127. def gen_game_full_zip():
  128. print(">>generate game full zips start...")
  129. gen_full_zip(G_GAME_VERSION)
  130. print(">>generate game full zips finish...")
  131. # 生成更新差分包
  132. def gen_diff_zip(version_list):
  133. for key in version_list:
  134. dst_path = G_OUT_PATH + "\\diff\\" + key + "\\"
  135. create_dir(dst_path)
  136. max_version = version_list[key]
  137. for cur_version in range(max_version, G_MIN_VERSION, -1):
  138. min_version = max(G_MIN_VERSION, cur_version - G_DIFF_VERSION)
  139. for tmp_version in range(min_version, cur_version):
  140. print(">>generate diff " + key + " " + str(tmp_version) + " -> " + str(cur_version))
  141. pre_path = G_RUN_PATH + "\\" + key + "\\" + str(tmp_version) + "\\"
  142. cur_path = G_RUN_PATH + "\\" + key + "\\" + str(cur_version) + "\\"
  143. tmp_path = G_RUN_PATH + "\\" + key + "\\" + str(tmp_version) + "_" + str(cur_version) + "\\"
  144. if not os.path.exists(pre_path):
  145. print(">>generate diff " + key + " no version " + str(tmp_version))
  146. continue
  147. create_dir(tmp_path)
  148. diff_list = get_diff_list(pre_path, cur_path)
  149. for diff_file in diff_list:
  150. file_dir = os.path.dirname(tmp_path + diff_file)
  151. if not os.path.exists(file_dir):
  152. os.makedirs(file_dir)
  153. shutil.copy(cur_path + diff_file, file_dir)
  154. zip_name = key + "_" + str(tmp_version) + "_" + str(cur_version) + ".zip"
  155. print(">>zipping " + zip_name)
  156. zip_file(tmp_path, zip_name)
  157. print(">>moving " + zip_name)
  158. src_zip = tmp_path + zip_name
  159. shutil.move(src_zip, dst_path)
  160. print(">>finish " + zip_name)
  161. shutil.rmtree(tmp_path)
  162. # 生成大厅更新差分包
  163. def gen_hall_diff_zip():
  164. print(">>generate hall diff zips start...")
  165. gen_diff_zip(G_HALL_VERSION)
  166. print(">>generate hall diff zips finish...")
  167. # 生成游戏更新差分包
  168. def gen_game_diff_zip():
  169. print(">>generate game diff zips start...")
  170. gen_diff_zip(G_GAME_VERSION)
  171. print(">>generate game diff zips finish...")
  172. def gen_zip():
  173. gen_hall_full_zip()
  174. gen_game_full_zip()
  175. gen_hall_diff_zip()
  176. gen_game_diff_zip()
  177. def start():
  178. gen_zip()
  179. if __name__ == '__main__':
  180. print(">>hotupdate zip start...")
  181. start()
  182. print(">>hotupdate zip finish...")