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 json 21import glob 22sys.path.append( 23 os.path.dirname(os.path.dirname(os.path.dirname( 24 os.path.abspath(__file__))))) 25from scripts.util import build_utils # noqa: E402 26 27 28def generate_notice_files(dest_dir, module_info, depfiles): 29 dest = os.path.join(dest_dir, "{}.txt".format(module_info['dest'][0])) 30 target_dir = os.path.join(dest_dir, module_info['dest'][0]) 31 module_source = module_info['source'] 32 if os.path.isdir(module_source): 33 dest_files = [] 34 is_hvigor_hap = False 35 for filename in os.listdir(module_source): 36 if filename.endswith(".hap") or filename.endswith(".hsp"): 37 is_hvigor_hap = True 38 dest_files.append(f"{os.path.join(target_dir, filename)}.txt") 39 if not is_hvigor_hap and os.listdir(module_source): 40 dest_files.extend([f"{os.path.join(target_dir, f)}.txt" for f in os.listdir(module_source)]) 41 else: 42 dest_files = [dest] 43 for dest_file in dest_files: 44 os.makedirs(os.path.dirname(dest_file), exist_ok=True) 45 shutil.copyfile(module_info['notice'], dest_file) 46 depfiles.append(module_info['notice']) 47 if os.path.isfile("{}.json".format(module_info['notice'])): 48 os.makedirs(os.path.dirname("{}.json".format(dest_file)), exist_ok=True) 49 shutil.copyfile("{}.json".format(module_info['notice']), "{}.json".format(dest_file)) 50 51 52def collect_notice_files(options, dest_dir: str, depfiles: str): 53 subsystem_info_files = [] 54 with open(options.install_info_file, 'r') as file: 55 install_info = json.load(file) 56 for item in install_info: 57 subsystem_info_files.append(item['part_info_file']) 58 depfiles.extend(subsystem_info_files) 59 60 module_info_files = [] 61 for subsystem_info in subsystem_info_files: 62 with open(subsystem_info, 'r') as file: 63 subsystem_info = json.load(file) 64 for item in subsystem_info: 65 module_info_files.append(item['module_info_file']) 66 depfiles.extend(module_info_files) 67 68 for module_info in module_info_files: 69 with open(module_info, 'r') as file: 70 module_info = json.load(file) 71 if 'notice' in module_info and module_info[ 72 'type'] != "java_library": 73 notice_file = module_info['notice'] 74 if os.path.exists(notice_file) is False or os.stat( 75 notice_file).st_size == 0: 76 continue 77 generate_notice_files(dest_dir, module_info, depfiles) 78 79 notice_files = build_utils.get_all_files(options.notice_root_dir) 80 depfiles.extend(notice_files) 81 for file in notice_files: 82 dest = os.path.join(dest_dir, 83 os.path.relpath(file, options.notice_root_dir)) 84 os.makedirs(os.path.dirname(dest), exist_ok=True) 85 shutil.copyfile(file, dest) 86 if os.path.isfile("{}.json".format(file)): 87 os.makedirs(os.path.dirname("{}.json".format(dest)), exist_ok=True) 88 shutil.copyfile("{}.json".format(file), "{}.json".format(dest)) 89 90 91def main(): 92 parser = argparse.ArgumentParser() 93 parser.add_argument('--install-info-file', required=True) 94 parser.add_argument('--notice-root-dir', required=True) 95 parser.add_argument('--output-file', required=True) 96 parser.add_argument('--depfile', required=False) 97 args = parser.parse_args() 98 99 depfiles = [args.install_info_file] 100 with build_utils.temp_dir() as tmp: 101 collect_notice_files(args, tmp, depfiles) 102 build_utils.zip_dir(args.output_file, tmp) 103 104 if args.depfile: 105 build_utils.write_depfile(args.depfile, 106 args.output_file, 107 sorted(depfiles), 108 add_pydeps=False) 109 return 0 110 111 112if __name__ == '__main__': 113 sys.exit(main()) 114