import importlib import json import os.path import sys import common_utils import file_utils import package_utils sdk_mapping = { 'jmsdk': 'jm', 'qingshi': 'qingshi' } script_mapping = { 'jmsdk': 'jm', 'qingshi': 'qingshi' } def package_test(package_config): # 打包配置的路径 json_text = file_utils.read_file(package_config) config = json.loads(json_text) # 重构sdk判断 if 'refactorSdk' in config and toBoolean(config['refactorSdk']): print('重构sdk特殊处理...') config['sdk'] = 'qingshi' properties = config['properties'] if 'version' in properties: properties['QS_PACKAGE_VERSION'] = properties.pop('version') if 'agent' in properties: properties['QS_CAMPAIGN_ID'] = properties.pop('agent') if 'appid' in properties: properties['QS_APP_ID'] = properties.pop('appid') if 'appkey' in properties: properties['QS_APP_KEY'] = properties.pop('appkey') if 'host' in properties: properties['QS_ONLINE_ENV'] = properties.pop('host') if 'testGcpCode' in config: properties['QS_GCP_ID'] = config['testGcpCode'] print('*************config*****************') print(json.JSONEncoder().encode(config)) print('************************************') sdk = get_mapping_sdk(config) package(config, sdk) def packageWeb(): if len(sys.argv) < 2: print('argument is missing') return 1 # 打包配置的路径 packageConfig = sys.argv[1] json_text = file_utils.read_file(packageConfig) config = json.loads(json_text) # 重构sdk判断 if 'refactorSdk' in config and toBoolean(config['refactorSdk']): print('重构sdk特殊处理...') config['sdk'] = 'qingshi' properties = config['properties'] if 'version' in properties: properties['QS_PACKAGE_VERSION'] = properties.pop('version') if 'agent' in properties: properties['QS_CAMPAIGN_ID'] = properties.pop('agent') if 'appid' in properties: properties['QS_APP_ID'] = properties.pop('appid') if 'appkey' in properties: properties['QS_APP_KEY'] = properties.pop('appkey') if 'host' in properties: properties['QS_ONLINE_ENV'] = properties.pop('host') if 'testGcpCode' in config: properties['QS_GCP_ID'] = config['testGcpCode'] print('*************config*****************') print(json.JSONEncoder().encode(config)) print('************************************') sdk = get_mapping_sdk(config) 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.get_random_number(6) outName = config['random'] channelPath = file_utils.getChannelPath(game, outName, sdk) subChannelPath = os.path.join(channelPath, subChannel) print('打包开始,删除缓存目录 %s ' % subChannelPath) file_utils.delete_folder(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']) if 'refactorSdk' in config and toBoolean(config['refactorSdk']): jsonConfig['refactorSdk'] = toBoolean(config['refactorSdk']) # 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相关配置 get_sdk_config(sdk, jsonConfig, config) # 生成配置文件 create_or_update_config_file(game, sdk, jsonConfig) # 拷贝资源 copy_res(game, sdk, subChannel, config) # 打包 package_utils.packConsole(game, sdk, config, subChannel) def toBoolean(bool_str): if type(bool_str) == bool: return bool_str if bool_str == 'true': return True return False def get_sdk_config(sdk, json_config, config): script_path = os.path.join(file_utils.get_current_path(), 'sdk_script') sdk_script = get_script_mapping(sdk) target_script = os.path.join(script_path, '%s.py' % sdk_script) if not os.path.exists(target_script): print('%s no exists' % target_script) return 0 print('sdk_script -- > %s' % target_script) sys.path.append(script_path) module = importlib.import_module(sdk_script) # 动态导入相应模块 module.get_sdk_config(json_config, config) # 执行脚本功能 sys.path.remove(script_path) def create_or_update_config_file(game, sdk, json_config): """ 更新配置文件 """ if 'subChannel' not in json_config: return 0 print('createOrUpdateConfigFile ...') random = json_config['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.read_file(configPath) config = json.loads(jsonText) count = 0 if type(config) == list: for item in config: if item['subChannel'] == json_config['subChannel']: print('find same config ...') del config[count] break count += 1 config.append(json_config) createConfigFile(config, configPath) elif type(config) == dict: if config['subChannel'] == json_config['subChannel']: print('find same config ...') createConfigFile(json_config, configPath) else: print('add a new config ...') config = [config, json_config] createConfigFile(config, configPath) else: print('create a new config ...') createConfigFile([json_config], configPath) def createConfigFile(json_config, config_path): """ 创建配置文件 """ jsonStr = json.dumps(json_config, ensure_ascii=False) print('*************out config*************') print(jsonStr) print('************************************') file_utils.create_file(config_path, jsonStr) def copy_res(game, sdk, sub_channel, config): """ 拷贝资源 """ if sub_channel is None: return 0 random = config['random'] channelPath = file_utils.getChannelPath(game, random, sdk) subChannelPath = os.path.join(channelPath, sub_channel) print('channel_path: %s' % channelPath) print('sub_channel_path: %s' % subChannelPath) 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.copy_file(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.copy_file(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 = get_package_path(subChannelPath, toFile) file_utils.copy_file(item['fromFile'], resPath) if 'package' in config and os.path.exists(config['package']): newGameApk = config['package'] gameApk = file_utils.getFullGameApk(game) cache_game_apk = file_utils.get_cache_game_apk(game, random, sdk) file_utils.copy_file(newGameApk, cache_game_apk) return 1 def get_package_path(base_path, package_name): """ 包名对应的目录 """ package_name_split = package_name.replace('\\', '/').split('/') newPath = base_path for item in package_name_split: newPath = os.path.join(newPath, item) return newPath def get_mapping_sdk(config): sdk = config['sdk'] mapping = sdk if sdk in sdk_mapping: mapping = sdk_mapping[sdk] else: return 'jm_' + sdk if type(mapping) == dict: sdk_config = config[sdk] if 'SUB_CHANNEL' in sdk_config and sdk_config['SUB_CHANNEL'] != '': sub_channel = sdk_config['SUB_CHANNEL'] else: sub_channel = 'default' return mapping[sub_channel] else: return mapping def get_script_mapping(sdk): if sdk in script_mapping: return script_mapping[sdk] return sdk # packageWeb() if __name__ == '__main__': package_test('/Users/suyghur/Develop/yyxx/qs/test_config/test.json')