package_web.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. jsonConfig['game'] = game
  78. # 可选参数
  79. if 'outName' in config:
  80. jsonConfig['outName'] = config['outName']
  81. if 'outPath' in config:
  82. jsonConfig['outPath'] = config['outPath']
  83. if 'changeIcon' in config:
  84. jsonConfig['changeIcon'] = toBoolean(config['changeIcon'])
  85. if 'addLauncher' in config:
  86. jsonConfig['addLauncher'] = toBoolean(config['addLauncher'])
  87. if 'versionCode' in config:
  88. jsonConfig['versionCode'] = config['versionCode']
  89. if 'versionName' in config:
  90. jsonConfig['versionName'] = config['versionName']
  91. if 'targetSdkVersion' in config:
  92. jsonConfig['targetSdkVersion'] = config['targetSdkVersion']
  93. if 'v2disable' in config:
  94. jsonConfig['v2disable'] = toBoolean(config['v2disable'])
  95. if 'aapt2disable' in config:
  96. jsonConfig['aapt2disable'] = toBoolean(config['aapt2disable'])
  97. # sdk相关参数
  98. if 'properties' in config:
  99. jsonConfig['properties'] = config['properties']
  100. jsonConfig['properties']['isDebugMode'] = 'false'
  101. jsonConfig['logSdk'] = ['jrtt', 'gdt']
  102. # 获取sdk相关配置
  103. getSdkConfig(sdk, jsonConfig, config)
  104. # 生成配置文件
  105. createOrUpdateConfigFile(game, sdk, jsonConfig)
  106. # 拷贝资源
  107. copyRes(game, sdk, subChannel, config)
  108. # 打包
  109. package_utils.packConsole(game, sdk, subChannel)
  110. def toBoolean(booleanStr):
  111. if type(booleanStr) == bool:
  112. return booleanStr
  113. if booleanStr == 'true':
  114. return True
  115. return False
  116. def getSdkConfig(sdk, jsonConfig, config):
  117. scriptPath = os.path.join(file_utils.getCurrentPath(), 'sdk_script')
  118. sdkScript = getScriptMapping(sdk)
  119. targetScript = os.path.join(scriptPath, '%s.py' % sdkScript)
  120. if not os.path.exists(targetScript):
  121. print('%s no exists' % targetScript)
  122. return 0
  123. print('sdk_script -- > %s' % targetScript)
  124. sys.path.append(scriptPath)
  125. module = importlib.import_module(sdkScript)# 动态导入相应模块
  126. module.getSdkConfig(jsonConfig, config)# 执行脚本功能
  127. sys.path.remove(scriptPath)
  128. def createOrUpdateConfigFile(game, sdk, jsonConfig):
  129. '''
  130. 更新配置文件
  131. '''
  132. if 'subChannel' not in jsonConfig:
  133. return 0
  134. print('createOrUpdateConfigFile ...')
  135. channelPath = file_utils.getChannelPath(game, sdk)
  136. configPath = os.path.join(channelPath, 'config.json')
  137. #configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  138. if os.path.exists(configPath):
  139. # 更新数据
  140. jsonText = file_utils.readFile(configPath)
  141. config = json.loads(jsonText)
  142. count = 0
  143. if type(config) == list:
  144. for item in config:
  145. if item['subChannel'] == jsonConfig['subChannel']:
  146. print('find same config ...')
  147. del config[count]
  148. break
  149. count += 1
  150. config.append(jsonConfig)
  151. createConfigFile(config, configPath)
  152. elif type(config) == dict:
  153. if config['subChannel'] == jsonConfig['subChannel']:
  154. print('find same config ...')
  155. createConfigFile(jsonConfig, configPath)
  156. else:
  157. print('add a new config ...')
  158. config = [config, jsonConfig]
  159. createConfigFile(config, configPath)
  160. else:
  161. print('create a new config ...')
  162. createConfigFile([jsonConfig], configPath)
  163. def createConfigFile(jsonConfig, configPath):
  164. '''
  165. 创建配置文件
  166. '''
  167. jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
  168. print('*************out config*************')
  169. print(jsonStr)
  170. print('************************************')
  171. file_utils.createFile(configPath, jsonStr)
  172. def copyRes(game, sdk, subChannel, config):
  173. '''
  174. 拷贝资源
  175. '''
  176. if subChannel is None:
  177. return 0
  178. channelPath = file_utils.getChannelPath(game, sdk)
  179. subChannelPath = os.path.join(channelPath, subChannel)
  180. if 'icon' in config and os.path.exists(config['icon']):
  181. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  182. for mipmap in mipmapSupport:
  183. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon.png')
  184. file_utils.copyFile(config['icon'], iconPath)
  185. '''if 'switchIcon' in config and os.path.exists(config['switchIcon']):
  186. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  187. for mipmap in mipmapSupport:
  188. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon2.png')
  189. file_utils.copyFile(config['switchIcon'], iconPath)'''
  190. if 'splash' in config and os.path.exists(config['splash']):
  191. splashPath = os.path.join(subChannelPath, 'splash', 'drawable-hdpi', 'common_sdk_launcher_bg.jpg')
  192. file_utils.copyFile(config['splash'], splashPath)
  193. if 'copyList' in config:
  194. for item in config['copyList']:
  195. if item['toFile'] == '':
  196. continue
  197. if not os.path.exists(item['fromFile']):
  198. continue
  199. toFile = item['toFile']
  200. if toFile[:3] == 'res':
  201. toFile = 'image' + toFile[3:]
  202. resPath = getPackagePath(subChannelPath, toFile)
  203. file_utils.copyFile(item['fromFile'], resPath)
  204. if 'package' in config and os.path.exists(config['package']):
  205. newGameApk = config['package']
  206. gameApk = file_utils.getFullGameApk(game)
  207. file_utils.copyFile(newGameApk, gameApk)
  208. def getPackagePath(basePath, packageName):
  209. '''
  210. 包名对应的目录
  211. '''
  212. packageNameSplit = packageName.replace('\\', '/').split('/')
  213. newPath = basePath
  214. for item in packageNameSplit:
  215. newPath = os.path.join(newPath, item)
  216. return newPath
  217. def getMappingSdk(config):
  218. sdk = config['sdk']
  219. mapping = sdk
  220. if sdk in sdkMapping:
  221. mapping = sdkMapping[sdk]
  222. else:
  223. return 'jm_' + sdk
  224. if type(mapping) == dict:
  225. sdkConfig = config[sdk]
  226. if 'SUB_CHANNEL' in sdkConfig and sdkConfig['SUB_CHANNEL'] != '':
  227. subChannel = sdkConfig['SUB_CHANNEL']
  228. else:
  229. subChannel = 'default'
  230. return mapping[subChannel]
  231. else:
  232. return mapping
  233. def getScriptMapping(sdk):
  234. if sdk in scriptMapping:
  235. return scriptMapping[sdk]
  236. return sdk
  237. packageWeb()