package_web_record.py 7.3 KB

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