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 20import zipfile 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 25from scripts.util import build_utils # noqa: E402 26 27 28def get_no_src_parts(system_install_info_file: str): 29 install_parts_info = read_json_file(system_install_info_file) 30 if install_parts_info is None: 31 raise Exception("read no src subsystem info file failed.") 32 parts_name_list = [] 33 for info in install_parts_info: 34 if 'is_source' not in info: 35 raise Exception( 36 "read subsystem info error, missing key is_source.") 37 if info.get('is_source') is True: 38 continue 39 if 'part_name' not in info: 40 raise Exception("read part info error, missing key part_name.") 41 42 part_name = info.get('part_name') 43 parts_name_list.append(part_name) 44 return parts_name_list 45 46 47def generate_binary_sa_archive(parts_list: list, sa_profile_archive_info_file: str, 48 sa_output_dir: str, depfiles: list): 49 if not os.path.exists(sa_output_dir): 50 os.makedirs(sa_output_dir, exist_ok=True) 51 sa_tuples = [] 52 subsystem_sa_archive_dir = os.path.dirname(sa_profile_archive_info_file) 53 if not os.path.exists(subsystem_sa_archive_dir): 54 return sa_tuples 55 if not os.path.exists(sa_profile_archive_info_file): 56 print("warning: sa profile archive info file does not exist.") 57 return sa_tuples 58 59 sa_profile_archive_info = read_json_file(sa_profile_archive_info_file) 60 if sa_profile_archive_info is None: 61 raise Exception("read sa profile archive info file failed.") 62 63 for _part_name in parts_list: 64 _sa_file_list = sa_profile_archive_info.get(_part_name) 65 if _sa_file_list is None: 66 continue 67 for _sa_file in _sa_file_list: 68 _sa_file_path = os.path.join(subsystem_sa_archive_dir, _sa_file) 69 sa_tuples.append((_sa_file_path, 70 os.path.relpath(_sa_file_path, 71 subsystem_sa_archive_dir))) 72 depfiles.append(_sa_file_path) 73 return sa_tuples 74 75 76def main(): 77 parser = argparse.ArgumentParser() 78 parser.add_argument('--system-install-info-file', required=True) 79 parser.add_argument('--sa-profile-archive-info-file', required=False) 80 parser.add_argument('--sa-output-dir', required=True) 81 parser.add_argument('--sa-output-zipfile', required=True) 82 parser.add_argument('--depfile', required=True) 83 args = parser.parse_args() 84 85 sa_files_list = [] 86 depfiles = [] 87 sa_files_tuples = [] 88 if args.sa_profile_archive_info_file: 89 depfiles.append(args.sa_profile_archive_info_file) 90 parts_list = get_no_src_parts(args.system_install_info_file) 91 sa_files_tuples.extend( 92 generate_binary_sa_archive(parts_list, 93 args.sa_profile_archive_info_file, 94 args.sa_output_dir, depfiles)) 95 with zipfile.ZipFile(args.sa_output_zipfile, 'w') as outfile: 96 for sa_file_path, sa_file in sa_files_tuples: 97 build_utils.add_to_zip_hermetic(outfile, 98 sa_file, 99 src_path=sa_file_path) 100 build_utils.write_depfile(args.depfile, args.sa_output_zipfile, depfiles) 101 return 0 102 103 104if __name__ == '__main__': 105 sys.exit(main()) 106