package_web.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. import common_utils
  11. sdkMapping = {
  12. 'ysdk': 'jm_ysdk',
  13. 'ziyun': {
  14. 'default': 'jm_zy',
  15. 'ysdk': 'jm_zy_ysdk',
  16. },
  17. 'xunqu': {
  18. 'default': 'jm_xq',
  19. 'xiaomi': 'jm_xq_mi',
  20. 'jrtt': 'jm_xq_jrtt'
  21. },
  22. 'jingqi': 'jm_jq',
  23. 'qytx': {
  24. 'default': 'jm_qytx',
  25. 'cangyu': 'jm_qytx2',
  26. 'cangyu_new': 'jm_cangyu'
  27. },
  28. 'jmsdk': 'jm',
  29. 'oppo': 'jm_oppo',
  30. 'quick': 'jm_quick',
  31. 'beiyu': 'jm_beiyu',
  32. 'yijie': 'jm_yijie',
  33. 'gzjysdk': 'gzjysdk',
  34. 'record': 'record',
  35. 'float': 'float',
  36. 'yfsdk': 'yfsdk'
  37. }
  38. scriptMapping = {
  39. 'jm_xq_mi': 'jm_xq',
  40. 'jm_xq_jrtt': 'jm_xq',
  41. 'jm_qytx2': 'jm_qytx',
  42. 'jm_cangyu': 'jm_qytx'
  43. }
  44. newSdk = ["shanshen", "gzjysdk"]
  45. recordSdk = ["record", "float"]
  46. yanfaSdk = ["yfsdk"] # 研发sdk打包
  47. def packageWeb():
  48. if len(sys.argv) < 2:
  49. print('argument is missing')
  50. return 1
  51. # 打包配置的路径
  52. packageConfig = sys.argv[1]
  53. jsonText = file_utils.readFile(packageConfig)
  54. config = json.loads(jsonText)
  55. print('*************config*****************')
  56. print(jsonText)
  57. print('************************************')
  58. sdk = getMappingSdk(config)
  59. if sdk in recordSdk:
  60. package_web_record.package(config, sdk)
  61. elif sdk in newSdk:
  62. package_web_shanshen.package(config, sdk)
  63. elif sdk in yanfaSdk:
  64. package_web_yfsdk.package(config, sdk)
  65. else:
  66. package(config, sdk)
  67. def package(config, sdk):
  68. print('use script 1st')
  69. jsonConfig = {}
  70. game = config['app']
  71. subChannel = None
  72. if 'subChannel' in config:
  73. subChannel = config['subChannel']
  74. jsonConfig['subChannel'] = config['subChannel']
  75. config['random'] = common_utils.getRandomNumber(6)
  76. outName = config['random']
  77. channelPath = file_utils.getChannelPath(game, outName, sdk)
  78. subChannelPath = os.path.join(channelPath, subChannel)
  79. print('打包开始,删除缓存目录 %s ' % subChannelPath)
  80. file_utils.deleteFolder(subChannelPath)
  81. # 必须参数
  82. jsonConfig['packageName'] = config['packageName']
  83. jsonConfig['name'] = config['name']
  84. jsonConfig['random'] = config['random']
  85. # 可选参数
  86. if 'outName' in config:
  87. jsonConfig['outName'] = config['outName']
  88. if 'outPath' in config:
  89. jsonConfig['outPath'] = config['outPath']
  90. if 'changeIcon' in config:
  91. jsonConfig['changeIcon'] = toBoolean(config['changeIcon'])
  92. if 'addLauncher' in config:
  93. jsonConfig['addLauncher'] = toBoolean(config['addLauncher'])
  94. if 'versionCode' in config:
  95. jsonConfig['versionCode'] = config['versionCode']
  96. if 'versionName' in config:
  97. jsonConfig['versionName'] = config['versionName']
  98. if 'targetSdkVersion' in config:
  99. jsonConfig['targetSdkVersion'] = config['targetSdkVersion']
  100. if 'v2disable' in config:
  101. jsonConfig['v2disable'] = toBoolean(config['v2disable'])
  102. if 'aapt2disable' in config:
  103. jsonConfig['aapt2disable'] = toBoolean(config['aapt2disable'])
  104. # sdk相关参数
  105. if 'properties' in config:
  106. jsonConfig['properties'] = config['properties']
  107. jsonConfig['properties']['isDebugMode'] = 'false'
  108. # jsonConfig['logSdk'] = ['jrtt', 'gdt', 'ks', 'uc']
  109. # 广点通参数
  110. if 'gdt_params' in config:
  111. jsonConfig['logSdk'] = ['gdt']
  112. gdt = {'gdt': config['gdt_params']}
  113. if 'configData' in jsonConfig:
  114. configData = jsonConfig['configData']
  115. configData['channel_sdk_list'] = gdt
  116. else:
  117. jsonConfig['configData'] = {
  118. 'channel_sdk_list': gdt
  119. }
  120. # uc广告参数
  121. if 'uc_params' in config:
  122. jsonConfig['logSdk'] = ['uc']
  123. uc = {'uc': config['uc_params']}
  124. if 'configData' in jsonConfig:
  125. configData = jsonConfig['configData']
  126. configData['channel_sdk_list'] = uc
  127. else:
  128. jsonConfig['configData'] = {
  129. 'channel_sdk_list': uc
  130. }
  131. # baidu广告参数
  132. if 'bd_params' in config:
  133. jsonConfig['logSdk'] = ['bd']
  134. bd = {'bd': config['bd_params']}
  135. if 'configData' in jsonConfig:
  136. configData = jsonConfig['configData']
  137. configData['channel_sdk_list'] = bd
  138. else:
  139. jsonConfig['configData'] = {
  140. 'channel_sdk_list': bd
  141. }
  142. if 'tt_params' in config:
  143. jsonConfig['logSdk'] = ['jrtt']
  144. jrtt = {'jrtt': config['tt_params']}
  145. if 'configData' in jsonConfig:
  146. configData = jsonConfig['configData']
  147. configData['channel_sdk_list'] = jrtt
  148. else:
  149. jsonConfig['configData'] = {
  150. 'channel_sdk_list': jrtt
  151. }
  152. if 'ks_params' in config:
  153. jsonConfig['logSdk'] = ['ks']
  154. # 获取sdk相关配置
  155. getSdkConfig(sdk, jsonConfig, config)
  156. # 生成配置文件
  157. createOrUpdateConfigFile(game, sdk, jsonConfig)
  158. # 拷贝资源
  159. copyRes(game, sdk, subChannel, config)
  160. # 打包
  161. package_utils.packConsole(game, sdk, config, subChannel)
  162. def toBoolean(booleanStr):
  163. if type(booleanStr) == bool:
  164. return booleanStr
  165. if booleanStr == 'true':
  166. return True
  167. return False
  168. def getSdkConfig(sdk, jsonConfig, config):
  169. scriptPath = os.path.join(file_utils.getCurrentPath(), 'sdk_script')
  170. sdkScript = getScriptMapping(sdk)
  171. targetScript = os.path.join(scriptPath, '%s.py' % sdkScript)
  172. if not os.path.exists(targetScript):
  173. print('%s no exists' % targetScript)
  174. return 0
  175. print('sdk_script -- > %s' % targetScript)
  176. sys.path.append(scriptPath)
  177. module = importlib.import_module(sdkScript) # 动态导入相应模块
  178. module.getSdkConfig(jsonConfig, config) # 执行脚本功能
  179. sys.path.remove(scriptPath)
  180. def createOrUpdateConfigFile(game, sdk, jsonConfig):
  181. '''
  182. 更新配置文件
  183. '''
  184. if 'subChannel' not in jsonConfig:
  185. return 0
  186. print('createOrUpdateConfigFile ...')
  187. random = jsonConfig['random']
  188. channelPath = file_utils.getChannelPath(game, random, sdk)
  189. configPath = os.path.join(channelPath, 'config.json')
  190. # configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  191. print("configPath%s" % configPath)
  192. print("channelPath%s" % channelPath)
  193. if os.path.exists(configPath):
  194. # 更新数据
  195. jsonText = file_utils.readFile(configPath)
  196. config = json.loads(jsonText)
  197. count = 0
  198. if type(config) == list:
  199. for item in config:
  200. if item['subChannel'] == jsonConfig['subChannel']:
  201. print('find same config ...')
  202. del config[count]
  203. break
  204. count += 1
  205. config.append(jsonConfig)
  206. createConfigFile(config, configPath)
  207. elif type(config) == dict:
  208. if config['subChannel'] == jsonConfig['subChannel']:
  209. print('find same config ...')
  210. createConfigFile(jsonConfig, configPath)
  211. else:
  212. print('add a new config ...')
  213. config = [config, jsonConfig]
  214. createConfigFile(config, configPath)
  215. else:
  216. print('create a new config ...')
  217. createConfigFile([jsonConfig], configPath)
  218. def createConfigFile(jsonConfig, configPath):
  219. '''
  220. 创建配置文件
  221. '''
  222. jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
  223. print('*************out config*************')
  224. print(jsonStr)
  225. print('************************************')
  226. file_utils.createFile(configPath, jsonStr)
  227. def copyRes(game, sdk, subChannel, config):
  228. '''
  229. 拷贝资源
  230. '''
  231. if subChannel is None:
  232. return 0
  233. random = config['random']
  234. channelPath = file_utils.getChannelPath(game, random, sdk)
  235. subChannelPath = os.path.join(channelPath, subChannel)
  236. if 'icon' in config and os.path.exists(config['icon']):
  237. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  238. for mipmap in mipmapSupport:
  239. iconPath = os.path.join(
  240. subChannelPath, 'icon', mipmap, 'common_sdk_icon.png')
  241. file_utils.copyFile(config['icon'], iconPath)
  242. '''if 'switchIcon' in config and os.path.exists(config['switchIcon']):
  243. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  244. for mipmap in mipmapSupport:
  245. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon2.png')
  246. file_utils.copyFile(config['switchIcon'], iconPath)'''
  247. if 'splash' in config and os.path.exists(config['splash']):
  248. splashPath = os.path.join(
  249. subChannelPath, 'splash', 'drawable-hdpi', 'common_sdk_launcher_bg.jpg')
  250. file_utils.copyFile(config['splash'], splashPath)
  251. # print('------subChannelPath 目录清空:%s -------' % subChannelPath)
  252. # file_utils.deleteFolder(subChannelPath)
  253. if 'copyList' in config:
  254. for item in config['copyList']:
  255. if item['toFile'] == '':
  256. continue
  257. if not os.path.exists(item['fromFile']):
  258. continue
  259. toFile = item['toFile']
  260. if toFile[:3] == 'res':
  261. toFile = 'image' + toFile[3:]
  262. resPath = getPackagePath(subChannelPath, toFile)
  263. file_utils.copyFile(item['fromFile'], resPath)
  264. if 'package' in config and os.path.exists(config['package']):
  265. newGameApk = config['package']
  266. gameApk = file_utils.getFullGameApk(game)
  267. cacheGameApk = file_utils.getCacheGameApk(game, random, sdk)
  268. file_utils.copyFile(newGameApk, cacheGameApk)
  269. return 1
  270. def getPackagePath(basePath, packageName):
  271. '''
  272. 包名对应的目录
  273. '''
  274. packageNameSplit = packageName.replace('\\', '/').split('/')
  275. newPath = basePath
  276. for item in packageNameSplit:
  277. newPath = os.path.join(newPath, item)
  278. return newPath
  279. def getMappingSdk(config):
  280. sdk = config['sdk']
  281. mapping = sdk
  282. if sdk in sdkMapping:
  283. mapping = sdkMapping[sdk]
  284. else:
  285. return 'jm_' + sdk
  286. if type(mapping) == dict:
  287. sdkConfig = config[sdk]
  288. if 'SUB_CHANNEL' in sdkConfig and sdkConfig['SUB_CHANNEL'] != '':
  289. subChannel = sdkConfig['SUB_CHANNEL']
  290. else:
  291. subChannel = 'default'
  292. return mapping[subChannel]
  293. else:
  294. return mapping
  295. def getScriptMapping(sdk):
  296. if sdk in scriptMapping:
  297. return scriptMapping[sdk]
  298. return sdk
  299. packageWeb()