package_web.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import importlib
  2. import json
  3. import os.path
  4. import sys
  5. import common_utils
  6. import file_utils
  7. import package_utils
  8. sdk_mapping = {
  9. 'jmsdk': 'jm',
  10. 'qingshi': 'qingshi'
  11. }
  12. script_mapping = {
  13. 'jmsdk': 'jm',
  14. 'qingshi': 'qingshi'
  15. }
  16. def package_test(package_config):
  17. # 打包配置的路径
  18. json_text = file_utils.read_file(package_config)
  19. config = json.loads(json_text)
  20. # 重构sdk判断
  21. if 'refactorSdk' in config and toBoolean(config['refactorSdk']):
  22. print('重构sdk特殊处理...')
  23. config['sdk'] = 'qingshi'
  24. properties = config['properties']
  25. if 'version' in properties:
  26. properties['QS_PACKAGE_VERSION'] = properties.pop('version')
  27. if 'agent' in properties:
  28. properties['QS_CAMPAIGN_ID'] = properties.pop('agent')
  29. if 'appid' in properties:
  30. properties['QS_APP_ID'] = properties.pop('appid')
  31. if 'appkey' in properties:
  32. properties['QS_APP_KEY'] = properties.pop('appkey')
  33. if 'host' in properties:
  34. properties['QS_ONLINE_ENV'] = properties.pop('host')
  35. if 'testGcpCode' in config:
  36. properties['QS_GCP_ID'] = config['testGcpCode']
  37. print('*************config*****************')
  38. print(json.JSONEncoder().encode(config))
  39. print('************************************')
  40. sdk = get_mapping_sdk(config)
  41. package(config, sdk)
  42. def packageWeb():
  43. if len(sys.argv) < 2:
  44. print('argument is missing')
  45. return 1
  46. # 打包配置的路径
  47. packageConfig = sys.argv[1]
  48. json_text = file_utils.read_file(packageConfig)
  49. config = json.loads(json_text)
  50. # 重构sdk判断
  51. if 'refactorSdk' in config and toBoolean(config['refactorSdk']):
  52. print('重构sdk特殊处理...')
  53. config['sdk'] = 'qingshi'
  54. properties = config['properties']
  55. if 'version' in properties:
  56. properties['QS_PACKAGE_VERSION'] = properties.pop('version')
  57. if 'agent' in properties:
  58. properties['QS_CAMPAIGN_ID'] = properties.pop('agent')
  59. if 'appid' in properties:
  60. properties['QS_APP_ID'] = properties.pop('appid')
  61. if 'appkey' in properties:
  62. properties['QS_APP_KEY'] = properties.pop('appkey')
  63. if 'host' in properties:
  64. properties['QS_ONLINE_ENV'] = properties.pop('host')
  65. if 'testGcpCode' in config:
  66. properties['QS_GCP_ID'] = config['testGcpCode']
  67. print('*************config*****************')
  68. print(json.JSONEncoder().encode(config))
  69. print('************************************')
  70. sdk = get_mapping_sdk(config)
  71. package(config, sdk)
  72. def package(config, sdk):
  73. print('use script 1st')
  74. jsonConfig = {}
  75. game = config['app']
  76. subChannel = None
  77. if 'subChannel' in config:
  78. subChannel = config['subChannel']
  79. jsonConfig['subChannel'] = config['subChannel']
  80. config['random'] = common_utils.get_random_number(6)
  81. outName = config['random']
  82. channelPath = file_utils.getChannelPath(game, outName, sdk)
  83. subChannelPath = os.path.join(channelPath, subChannel)
  84. print('打包开始,删除缓存目录 %s ' % subChannelPath)
  85. file_utils.delete_folder(subChannelPath)
  86. # 必须参数
  87. jsonConfig['packageName'] = config['packageName']
  88. jsonConfig['name'] = config['name']
  89. jsonConfig['random'] = config['random']
  90. # 可选参数
  91. if 'outName' in config:
  92. jsonConfig['outName'] = config['outName']
  93. if 'outPath' in config:
  94. jsonConfig['outPath'] = config['outPath']
  95. if 'changeIcon' in config:
  96. jsonConfig['changeIcon'] = toBoolean(config['changeIcon'])
  97. if 'addLauncher' in config:
  98. jsonConfig['addLauncher'] = toBoolean(config['addLauncher'])
  99. if 'versionCode' in config:
  100. jsonConfig['versionCode'] = config['versionCode']
  101. if 'versionName' in config:
  102. jsonConfig['versionName'] = config['versionName']
  103. if 'targetSdkVersion' in config:
  104. jsonConfig['targetSdkVersion'] = config['targetSdkVersion']
  105. if 'v2disable' in config:
  106. jsonConfig['v2disable'] = toBoolean(config['v2disable'])
  107. if 'aapt2disable' in config:
  108. jsonConfig['aapt2disable'] = toBoolean(config['aapt2disable'])
  109. if 'refactorSdk' in config and toBoolean(config['refactorSdk']):
  110. jsonConfig['refactorSdk'] = toBoolean(config['refactorSdk'])
  111. # sdk相关参数
  112. if 'properties' in config:
  113. jsonConfig['properties'] = config['properties']
  114. jsonConfig['properties']['isDebugMode'] = 'false'
  115. # jsonConfig['logSdk'] = ['jrtt', 'gdt', 'ks', 'uc']
  116. # 广点通参数
  117. if 'gdt_params' in config:
  118. jsonConfig['logSdk'] = ['gdt']
  119. gdt = {'gdt': config['gdt_params']}
  120. if 'configData' in jsonConfig:
  121. configData = jsonConfig['configData']
  122. configData['channel_sdk_list'] = gdt
  123. else:
  124. jsonConfig['configData'] = {
  125. 'channel_sdk_list': gdt
  126. }
  127. # uc广告参数
  128. if 'uc_params' in config:
  129. jsonConfig['logSdk'] = ['uc']
  130. uc = {'uc': config['uc_params']}
  131. if 'configData' in jsonConfig:
  132. configData = jsonConfig['configData']
  133. configData['channel_sdk_list'] = uc
  134. else:
  135. jsonConfig['configData'] = {
  136. 'channel_sdk_list': uc
  137. }
  138. # baidu广告参数
  139. if 'bd_params' in config:
  140. jsonConfig['logSdk'] = ['bd']
  141. bd = {'bd': config['bd_params']}
  142. if 'configData' in jsonConfig:
  143. configData = jsonConfig['configData']
  144. configData['channel_sdk_list'] = bd
  145. else:
  146. jsonConfig['configData'] = {
  147. 'channel_sdk_list': bd
  148. }
  149. if 'tt_params' in config:
  150. jsonConfig['logSdk'] = ['jrtt']
  151. jrtt = {'jrtt': config['tt_params']}
  152. if 'configData' in jsonConfig:
  153. configData = jsonConfig['configData']
  154. configData['channel_sdk_list'] = jrtt
  155. else:
  156. jsonConfig['configData'] = {
  157. 'channel_sdk_list': jrtt
  158. }
  159. if 'ks_params' in config:
  160. jsonConfig['logSdk'] = ['ks']
  161. # 获取sdk相关配置
  162. get_sdk_config(sdk, jsonConfig, config)
  163. # 生成配置文件
  164. create_or_update_config_file(game, sdk, jsonConfig)
  165. # 拷贝资源
  166. copy_res(game, sdk, subChannel, config)
  167. # 打包
  168. package_utils.packConsole(game, sdk, config, subChannel)
  169. def toBoolean(bool_str):
  170. if type(bool_str) == bool:
  171. return bool_str
  172. if bool_str == 'true':
  173. return True
  174. return False
  175. def get_sdk_config(sdk, json_config, config):
  176. script_path = os.path.join(file_utils.get_current_path(), 'sdk_script')
  177. sdk_script = get_script_mapping(sdk)
  178. target_script = os.path.join(script_path, '%s.py' % sdk_script)
  179. if not os.path.exists(target_script):
  180. print('%s no exists' % target_script)
  181. return 0
  182. print('sdk_script -- > %s' % target_script)
  183. sys.path.append(script_path)
  184. module = importlib.import_module(sdk_script) # 动态导入相应模块
  185. module.get_sdk_config(json_config, config) # 执行脚本功能
  186. sys.path.remove(script_path)
  187. def create_or_update_config_file(game, sdk, json_config):
  188. """
  189. 更新配置文件
  190. """
  191. if 'subChannel' not in json_config:
  192. return 0
  193. print('createOrUpdateConfigFile ...')
  194. random = json_config['random']
  195. channelPath = file_utils.getChannelPath(game, random, sdk)
  196. configPath = os.path.join(channelPath, 'config.json')
  197. # configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  198. print("configPath%s" % configPath)
  199. print("channelPath%s" % channelPath)
  200. if os.path.exists(configPath):
  201. # 更新数据
  202. jsonText = file_utils.read_file(configPath)
  203. config = json.loads(jsonText)
  204. count = 0
  205. if type(config) == list:
  206. for item in config:
  207. if item['subChannel'] == json_config['subChannel']:
  208. print('find same config ...')
  209. del config[count]
  210. break
  211. count += 1
  212. config.append(json_config)
  213. createConfigFile(config, configPath)
  214. elif type(config) == dict:
  215. if config['subChannel'] == json_config['subChannel']:
  216. print('find same config ...')
  217. createConfigFile(json_config, configPath)
  218. else:
  219. print('add a new config ...')
  220. config = [config, json_config]
  221. createConfigFile(config, configPath)
  222. else:
  223. print('create a new config ...')
  224. createConfigFile([json_config], configPath)
  225. def createConfigFile(json_config, config_path):
  226. """
  227. 创建配置文件
  228. """
  229. jsonStr = json.dumps(json_config, ensure_ascii=False)
  230. print('*************out config*************')
  231. print(jsonStr)
  232. print('************************************')
  233. file_utils.create_file(config_path, jsonStr)
  234. def copy_res(game, sdk, sub_channel, config):
  235. """
  236. 拷贝资源
  237. """
  238. if sub_channel is None:
  239. return 0
  240. random = config['random']
  241. channelPath = file_utils.getChannelPath(game, random, sdk)
  242. subChannelPath = os.path.join(channelPath, sub_channel)
  243. print('channel_path: %s' % channelPath)
  244. print('sub_channel_path: %s' % subChannelPath)
  245. if 'icon' in config and os.path.exists(config['icon']):
  246. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  247. for mipmap in mipmapSupport:
  248. iconPath = os.path.join(
  249. subChannelPath, 'icon', mipmap, 'common_sdk_icon.png')
  250. file_utils.copy_file(config['icon'], iconPath)
  251. '''if 'switchIcon' in config and os.path.exists(config['switchIcon']):
  252. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  253. for mipmap in mipmapSupport:
  254. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon2.png')
  255. file_utils.copyFile(config['switchIcon'], iconPath)'''
  256. if 'splash' in config and os.path.exists(config['splash']):
  257. splashPath = os.path.join(
  258. subChannelPath, 'splash', 'drawable-hdpi', 'common_sdk_launcher_bg.jpg')
  259. file_utils.copy_file(config['splash'], splashPath)
  260. # print('------subChannelPath 目录清空:%s -------' % subChannelPath)
  261. # file_utils.deleteFolder(subChannelPath)
  262. if 'copyList' in config:
  263. for item in config['copyList']:
  264. if item['toFile'] == '':
  265. continue
  266. if not os.path.exists(item['fromFile']):
  267. continue
  268. toFile = item['toFile']
  269. if toFile[:3] == 'res':
  270. toFile = 'image' + toFile[3:]
  271. resPath = get_package_path(subChannelPath, toFile)
  272. file_utils.copy_file(item['fromFile'], resPath)
  273. if 'package' in config and os.path.exists(config['package']):
  274. newGameApk = config['package']
  275. gameApk = file_utils.getFullGameApk(game)
  276. cache_game_apk = file_utils.get_cache_game_apk(game, random, sdk)
  277. file_utils.copy_file(newGameApk, cache_game_apk)
  278. return 1
  279. def get_package_path(base_path, package_name):
  280. """
  281. 包名对应的目录
  282. """
  283. package_name_split = package_name.replace('\\', '/').split('/')
  284. newPath = base_path
  285. for item in package_name_split:
  286. newPath = os.path.join(newPath, item)
  287. return newPath
  288. def get_mapping_sdk(config):
  289. sdk = config['sdk']
  290. mapping = sdk
  291. if sdk in sdk_mapping:
  292. mapping = sdk_mapping[sdk]
  293. else:
  294. return 'jm_' + sdk
  295. if type(mapping) == dict:
  296. sdk_config = config[sdk]
  297. if 'SUB_CHANNEL' in sdk_config and sdk_config['SUB_CHANNEL'] != '':
  298. sub_channel = sdk_config['SUB_CHANNEL']
  299. else:
  300. sub_channel = 'default'
  301. return mapping[sub_channel]
  302. else:
  303. return mapping
  304. def get_script_mapping(sdk):
  305. if sdk in script_mapping:
  306. return script_mapping[sdk]
  307. return sdk
  308. packageWeb()
  309. # if __name__ == '__main__':
  310. # package_test('/Users/suyghur/Develop/yyxx/qs/test_config/test.json')