package_web.py 7.9 KB

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