1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 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 16import sys 17import argparse 18import os 19import shutil 20 21sys.path.append( 22 os.path.dirname(os.path.dirname(os.path.dirname( 23 os.path.abspath(__file__))))) 24from scripts.util.file_utils import read_json_file, write_json_file # noqa: E402 25 26 27def get_src_parts(system_install_info_file): 28 parts_info_list = read_json_file(system_install_info_file) 29 if parts_info_list is None: 30 raise Exception( 31 "read file '{}' failed.".format(system_install_info_file)) 32 src_parts_info = {} 33 for part_info in parts_info_list: 34 if 'is_source' not in part_info: 35 raise Exception("read part info error, missing key is_source.") 36 if part_info.get('is_source') is True: 37 part_name = part_info.get('part_name') 38 src_parts_info[part_name] = part_info 39 return src_parts_info 40 41 42def _collect_testcase(resources_dir, parts_info, output_base_dir): 43 for part_name, part_info in parts_info.items(): 44 origin_part_name = part_info.get('origin_part_name') 45 if part_info.get('variant_name') != 'phone': 46 _toolchain = part_info.get('toolchain_label') 47 _toolchain_name = _toolchain.split(':')[1] 48 _variant_dir = os.path.join(_toolchain_name, resources_dir, 49 origin_part_name) 50 else: 51 _variant_dir = os.path.join(resources_dir, part_name) 52 if not os.path.exists(_variant_dir): 53 continue 54 55 _src_dir_name = os.path.basename(resources_dir) 56 package_output_dir = os.path.join(output_base_dir, _src_dir_name) 57 dest_dir = os.path.join(package_output_dir, origin_part_name) 58 if os.path.exists(dest_dir): 59 shutil.rmtree(dest_dir) 60 shutil.copytree(_variant_dir, dest_dir) 61 62 63def collect_testcase(resources_dir_list, parts_info, output_base_dir): 64 for resources_dir in resources_dir_list: 65 _collect_testcase(resources_dir, parts_info, output_base_dir) 66 67 68def _collect_test_mlf(resources_dir, parts_info, output_base_dir): 69 for part_name, part_info in parts_info.items(): 70 origin_part_name = part_info.get('origin_part_name') 71 if part_info.get('variant_name') != 'phone': 72 _toolchain = part_info.get('toolchain_label') 73 _toolchain_name = _toolchain.split(':')[1] 74 _variant_dir = os.path.join(_toolchain_name, resources_dir, 75 origin_part_name) 76 else: 77 _variant_dir = os.path.join(resources_dir, part_name) 78 if not os.path.exists(_variant_dir): 79 continue 80 81 _src_dir_name = os.path.basename(resources_dir) 82 package_output_dir = os.path.join(output_base_dir, _src_dir_name) 83 dest_dir = os.path.join(package_output_dir, origin_part_name) 84 if os.path.exists(dest_dir): 85 shutil.rmtree(dest_dir) 86 shutil.copytree(_variant_dir, dest_dir) 87 88 89def collect_test_mlf(resources_dir_list, parts_info, output_base_dir): 90 for resources_dir in resources_dir_list: 91 _collect_test_mlf(resources_dir, parts_info, output_base_dir) 92 93 94def _collect_libs_symbols(resources_dir, parts_info, package_output_base_dir): 95 for part_name, part_info in parts_info.items(): 96 origin_part_name = part_info.get('origin_part_name') 97 if part_info.get('variant_name') != 'phone': 98 _toolchain = part_info.get('toolchain_label') 99 _toolchain_name = _toolchain.split(':')[1] 100 _variant_dir = os.path.join(_toolchain_name, resources_dir, 101 _toolchain_name, 102 part_info.get('subsystem_name'), 103 origin_part_name) 104 else: 105 _variant_dir = os.path.join(resources_dir, 106 part_info.get('subsystem_name'), 107 part_name) 108 if not os.path.exists(_variant_dir): 109 continue 110 111 _src_dir_name = os.path.basename(resources_dir) 112 package_output_dir = os.path.join(package_output_base_dir, 113 _src_dir_name) 114 dest_dir = os.path.join(package_output_dir, origin_part_name) 115 if os.path.exists(dest_dir): 116 shutil.rmtree(dest_dir) 117 shutil.copytree(_variant_dir, dest_dir) 118 119 120def collect_libs_symbols(resources_dir_list, parts_info, output_base_dir): 121 for resources_dir in resources_dir_list: 122 _collect_libs_symbols(resources_dir, parts_info, output_base_dir) 123 124 125def _collect_libs_java(resources_dir, parts_info, output_base_dir): 126 parts_libs_java_path = {} 127 for part_name, part_info in parts_info.items(): 128 origin_part_name = part_info.get('origin_part_name') 129 if part_info.get('variant_name') != 'phone': 130 _toolchain = part_info.get('toolchain_label') 131 _toolchain_name = _toolchain.split(':')[1] 132 _variant_dir = os.path.join(_toolchain_name, resources_dir, 133 part_info.get('subsystem_name'), 134 origin_part_name) 135 else: 136 _variant_dir = os.path.join(resources_dir, 137 part_info.get('subsystem_name'), 138 part_name) 139 if not os.path.exists(_variant_dir): 140 continue 141 142 parts_libs_java_path[origin_part_name] = _variant_dir 143 _libs_java_path_info_file = os.path.join(output_base_dir, 144 'libs_java_info.json') 145 write_json_file(_libs_java_path_info_file, parts_libs_java_path) 146 147 148def collect_libs_java(resources_dir_list, parts_info, output_base_dir): 149 for resources_dir in resources_dir_list: 150 _collect_libs_java(resources_dir, parts_info, output_base_dir) 151 152 153def _collect_kits_modules(resources_dir, parts_info, output_base_dir): 154 parts_modules_path = {} 155 for part_name, part_info in parts_info.items(): 156 part_modules_dir = os.path.join(resources_dir, part_name) 157 if not os.path.exists(part_modules_dir): 158 continue 159 origin_part_name = part_info.get('origin_part_name') 160 parts_modules_path[origin_part_name] = part_modules_dir 161 _modules_path_info_file = os.path.join(output_base_dir, 162 'kits_modules_info.json') 163 write_json_file(_modules_path_info_file, parts_modules_path) 164 165 166def collect_kits_modules(resources_dir_list, parts_info, output_base_dir): 167 for resources_dir in resources_dir_list: 168 _collect_kits_modules(resources_dir, parts_info, output_base_dir) 169 170 171def collect_innerkits_modules(resources_dir_list, parts_info, output_base_dir): 172 for resources_dir in resources_dir_list: 173 _collect_kits_modules(resources_dir, parts_info, output_base_dir) 174 175 176def collect_resources(collect_type, resources_dir_list, parts_info, 177 output_base_dir): 178 if collect_type == 'libs_symbols': 179 collect_libs_symbols(resources_dir_list, parts_info, output_base_dir) 180 elif collect_type == 'module_list_files': 181 collect_test_mlf(resources_dir_list, parts_info, output_base_dir) 182 elif collect_type == 'testcase': 183 collect_testcase(resources_dir_list, parts_info, output_base_dir) 184 elif collect_type == 'kits_modules': 185 collect_kits_modules(resources_dir_list, parts_info, output_base_dir) 186 elif collect_type == 'innerkits_modules': 187 collect_innerkits_modules(resources_dir_list, parts_info, 188 output_base_dir) 189 elif collect_type == 'libs_java': 190 collect_libs_java(resources_dir_list, parts_info, output_base_dir) 191 else: 192 raise Exception("Unsupported resource type.") 193 194 195def main(): 196 parser = argparse.ArgumentParser() 197 parser.add_argument('--collect-type', required=True) 198 parser.add_argument('--system-install-info-file', required=True) 199 parser.add_argument('--resources-dir-list', nargs='+', default=[]) 200 parser.add_argument('--package-output-base-dir', required=True) 201 parser.add_argument('--output-file', required=True) 202 args = parser.parse_args() 203 204 parts_info = get_src_parts(args.system_install_info_file) 205 206 collect_resources(args.collect_type, args.resources_dir_list, parts_info, 207 args.package_output_base_dir) 208 write_json_file(args.output_file, '{}') 209 return 0 210 211 212if __name__ == '__main__': 213 sys.exit(main()) 214