package_web_shanshen.py 6.1 KB

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