package_web.py 7.9 KB

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