package_web.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import file_utils
  2. import package_utils
  3. import package_web_record
  4. import package_web_shanshen
  5. import package_web_yfsdk
  6. import os.path
  7. import sys
  8. import json
  9. import importlib
  10. sdkMapping = {
  11. 'ysdk':'jm_ysdk',
  12. 'ziyun': {
  13. 'default': 'jm_zy',
  14. 'ysdk': 'jm_zy_ysdk',
  15. },
  16. 'xunqu':{
  17. 'default':'jm_xq',
  18. 'xiaomi':'jm_xq_mi',
  19. 'jrtt':'jm_xq_jrtt'
  20. },
  21. 'jingqi':'jm_jq',
  22. 'qytx':{
  23. 'default':'jm_qytx',
  24. 'cangyu':'jm_qytx2',
  25. 'cangyu_new':'jm_cangyu'
  26. },
  27. 'jmsdk':'jm',
  28. 'oppo':'jm_oppo',
  29. 'quick':'jm_quick',
  30. 'beiyu':'jm_beiyu',
  31. 'yijie':'jm_yijie',
  32. 'gzjysdk':'gzjysdk',
  33. 'record':'record',
  34. 'float':'float',
  35. 'yfsdk':'yfsdk'
  36. }
  37. scriptMapping = {
  38. 'jm_xq_mi':'jm_xq',
  39. 'jm_xq_jrtt':'jm_xq',
  40. 'jm_qytx2':'jm_qytx',
  41. 'jm_cangyu':'jm_qytx'
  42. }
  43. newSdk = ["shanshen", "gzjysdk"]
  44. recordSdk = ["record", "float"]
  45. yanfaSdk = ["yfsdk"] #研发sdk打包
  46. def packageWeb():
  47. if len(sys.argv) < 2:
  48. print('argument is missing')
  49. return 1
  50. # 打包配置的路径
  51. packageConfig = sys.argv[1]
  52. jsonText = file_utils.readFile(packageConfig)
  53. config = json.loads(jsonText)
  54. print('*************config*****************')
  55. print(jsonText)
  56. print('************************************')
  57. sdk = getMappingSdk(config)
  58. if sdk in recordSdk:
  59. package_web_record.package(config, sdk)
  60. elif sdk in newSdk:
  61. package_web_shanshen.package(config, sdk)
  62. elif sdk in yanfaSdk:
  63. package_web_yfsdk.package(config, sdk)
  64. else:
  65. package(config, sdk)
  66. def package(config, sdk):
  67. print('use script 1st')
  68. jsonConfig = {}
  69. game = config['app']
  70. subChannel = None
  71. if 'subChannel' in config:
  72. subChannel = config['subChannel']
  73. jsonConfig['subChannel'] = config['subChannel']
  74. # 必须参数
  75. jsonConfig['packageName'] = config['packageName']
  76. jsonConfig['name'] = config['name']
  77. # 可选参数
  78. if 'outName' in config:
  79. jsonConfig['outName'] = config['outName']
  80. if 'outPath' in config:
  81. jsonConfig['outPath'] = config['outPath']
  82. if 'changeIcon' in config:
  83. jsonConfig['changeIcon'] = toBoolean(config['changeIcon'])
  84. if 'addLauncher' in config:
  85. jsonConfig['addLauncher'] = toBoolean(config['addLauncher'])
  86. if 'versionCode' in config:
  87. jsonConfig['versionCode'] = config['versionCode']
  88. if 'versionName' in config:
  89. jsonConfig['versionName'] = config['versionName']
  90. if 'targetSdkVersion' in config:
  91. jsonConfig['targetSdkVersion'] = config['targetSdkVersion']
  92. if 'v2disable' in config:
  93. jsonConfig['v2disable'] = toBoolean(config['v2disable'])
  94. if 'aapt2disable' in config:
  95. jsonConfig['aapt2disable'] = toBoolean(config['aapt2disable'])
  96. # sdk相关参数
  97. if 'properties' in config:
  98. jsonConfig['properties'] = config['properties']
  99. jsonConfig['properties']['isDebugMode'] = 'false'
  100. jsonConfig['logSdk'] = ['jrtt', 'gdt']
  101. #广点通参数
  102. if 'gdt_params' in config:
  103. gdt = { 'gdt':config['gdt_params']}
  104. if 'configData' in jsonConfig:
  105. configData = jsonConfig['configData']
  106. configData['channel_sdk_list'] = gdt
  107. else:
  108. jsonConfig['configData'] = {
  109. 'channel_sdk_list':gdt
  110. }
  111. # 获取sdk相关配置
  112. getSdkConfig(sdk, jsonConfig, config)
  113. # 生成配置文件
  114. createOrUpdateConfigFile(game, sdk, jsonConfig)
  115. # 拷贝资源
  116. copyRes(game, sdk, subChannel, config)
  117. # 打包
  118. package_utils.packConsole(game, sdk, subChannel)
  119. def toBoolean(booleanStr):
  120. if type(booleanStr) == bool:
  121. return booleanStr
  122. if booleanStr == 'true':
  123. return True
  124. return False
  125. def getSdkConfig(sdk, jsonConfig, config):
  126. scriptPath = os.path.join(file_utils.getCurrentPath(), 'sdk_script')
  127. sdkScript = getScriptMapping(sdk)
  128. targetScript = os.path.join(scriptPath, '%s.py' % sdkScript)
  129. if not os.path.exists(targetScript):
  130. print('%s no exists' % targetScript)
  131. return 0
  132. print('sdk_script -- > %s' % targetScript)
  133. sys.path.append(scriptPath)
  134. module = importlib.import_module(sdkScript)# 动态导入相应模块
  135. module.getSdkConfig(jsonConfig, config)# 执行脚本功能
  136. sys.path.remove(scriptPath)
  137. def createOrUpdateConfigFile(game, sdk, jsonConfig):
  138. '''
  139. 更新配置文件
  140. '''
  141. if 'subChannel' not in jsonConfig:
  142. return 0
  143. print('createOrUpdateConfigFile ...')
  144. channelPath = file_utils.getChannelPath(game, sdk)
  145. configPath = os.path.join(channelPath, 'config.json')
  146. #configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  147. if os.path.exists(configPath):
  148. # 更新数据
  149. jsonText = file_utils.readFile(configPath)
  150. config = json.loads(jsonText)
  151. count = 0
  152. if type(config) == list:
  153. for item in config:
  154. if item['subChannel'] == jsonConfig['subChannel']:
  155. print('find same config ...')
  156. del config[count]
  157. break
  158. count += 1
  159. config.append(jsonConfig)
  160. createConfigFile(config, configPath)
  161. elif type(config) == dict:
  162. if config['subChannel'] == jsonConfig['subChannel']:
  163. print('find same config ...')
  164. createConfigFile(jsonConfig, configPath)
  165. else:
  166. print('add a new config ...')
  167. config = [config, jsonConfig]
  168. createConfigFile(config, configPath)
  169. else:
  170. print('create a new config ...')
  171. createConfigFile([jsonConfig], configPath)
  172. def createConfigFile(jsonConfig, configPath):
  173. '''
  174. 创建配置文件
  175. '''
  176. jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
  177. print('*************out config*************')
  178. print(jsonStr)
  179. print('************************************')
  180. file_utils.createFile(configPath, jsonStr)
  181. def copyRes(game, sdk, subChannel, config):
  182. '''
  183. 拷贝资源
  184. '''
  185. if subChannel is None:
  186. return 0
  187. channelPath = file_utils.getChannelPath(game, sdk)
  188. subChannelPath = os.path.join(channelPath, subChannel)
  189. if 'icon' in config and os.path.exists(config['icon']):
  190. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  191. for mipmap in mipmapSupport:
  192. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon.png')
  193. file_utils.copyFile(config['icon'], iconPath)
  194. '''if 'switchIcon' in config and os.path.exists(config['switchIcon']):
  195. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  196. for mipmap in mipmapSupport:
  197. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon2.png')
  198. file_utils.copyFile(config['switchIcon'], iconPath)'''
  199. if 'splash' in config and os.path.exists(config['splash']):
  200. splashPath = os.path.join(subChannelPath, 'splash', 'drawable-hdpi', 'common_sdk_launcher_bg.jpg')
  201. file_utils.copyFile(config['splash'], splashPath)
  202. if 'copyList' in config:
  203. for item in config['copyList']:
  204. if item['toFile'] == '':
  205. continue
  206. if not os.path.exists(item['fromFile']):
  207. continue
  208. toFile = item['toFile']
  209. if toFile[:3] == 'res':
  210. toFile = 'image' + toFile[3:]
  211. resPath = getPackagePath(subChannelPath, toFile)
  212. file_utils.copyFile(item['fromFile'], resPath)
  213. if 'package' in config and os.path.exists(config['package']):
  214. newGameApk = config['package']
  215. gameApk = file_utils.getFullGameApk(game)
  216. file_utils.copyFile(newGameApk, gameApk)
  217. def getPackagePath(basePath, packageName):
  218. '''
  219. 包名对应的目录
  220. '''
  221. packageNameSplit = packageName.replace('\\', '/').split('/')
  222. newPath = basePath
  223. for item in packageNameSplit:
  224. newPath = os.path.join(newPath, item)
  225. return newPath
  226. def getMappingSdk(config):
  227. sdk = config['sdk']
  228. mapping = sdk
  229. if sdk in sdkMapping:
  230. mapping = sdkMapping[sdk]
  231. else:
  232. return 'jm_' + sdk
  233. if type(mapping) == dict:
  234. sdkConfig = config[sdk]
  235. if 'SUB_CHANNEL' in sdkConfig and sdkConfig['SUB_CHANNEL'] != '':
  236. subChannel = sdkConfig['SUB_CHANNEL']
  237. else:
  238. subChannel = 'default'
  239. return mapping[subChannel]
  240. else:
  241. return mapping
  242. def getScriptMapping(sdk):
  243. if sdk in scriptMapping:
  244. return scriptMapping[sdk]
  245. return sdk
  246. packageWeb()