package_web_record.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. # 获取sdk相关配置
  54. getSdkConfig(sdk, jsonConfig, config)
  55. # 生成配置文件
  56. createOrUpdateConfigFile(game, sdk, jsonConfig)
  57. # 拷贝资源
  58. copyRes(game, sdk, subChannel, config)
  59. # 打包
  60. package_utils_record.packConsole(game, sdk, subChannel)
  61. def setRecordConfig(config, recordConfig):
  62. configData = None
  63. if 'configData' in config:
  64. configData = config['configData']
  65. configData['bgMusic'] = 'bg_music.mp3'
  66. configData['gameId'] = recordConfig['gameId']
  67. configData['gameName'] = recordConfig['gameName']
  68. configData['gameIcon'] = recordConfig['gameIcon']
  69. configData['gameUrl'] = recordConfig['gameUrl']
  70. configData['skinId'] = recordConfig['skinId']
  71. else:
  72. config['configData'] = {
  73. 'bgMusic':'bg_music.mp3',
  74. 'gameId':recordConfig['gameId'],
  75. 'gameName':recordConfig['gameName'],
  76. 'gameIcon':recordConfig['gameIcon'],
  77. 'gameUrl':recordConfig['gameUrl'],
  78. 'skinId':recordConfig['skinId']
  79. }
  80. def toBoolean(booleanStr):
  81. if type(booleanStr) == bool:
  82. return booleanStr
  83. if booleanStr == 'true':
  84. return True
  85. return False
  86. def getSdkConfig(sdk, jsonConfig, config):
  87. scriptPath = os.path.join(file_utils.getCurrentPath(), 'sdk_script')
  88. sdkScript = getScriptMapping(sdk)
  89. targetScript = os.path.join(scriptPath, '%s.py' % sdkScript)
  90. if not os.path.exists(targetScript):
  91. print('%s no exists' % targetScript)
  92. return 0
  93. sys.path.append(scriptPath)
  94. module = importlib.import_module(sdkScript)# 动态导入相应模块
  95. module.getSdkConfig(jsonConfig, config)# 执行脚本功能
  96. sys.path.remove(scriptPath)
  97. def createOrUpdateConfigFile(game, sdk, jsonConfig):
  98. '''
  99. 更新配置文件
  100. '''
  101. if 'subChannel' not in jsonConfig:
  102. return 0
  103. print('createOrUpdateConfigFile ...')
  104. channelPath = file_utils.getChannelPath(game, sdk)
  105. configPath = os.path.join(channelPath, 'config.json')
  106. #configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  107. if os.path.exists(configPath):
  108. # 更新数据
  109. jsonText = file_utils.readFile(configPath)
  110. config = json.loads(jsonText)
  111. count = 0
  112. if type(config) == list:
  113. for item in config:
  114. if item['subChannel'] == jsonConfig['subChannel']:
  115. print('find same config ...')
  116. del config[count]
  117. break
  118. count += 1
  119. config.append(jsonConfig)
  120. createConfigFile(config, configPath)
  121. elif type(config) == dict:
  122. if config['subChannel'] == jsonConfig['subChannel']:
  123. print('find same config ...')
  124. createConfigFile(jsonConfig, configPath)
  125. else:
  126. print('add a new config ...')
  127. config = [config, jsonConfig]
  128. createConfigFile(config, configPath)
  129. else:
  130. print('create a new config ...')
  131. createConfigFile([jsonConfig], configPath)
  132. def createConfigFile(jsonConfig, configPath):
  133. '''
  134. 创建配置文件
  135. '''
  136. jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
  137. print('*************out config*************')
  138. print(jsonStr)
  139. print('************************************')
  140. file_utils.createFile(configPath, jsonStr)
  141. def copyRes(game, sdk, subChannel, config):
  142. '''
  143. 拷贝资源
  144. '''
  145. if subChannel is None:
  146. return 0
  147. channelPath = file_utils.getChannelPath(game, sdk)
  148. subChannelPath = os.path.join(channelPath, subChannel)
  149. if 'icon' in config and os.path.exists(config['icon']):
  150. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  151. for mipmap in mipmapSupport:
  152. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'record_sdk_icon.png')
  153. file_utils.copyFile(config['icon'], iconPath)
  154. if 'splash' in config and os.path.exists(config['splash']):
  155. splashPath = os.path.join(subChannelPath, 'splash', 'drawable-hdpi', 'shanshen_sdk_launcher_bg.jpg')
  156. file_utils.copyFile(config['splash'], splashPath)
  157. if 'bgMusic' in config and os.path.exists(config['bgMusic']):
  158. musicPath = os.path.join(subChannelPath, 'assets', 'bg_music.mp3')
  159. file_utils.copyFile(config['bgMusic'], musicPath)
  160. if 'copyList' in config:
  161. for item in config['copyList']:
  162. if item['toFile'] == '':
  163. continue
  164. if not os.path.exists(item['fromFile']):
  165. continue
  166. toFile = item['toFile']
  167. if toFile[:3] == 'res':
  168. toFile = 'image' + toFile[3:]
  169. resPath = getPackagePath(subChannelPath, toFile)
  170. file_utils.copyFile(item['fromFile'], resPath)
  171. if 'package' in config and os.path.exists(config['package']):
  172. newGameApk = config['package']
  173. gameApk = file_utils.getFullGameApk(game)
  174. file_utils.copyFile(newGameApk, gameApk)
  175. def getPackagePath(basePath, packageName):
  176. '''
  177. 包名对应的目录
  178. '''
  179. packageNameSplit = packageName.replace('\\', '/').split('/')
  180. newPath = basePath
  181. for item in packageNameSplit:
  182. newPath = os.path.join(newPath, item)
  183. return newPath
  184. def getMappingSdk(config):
  185. return config['sdk']
  186. def getScriptMapping(sdk):
  187. return sdk
  188. #packageWeb()