123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- import file_utils
- import package_utils
- import package_web_record
- import package_web_shanshen
- import package_web_yfsdk
- import os.path
- import sys
- import json
- import importlib
- import common_utils
- sdkMapping = {
- 'ysdk': 'jm_ysdk',
- 'ziyun': {
- 'default': 'jm_zy',
- 'ysdk': 'jm_zy_ysdk',
- },
- 'xunqu': {
- 'default': 'jm_xq',
- 'xiaomi': 'jm_xq_mi',
- 'jrtt': 'jm_xq_jrtt'
- },
- 'jingqi': 'jm_jq',
- 'qytx': {
- 'default': 'jm_qytx',
- 'cangyu': 'jm_qytx2',
- 'cangyu_new': 'jm_cangyu'
- },
- 'jmsdk': 'jm',
- 'oppo': 'jm_oppo',
- 'quick': 'jm_quick',
- 'beiyu': 'jm_beiyu',
- 'yijie': 'jm_yijie',
- 'gzjysdk': 'gzjysdk',
- 'record': 'record',
- 'float': 'float',
- 'yfsdk': 'yfsdk'
- }
- scriptMapping = {
- 'jm_xq_mi': 'jm_xq',
- 'jm_xq_jrtt': 'jm_xq',
- 'jm_qytx2': 'jm_qytx',
- 'jm_cangyu': 'jm_qytx'
- }
- newSdk = ["shanshen", "gzjysdk"]
- recordSdk = ["record", "float"]
- yanfaSdk = ["yfsdk"] # 研发sdk打包
- def packageWeb():
- if len(sys.argv) < 2:
- print('argument is missing')
- return 1
- # 打包配置的路径
- packageConfig = sys.argv[1]
- jsonText = file_utils.readFile(packageConfig)
- config = json.loads(jsonText)
- print('*************config*****************')
- print(jsonText)
- print('************************************')
- sdk = getMappingSdk(config)
- if sdk in recordSdk:
- package_web_record.package(config, sdk)
- elif sdk in newSdk:
- package_web_shanshen.package(config, sdk)
- elif sdk in yanfaSdk:
- package_web_yfsdk.package(config, sdk)
- else:
- package(config, sdk)
- def package(config, sdk):
- print('use script 1st')
- jsonConfig = {}
- game = config['app']
- subChannel = None
- if 'subChannel' in config:
- subChannel = config['subChannel']
- jsonConfig['subChannel'] = config['subChannel']
- config['random'] = common_utils.getRandomNumber(6)
- outName = config['random']
- channelPath = file_utils.getChannelPath(game, outName, sdk)
- subChannelPath = os.path.join(channelPath, subChannel)
- print('打包开始,删除缓存目录 %s ' % subChannelPath)
- file_utils.deleteFolder(subChannelPath)
- # 必须参数
- jsonConfig['packageName'] = config['packageName']
- jsonConfig['name'] = config['name']
- jsonConfig['random'] = config['random']
- # 可选参数
- if 'outName' in config:
- jsonConfig['outName'] = config['outName']
- if 'outPath' in config:
- jsonConfig['outPath'] = config['outPath']
- if 'changeIcon' in config:
- jsonConfig['changeIcon'] = toBoolean(config['changeIcon'])
- if 'addLauncher' in config:
- jsonConfig['addLauncher'] = toBoolean(config['addLauncher'])
- if 'versionCode' in config:
- jsonConfig['versionCode'] = config['versionCode']
- if 'versionName' in config:
- jsonConfig['versionName'] = config['versionName']
- if 'targetSdkVersion' in config:
- jsonConfig['targetSdkVersion'] = config['targetSdkVersion']
- if 'v2disable' in config:
- jsonConfig['v2disable'] = toBoolean(config['v2disable'])
- if 'aapt2disable' in config:
- jsonConfig['aapt2disable'] = toBoolean(config['aapt2disable'])
- # sdk相关参数
- if 'properties' in config:
- jsonConfig['properties'] = config['properties']
- jsonConfig['properties']['isDebugMode'] = 'false'
- # jsonConfig['logSdk'] = ['jrtt', 'gdt', 'ks', 'uc']
- # 广点通参数
- if 'gdt_params' in config:
- jsonConfig['logSdk'] = ['gdt']
- gdt = {'gdt': config['gdt_params']}
- if 'configData' in jsonConfig:
- configData = jsonConfig['configData']
- configData['channel_sdk_list'] = gdt
- else:
- jsonConfig['configData'] = {
- 'channel_sdk_list': gdt
- }
- # uc广告参数
- if 'uc_params' in config:
- jsonConfig['logSdk'] = ['uc']
- uc = {'uc': config['uc_params']}
- if 'configData' in jsonConfig:
- configData = jsonConfig['configData']
- configData['channel_sdk_list'] = uc
- else:
- jsonConfig['configData'] = {
- 'channel_sdk_list': uc
- }
- # baidu广告参数
- if 'bd_params' in config:
- jsonConfig['logSdk'] = ['bd']
- bd = {'bd': config['bd_params']}
- if 'configData' in jsonConfig:
- configData = jsonConfig['configData']
- configData['channel_sdk_list'] = bd
- else:
- jsonConfig['configData'] = {
- 'channel_sdk_list': bd
- }
- if 'tt_params' in config:
- jsonConfig['logSdk'] = ['jrtt']
- jrtt = {'jrtt': config['tt_params']}
- if 'configData' in jsonConfig:
- configData = jsonConfig['configData']
- configData['channel_sdk_list'] = jrtt
- else:
- jsonConfig['configData'] = {
- 'channel_sdk_list': jrtt
- }
- if 'ks_params' in config:
- jsonConfig['logSdk'] = ['ks']
- # 获取sdk相关配置
- getSdkConfig(sdk, jsonConfig, config)
- # 生成配置文件
- createOrUpdateConfigFile(game, sdk, jsonConfig)
- # 拷贝资源
- copyRes(game, sdk, subChannel, config)
- # 打包
- package_utils.packConsole(game, sdk, config, subChannel)
- def toBoolean(booleanStr):
- if type(booleanStr) == bool:
- return booleanStr
- if booleanStr == 'true':
- return True
- return False
- def getSdkConfig(sdk, jsonConfig, config):
- scriptPath = os.path.join(file_utils.getCurrentPath(), 'sdk_script')
- sdkScript = getScriptMapping(sdk)
- targetScript = os.path.join(scriptPath, '%s.py' % sdkScript)
- if not os.path.exists(targetScript):
- print('%s no exists' % targetScript)
- return 0
- print('sdk_script -- > %s' % targetScript)
- sys.path.append(scriptPath)
- module = importlib.import_module(sdkScript) # 动态导入相应模块
- module.getSdkConfig(jsonConfig, config) # 执行脚本功能
- sys.path.remove(scriptPath)
- def createOrUpdateConfigFile(game, sdk, jsonConfig):
- '''
- 更新配置文件
- '''
- if 'subChannel' not in jsonConfig:
- return 0
- print('createOrUpdateConfigFile ...')
- random = jsonConfig['random']
- channelPath = file_utils.getChannelPath(game, random, sdk)
- configPath = os.path.join(channelPath, 'config.json')
- # configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
- print("configPath%s" % configPath)
- print("channelPath%s" % channelPath)
- if os.path.exists(configPath):
- # 更新数据
- jsonText = file_utils.readFile(configPath)
- config = json.loads(jsonText)
- count = 0
- if type(config) == list:
- for item in config:
- if item['subChannel'] == jsonConfig['subChannel']:
- print('find same config ...')
- del config[count]
- break
- count += 1
- config.append(jsonConfig)
- createConfigFile(config, configPath)
- elif type(config) == dict:
- if config['subChannel'] == jsonConfig['subChannel']:
- print('find same config ...')
- createConfigFile(jsonConfig, configPath)
- else:
- print('add a new config ...')
- config = [config, jsonConfig]
- createConfigFile(config, configPath)
- else:
- print('create a new config ...')
- createConfigFile([jsonConfig], configPath)
- def createConfigFile(jsonConfig, configPath):
- '''
- 创建配置文件
- '''
- jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
- print('*************out config*************')
- print(jsonStr)
- print('************************************')
- file_utils.createFile(configPath, jsonStr)
- def copyRes(game, sdk, subChannel, config):
- '''
- 拷贝资源
- '''
- if subChannel is None:
- return 0
- random = config['random']
- channelPath = file_utils.getChannelPath(game, random, sdk)
- subChannelPath = os.path.join(channelPath, subChannel)
- if 'icon' in config and os.path.exists(config['icon']):
- mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
- for mipmap in mipmapSupport:
- iconPath = os.path.join(
- subChannelPath, 'icon', mipmap, 'common_sdk_icon.png')
- file_utils.copyFile(config['icon'], iconPath)
- '''if 'switchIcon' in config and os.path.exists(config['switchIcon']):
- mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
- for mipmap in mipmapSupport:
- iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon2.png')
- file_utils.copyFile(config['switchIcon'], iconPath)'''
- if 'splash' in config and os.path.exists(config['splash']):
- splashPath = os.path.join(
- subChannelPath, 'splash', 'drawable-hdpi', 'common_sdk_launcher_bg.jpg')
- file_utils.copyFile(config['splash'], splashPath)
- # print('------subChannelPath 目录清空:%s -------' % subChannelPath)
- # file_utils.deleteFolder(subChannelPath)
- if 'copyList' in config:
- for item in config['copyList']:
- if item['toFile'] == '':
- continue
- if not os.path.exists(item['fromFile']):
- continue
- toFile = item['toFile']
- if toFile[:3] == 'res':
- toFile = 'image' + toFile[3:]
- resPath = getPackagePath(subChannelPath, toFile)
- file_utils.copyFile(item['fromFile'], resPath)
- if 'package' in config and os.path.exists(config['package']):
- newGameApk = config['package']
- gameApk = file_utils.getFullGameApk(game)
- cacheGameApk = file_utils.getCacheGameApk(game, random, sdk)
- file_utils.copyFile(newGameApk, cacheGameApk)
- return 1
- def getPackagePath(basePath, packageName):
- '''
- 包名对应的目录
- '''
- packageNameSplit = packageName.replace('\\', '/').split('/')
- newPath = basePath
- for item in packageNameSplit:
- newPath = os.path.join(newPath, item)
- return newPath
- def getMappingSdk(config):
- sdk = config['sdk']
- mapping = sdk
- if sdk in sdkMapping:
- mapping = sdkMapping[sdk]
- else:
- return 'jm_' + sdk
- if type(mapping) == dict:
- sdkConfig = config[sdk]
- if 'SUB_CHANNEL' in sdkConfig and sdkConfig['SUB_CHANNEL'] != '':
- subChannel = sdkConfig['SUB_CHANNEL']
- else:
- subChannel = 'default'
- return mapping[subChannel]
- else:
- return mapping
- def getScriptMapping(sdk):
- if sdk in scriptMapping:
- return scriptMapping[sdk]
- return sdk
- packageWeb()
|