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 20import re 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_label_name(label): 29 return re.split('[:|()]', label)[1] 30 31 32def _read_notice_info_file(subsystem_notice_info_dir: str, platform_base_dir: str): 33 subsystem_notice_info_list = [] 34 subsystem_notice_files = [] 35 _file_list = os.listdir(subsystem_notice_info_dir) 36 for _info_file in _file_list: 37 _info_file_path = os.path.join(subsystem_notice_info_dir, _info_file) 38 if not os.path.isfile(_info_file_path): 39 continue 40 if not _info_file.endswith('_notice_file'): 41 continue 42 subsystem_notice_info_list.append(_info_file_path) 43 _nf_src_list = read_json_file(_info_file_path) 44 for _nf_src in _nf_src_list: 45 _dest = os.path.join(platform_base_dir, os.path.dirname(_nf_src)) 46 if not os.path.exists(_dest): 47 os.makedirs(_dest, exist_ok=True) 48 shutil.copy2(_nf_src, _dest) 49 _dest_file = os.path.relpath( 50 os.path.join(_dest, os.path.basename(_nf_src))) 51 subsystem_notice_files.append(_dest_file) 52 return subsystem_notice_info_list, subsystem_notice_files 53 54 55def get_notice_info(system_install_info_file: str, notice_info_dir: str, 56 platform_base_dir: str): 57 system_install_info = read_json_file(system_install_info_file) 58 if system_install_info is None: 59 raise Exception( 60 "read file '{}' failed.".format(system_install_info_file)) 61 62 src_subsystem_list = [] 63 for subsystem_info in system_install_info: 64 if subsystem_info.get('is_source') is False: 65 continue 66 part_label = subsystem_info.get('part_label') 67 part_name = _get_label_name(part_label) 68 src_subsystem_list.append(part_name) 69 70 notice_info_file_list = [] 71 system_notice_files = [] 72 for subsystem_name in src_subsystem_list: 73 subsystem_notice_info_dir = os.path.join(notice_info_dir, 74 subsystem_name) 75 if not os.path.exists(subsystem_notice_info_dir): 76 continue 77 subsystem_notice_info_list, subsystem_notice_files = _read_notice_info_file( 78 subsystem_notice_info_dir, platform_base_dir) 79 notice_info_file_list.extend(subsystem_notice_info_list) 80 system_notice_files.extend(subsystem_notice_files) 81 return notice_info_file_list, system_notice_files 82 83 84def main(): 85 parser = argparse.ArgumentParser() 86 parser.add_argument('--system-install-info-file', required=True) 87 parser.add_argument('--notice-file-info-dir', required=True) 88 parser.add_argument('--platform-base-dir', required=True) 89 parser.add_argument('--output-file', required=True) 90 parser.add_argument('--depfile', required=False) 91 args = parser.parse_args() 92 93 _dep_files = [args.system_install_info_file] 94 notice_info_file_list, system_notice_files = get_notice_info( 95 args.system_install_info_file, args.notice_file_info_dir, 96 args.platform_base_dir) 97 98 _dep_files.extend(notice_info_file_list) 99 write_json_file(args.output_file, system_notice_files) 100 101 if args.depfile: 102 list.sort(_dep_files) 103 build_utils.write_depfile(args.depfile, 104 args.output_file, 105 _dep_files, 106 add_pydeps=False) 107 return 0 108 109 110if __name__ == '__main__': 111 sys.exit(main()) 112