1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2022-2024 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15""" 16Archives a set of files. 17""" 18 19 20import os 21import sys 22import argparse 23import subprocess 24import re 25 26 27def run_cmd(title, cmd, time_out): 28 child_process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 29 cmd_out, cmd_err = child_process.communicate(timeout=time_out) 30 if child_process.returncode != 0: 31 print(cmd_out.decode('utf-8')) 32 print(cmd_err.decode('utf-8')) 33 raise Exception('compile module: ' + title + ' failed!') 34 35 36def get_compile_str(src_path, java_sources): 37 compile_java = '' 38 for src in java_sources: 39 if len(compile_java) == 0: 40 compile_java = '{}'.format(os.path.join(src_path, src)) 41 else: 42 compile_java = '{} {}'.format(compile_java, os.path.join(src_path, src)) 43 return compile_java 44 45 46def compile_haptobin_tool(root_path, src_path, jar_output, out_path, 47 toolchain, fastjson_jar): 48 java_sources = [ 49 'BinaryTool.java', 50 'BundleException.java', 51 'ConvertHapToBin.java', 52 'FileUtils.java', 53 'Log.java', 54 'PackFormatter.java', 55 'Utility.java' 56 ] 57 compile_java = get_compile_str(src_path, java_sources) 58 59 shell_path = os.path.join(root_path, "haptobin.sh") 60 command = ['bash', shell_path, root_path, jar_output, out_path, 61 toolchain, compile_java, fastjson_jar] 62 print('command: ', command) 63 run_cmd('haptobin_tool', command, 5000) 64 return 65 66 67def compile_unpacking_tool(root_path, src_path, jar_output, out_path, big_version, 68 toolchain, fastjson_jar): 69 java_sources = [ 70 'AbilityFormInfo.java', 'AbilityInfo.java', 'ApiVersion.java', 71 'AppInfo.java', 'AppModel.java', 'APPQFResult.java', 72 'BundleException.java', 'CommandParser.java', 'CommonEvent.java', 73 'CountryCode.java', 'CustomizeData.java', 'DefinePermission.java', 74 'DefPermissionGroup.java', 'DefPermission.java', 'DependencyItem.java', 75 'DeviceConfig.java', 'DistroFilter.java', 'Distro.java', 76 'ExtensionAbilityInfo.java', 'FileUtils.java', 'FormInfo.java', 77 'HapInfo.java', 'HapZipInfo.java', 'HQFInfo.java', 78 'IntentInfo.java', 'JsInfo.java', 'JsonUtil.java', 79 'Log.java', 'MetaDataInfo.java', 'MetaData.java', 80 'ModuleAbilityInfo.java', 'ModuleAdaption.java', 'ModuleAppInfo.java', 81 'ModuleAtomicService.java', 'ModuleDeviceType.java', 'ModuleInfo.java', 82 'ModuleMetadataInfo.java', 'ModuleProfileInfo.java', 'ModuleResult.java', 83 'ModuleShortcut.java', 'PackFormatter.java', 'PackInfo.java', 84 'PreloadItem.java', 'ProfileInfo.java', 'ReqPermission.java', 85 'ResourceIndexResult.java', 'ResourcesParser.java', 'ScreenDensity.java', 86 'ScreenShape.java', 'ScreenWindow.java', 'Shortcut.java', 87 'ShowHelp.java', 'SkillInfo.java', 'UncompressEntrance.java', 88 'Uncompress.java', 'UncompressResult.java', 'UncompressVerify.java', 89 'UriInfo.java', 'UsedScene.java', 'Utility.java', 'Want.java' 90 ] 91 compile_java = get_compile_str(src_path, java_sources) 92 93 shell_path = os.path.join(root_path, "unpackingTool.sh") 94 command = ['bash', shell_path, root_path, jar_output, out_path, big_version, 95 toolchain, compile_java, fastjson_jar] 96 print('command: ', command) 97 run_cmd('app_unpacking_tool', command, 5000) 98 return 99 100 101def compile_packing_tool(root_path, src_path, jar_output, out_path, 102 toolchain, fastjson_jar, compress_jar): 103 java_sources = [ 104 'ApiVersion.java', 105 'BundleException.java', 106 'CommandParser.java', 107 'CompressEntrance.java', 108 'Compressor.java', 109 'CompressVerify.java', 110 'CountryCode.java', 111 'DependencyItem.java', 112 'DistroFilter.java', 113 'FileUtils.java', 114 'HapVerify.java', 115 'HapVerifyInfo.java', 116 'HQFInfo.java', 117 'HQFVerify.java', 118 'Log.java', 119 'ModuleApiVersion.java', 120 'ModuleJsonUtil.java', 121 'ModuleMetadataInfo.java', 122 'MultiAppMode.java', 123 'PackageNormalize.java', 124 'PackFormatter.java', 125 'PreloadItem.java', 126 'ScreenDensity.java', 127 'ScreenShape.java', 128 'ScreenWindow.java', 129 'ShowHelp.java', 130 'Utility.java', 131 'VerifyCollection.java', 132 'Version.java' 133 ] 134 compile_java = get_compile_str(src_path, java_sources) 135 136 shell_path = os.path.join(root_path, "packingTool.sh") 137 command = ['bash', shell_path, root_path, jar_output, out_path, 138 toolchain, compile_java, fastjson_jar, compress_jar] 139 print('command: ', command) 140 run_cmd('app_packing_tool', command, 5000) 141 return 142 143 144def compile_check_tool(root_path, src_path, jar_output, out_path, 145 toolchain, fastjson_jar): 146 java_sources = [ 147 'BundleException.java', 148 'CommandParser.java', 149 'FileUtils.java', 150 'Log.java', 151 'PackFormatter.java', 152 'Scan.java', 153 'ScanEntrance.java', 154 'ScanErrorEnum.java', 155 'ScanStatDuplicate.java', 156 'ScanStatFileSize.java', 157 'ScanStatSuffix.java', 158 'ScanVerify.java', 159 'ShowHelp.java', 160 'Utility.java' 161 ] 162 compile_java = get_compile_str(src_path, java_sources) 163 164 shell_path = os.path.join(root_path, "checkTool.sh") 165 command = ['bash', shell_path, root_path, jar_output, out_path, 166 toolchain, compile_java, fastjson_jar] 167 print('command: ', command) 168 run_cmd('app_check_tool', command, 5000) 169 return 170 171 172def main(): 173 parser = argparse.ArgumentParser() 174 parser.add_argument('--haptobin', required=True) 175 parser.add_argument('--haptobinOutput', required=True) 176 parser.add_argument('--unpackOutput', required=True) 177 parser.add_argument('--packOutput', required=True) 178 parser.add_argument('--checkOutput', required=True) 179 parser.add_argument('--outpath', required=True) 180 parser.add_argument('--toolchain', required=True) 181 parser.add_argument('--compileTarget', required=True) 182 args = parser.parse_args() 183 print('packingTool args: ', args) 184 root_dir = os.path.dirname(os.path.realpath(__file__)) 185 src_dir = os.path.join(root_dir, "./adapter/ohos/") 186 toolchain = args.toolchain 187 tool_list = toolchain.split(':') 188 toolchain = tool_list[-1] 189 toolchain += "_" + args.compileTarget 190 191 fastjson_jar = os.path.join(root_dir, '../../prebuilts/packing_tool/fastjson-1.2.83.jar') 192 compress_jar = os.path.join(root_dir, '../../prebuilts/packing_tool/commons-compress-1.24.0.jar') 193 print('packingTool: ', toolchain, fastjson_jar, compress_jar) 194 195 # compile haptobin_tool.jar 196 compile_haptobin_tool(root_dir, src_dir, args.haptobinOutput, args.outpath, 197 toolchain, fastjson_jar) 198 199 # compile app_unpacking_tool.jar 200 version = subprocess.check_output(['javac', '-version'], stderr=subprocess.STDOUT) 201 version = version.decode('utf-8') 202 array = re.findall(r'\d+', version) 203 compatible_version = 8 204 big_version = '' 205 if int(array[0]) > compatible_version: 206 big_version = 'true' 207 else: 208 big_version = 'false' 209 compile_unpacking_tool(root_dir, src_dir, args.unpackOutput, args.outpath, big_version, 210 toolchain, fastjson_jar) 211 212 #compile app_packing_tool.jar 213 compile_packing_tool(root_dir, src_dir, args.packOutput, args.outpath, 214 toolchain, fastjson_jar, compress_jar) 215 216 #compile app_check_tool.jar 217 compile_check_tool(root_dir, src_dir, args.checkOutput, args.outpath, 218 toolchain, fastjson_jar) 219 220if __name__ == '__main__': 221 sys.exit(main()) 222