package_web.py 7.7 KB

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