package_web.py 8.0 KB

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