package_web_record.py 7.9 KB

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