1#!/usr/bin/env python3 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 21 22def create_dest_file(dest_dir): 23 if not os.path.exists(dest_dir): 24 os.makedirs(dest_dir, exist_ok=True) 25 26 27def get_file_name(target_name, opensource_name): 28 file_name = '' 29 if not opensource_name.strip(): 30 file_name = '{}.txt'.format(target_name) 31 else: 32 file_name = '{}.txt'.format(opensource_name) 33 return file_name 34 35 36def merge_multi_notices(notice_root_dir, 37 module_notices, 38 target_name, 39 opensource_name): 40 41 dest_file = os.path.join(notice_root_dir, 42 get_file_name(target_name, opensource_name)) 43 44 with open(dest_file, 'a') as dest_notice: 45 for notice in module_notices: 46 if os.path.exists(notice): 47 with open(notice, 'r', errors='ignore') as source_notice: 48 for line in source_notice.readlines(): 49 dest_notice.write(line) 50 dest_notice.write(u'\n\n') 51 52 53def copy_notice_file(root_out_dir, 54 module_notices, 55 target_name, 56 opensource_name): 57 nf_dest_dir = os.path.join(root_out_dir, 'NOTICE_FILE/system') 58 create_dest_file(nf_dest_dir) 59 60 # If the module has multi-notices, it need to merge one file. 61 if len(module_notices) > 1: 62 merge_multi_notices(nf_dest_dir, 63 module_notices, 64 target_name, 65 opensource_name) 66 else: 67 for notice in module_notices: 68 if os.path.exists(notice): 69 file_name = get_file_name(target_name, opensource_name) 70 dest_file = os.path.join(nf_dest_dir, file_name) 71 shutil.copy(notice, dest_file) 72 73 74def main(): 75 parser = argparse.ArgumentParser() 76 parser.add_argument('--root-out-dir', help='', required=True) 77 parser.add_argument('--target-name', help='', required=True) 78 parser.add_argument('--opensource-name', help='', required=True) 79 parser.add_argument('--module-notices', nargs='+', help='', required=True) 80 args = parser.parse_args() 81 82 copy_notice_file(args.root_out_dir, 83 args.module_notices, 84 args.target_name, 85 args.opensource_name) 86 87 return 0 88 89 90if __name__ == '__main__': 91 sys.exit(main()) 92