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 os 18import shutil 19import argparse 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 write_json_file, read_json_file # noqa: E402 E501 25from scripts.util import build_utils # noqa: E402 26 27 28def _get_src_sa_info(src_sa_install_info_file, depfiles): 29 src_sa_install_info = read_json_file(src_sa_install_info_file) 30 if src_sa_install_info is None: 31 raise Exception("read src_sa_install_info_file failed.") 32 install_info_file_dict = {} 33 for _install_info in src_sa_install_info: 34 if _install_info.get('type') == 'sa': 35 part_name = _install_info.get('part_name') 36 _install_infos = install_info_file_dict.get(part_name) 37 if _install_infos is None: 38 _install_infos = [] 39 _install_infos.append(_install_info.get('install_info_file')) 40 install_info_file_dict[part_name] = _install_infos 41 42 all_sa_info_files_dict = {} 43 for part_name, _install_info_files in install_info_file_dict.items(): 44 for _install_info_file in _install_info_files: 45 _install_info = read_json_file(_install_info_file) 46 if _install_info is None: 47 raise Exception("read install_info_file '{}' failed.".format( 48 _install_info_file)) 49 depfiles.append(_install_info_file) 50 sa_info_files = _install_info.get('sa_info_files') 51 _file_list = all_sa_info_files_dict.get(part_name) 52 if _file_list is None: 53 all_sa_info_files_dict[part_name] = sa_info_files 54 else: 55 _file_list.extend(sa_info_files) 56 all_sa_info_files_dict[part_name] = _file_list 57 return all_sa_info_files_dict 58 59 60def _file_archive(all_sa_info_files_dict, archive_output_dir, 61 archive_info_file, depfiles): 62 info_dict = {} 63 _info_file_dir = os.path.dirname(archive_info_file) 64 _relative_path = os.path.relpath(_info_file_dir, archive_output_dir) 65 for key, value in all_sa_info_files_dict.items(): 66 subsystem_out_dir = os.path.join(archive_output_dir, key) 67 if not os.path.exists(subsystem_out_dir): 68 os.makedirs(subsystem_out_dir, exist_ok=True) 69 _file_list = [] 70 for _file in value: 71 depfiles.append(_file) 72 shutil.copy2(_file, subsystem_out_dir) 73 _file_list.append( 74 os.path.join(_relative_path, key, os.path.basename(_file))) 75 info_dict[key] = _file_list 76 write_json_file(archive_info_file, info_dict) 77 78 79def main(): 80 parser = argparse.ArgumentParser() 81 parser.add_argument('--src-sa-install-info-file', required=True) 82 parser.add_argument('--sa-archive-output-dir', required=True) 83 parser.add_argument('--sa-archive-info-file', required=True) 84 parser.add_argument('--depfile', required=True) 85 args = parser.parse_args() 86 87 depfiles = [] 88 all_sa_info_files_dict = _get_src_sa_info(args.src_sa_install_info_file, 89 depfiles) 90 91 _file_archive(all_sa_info_files_dict, args.sa_archive_output_dir, 92 args.sa_archive_info_file, depfiles) 93 build_utils.write_depfile(args.depfile, args.sa_archive_info_file, 94 depfiles) 95 return 0 96 97 98if __name__ == '__main__': 99 sys.exit(main()) 100