package_web.py 7.6 KB

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