package_web.py 7.7 KB

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