package_web_shanshen.py 6.4 KB

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