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 20 21sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 22from scripts.util.file_utils import read_json_file # noqa: E402 23from scripts.util import build_utils # noqa: E402 24 25README_FILE_NAME = 'README.OpenSource' 26LICENSE_CANDIDATES = [ 27 'LICENSE', 28 'License', 29 'NOTICE', 30 'Notice', 31 'COPYRIGHT', 32 'Copyright' 33] 34 35 36def is_top_dir(current_dir): 37 return os.path.exists(os.path.join(current_dir, '.gn')) 38 39 40def find_license_recursively(current_dir, default_license): 41 if is_top_dir(current_dir): 42 return default_license 43 for file in ['LICENSE', 'NOTICE', 'License', 'Copyright']: 44 candidate = os.path.join(current_dir, file) 45 if os.path.exists(os.path.join(current_dir, file)): 46 return os.path.join(candidate) 47 return find_license_recursively(os.path.dirname(current_dir), 48 default_license) 49 50 51def get_license_from_readme(readme_path): 52 contents = read_json_file(readme_path) 53 if contents is None: 54 raise Exception("Error: failed to read {}.".format(readme_path)) 55 56 notice_file = contents[0].get('License File').strip() 57 if notice_file is None: 58 raise Exception("Error: value of notice file is empty in {}.".format( 59 readme_path)) 60 61 return os.path.join(os.path.dirname(readme_path), notice_file) 62 63 64def do_collect_notice_files(options, depfiles): 65 notice_file = options.license_file 66 if notice_file is None: 67 readme_path = os.path.join(options.module_source_dir, 68 README_FILE_NAME) 69 if os.path.exists(readme_path): 70 depfiles.append(readme_path) 71 notice_file = get_license_from_readme(readme_path) 72 73 if notice_file is None: 74 notice_file = find_license_recursively(options.module_source_dir, 75 options.default_license) 76 77 if notice_file: 78 for output in options.output: 79 os.makedirs(os.path.dirname(output), exist_ok=True) 80 if os.path.exists(notice_file): 81 shutil.copy(notice_file, output) 82 else: 83 build_utils.touch(output) 84 depfiles.append(notice_file) 85 86 87def main(args): 88 args = build_utils.expand_file_args(args) 89 90 parser = argparse.ArgumentParser() 91 build_utils.add_depfile_option(parser) 92 93 parser.add_argument('--license-file', required=False) 94 parser.add_argument('--default-license', required=True) 95 parser.add_argument('--output', action='append', required=False) 96 parser.add_argument('--module-source-dir', 97 help='source directory of this module', 98 required=True) 99 100 options = parser.parse_args() 101 depfiles = [] 102 103 do_collect_notice_files(options, depfiles) 104 if options.license_file: 105 depfiles.append(options.license_file) 106 build_utils.write_depfile(options.depfile, options.output[0], depfiles) 107 108 109if __name__ == '__main__': 110 sys.exit(main(sys.argv[1:])) 111