package_web.py 8.2 KB

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