1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2020 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import sys 20import argparse 21import os 22from utils import read_json_file 23from utils import makedirs 24from utils import encode 25from utils import decode 26 27 28def is_top_dir(src_path): 29 return os.path.exists(os.path.join(src_path, '.gn')) 30 31 32def find_config_parent_file(src_dir, target_cfg): 33 if (not os.path.isdir(src_dir)) or is_top_dir(src_dir): 34 return '' 35 for file in os.listdir(src_dir): 36 file_path = os.path.join(src_dir, file) 37 if os.path.isfile(file_path): 38 if os.path.basename(file_path) == target_cfg: 39 return src_dir 40 return find_config_parent_file(os.path.dirname(src_dir), target_cfg) 41 42 43def get_notice_file_name(readme_file_path, copyright_file, 44 module_relative_src_path): 45 if not os.path.exists(readme_file_path) or os.path.isdir(readme_file_path): 46 return '', '', '', '' 47 48 opensource_config = read_json_file(readme_file_path) 49 if opensource_config is None: 50 return '', '', '', '' 51 52 license_file = '' 53 license_name = None 54 software_name = None 55 for info in opensource_config: 56 license_file = info.get('License File') 57 license_name = info.get('License') 58 software_name = '{} {}'.format(info.get('Name'), 59 info.get('Version Number')) 60 61 license_file_path = os.path.join(os.path.dirname(readme_file_path), 62 license_file.strip()) 63 if not os.path.exists(license_file_path): 64 return '', '', '', '' 65 66 copyright_file_path = os.path.join(os.path.dirname(copyright_file), 67 copyright_file.strip()) 68 if not os.path.exists(copyright_file_path): 69 return '', '', '', '' 70 71 return license_file_path, license_name, software_name, copyright_file_path 72 73 74def get_opensource_config_file(module_source_dir, root_out_dir, target_cfg): 75 config_file = '' 76 77 expect_file = os.path.join(module_source_dir, target_cfg) 78 if os.path.exists(expect_file): 79 config_file = expect_file 80 else: 81 relpath = os.path.relpath(module_source_dir, root_out_dir) 82 parent_cfg_dir = find_config_parent_file(os.path.dirname(relpath), 83 target_cfg) 84 if parent_cfg_dir != '': 85 config_file = os.path.join(parent_cfg_dir, target_cfg) 86 87 return config_file 88 89 90def get_notice_file(module_source_dir, root_out_dir, 91 module_relative_source_dir): 92 cfg_file = get_opensource_config_file(module_source_dir, root_out_dir, 93 'README.OpenSource') 94 copyright_file = get_opensource_config_file(module_source_dir, 95 root_out_dir, 96 'COPYRIGHT.OpenSource') 97 98 if cfg_file == '': 99 return '', '', '', '' 100 if copyright_file == '': 101 return '', '', '', '' 102 return get_notice_file_name(cfg_file, copyright_file, 103 module_relative_source_dir) 104 105 106def get_notice_file_dest_path(root_out_dir, target_name): 107 nf_dest_list = [] 108 nf_dest_dir = os.path.join(root_out_dir, 'NOTICE_FILE', target_name) 109 110 license_num = 1 111 while os.path.exists(os.path.join(nf_dest_dir, 112 "LICENSE{}.txt".format(license_num))): 113 license_num += 1 114 115 nf_dest_list.append(os.path.join(nf_dest_dir, 116 "LICENSE{}.txt".format(license_num))) 117 return nf_dest_list 118 119 120def create_dest_file(filename): 121 dir_name = os.path.dirname(filename) 122 if not os.path.exists(dir_name): 123 makedirs(dir_name) 124 125 126def gen_license(target_path, nf_dict, nf_src, cp_src): 127 target_license_path = os.path.join(target_path, 'NOTICE') 128 makedirs(target_path) 129 130 with open(nf_src, 'rt') as f: 131 nf_dict["license_content"] = decode(f.read()) 132 with open(cp_src, 'rt') as f: 133 nf_dict["copyright"] = decode(f.read()) 134 135 with open(target_license_path, 'at') as f: 136 f.write("Software: {}\n\n".format(encode(nf_dict["software"]))) 137 f.write("Copyright notice: \n{}\n".format( 138 encode(nf_dict["copyright"]))) 139 f.write("License: {}\n{}\n\n".format( 140 encode(nf_dict["license_name"]), 141 encode(nf_dict["license_content"]))) 142 143 144def generate_notice_file(root_out_dir, module_source_dir, 145 module_relative_source_dir, target_name): 146 nf_src = '' 147 nf_dict = { 148 "software": '', 149 "copyright": '', 150 "license_name": '', 151 "license_content": '' 152 } 153 154 nf_src, nf_dict["license_name"], nf_dict["software"], cp_src = \ 155 get_notice_file(module_source_dir, root_out_dir, 156 module_relative_source_dir) 157 target_path = os.path.join(root_out_dir, 'NOTICE_FILE', target_name) 158 if os.path.exists(nf_src) and os.path.exists(cp_src): 159 gen_license(target_path, nf_dict, nf_src, cp_src) 160 161 162def main(): 163 parser = argparse.ArgumentParser() 164 parser.add_argument('--root-out-dir', help='', required=True) 165 parser.add_argument('--module-source-dir', help='', required=True) 166 parser.add_argument('--module-relative-source-dir', help='', required=True) 167 parser.add_argument('--target-name', help='', required=True) 168 args = parser.parse_args() 169 170 if 'third_party/' not in args.module_relative_source_dir: 171 return 0 172 173 generate_notice_file(args.root_out_dir, 174 args.module_source_dir, 175 args.module_relative_source_dir, 176 args.target_name) 177 178 return 0 179 180 181if __name__ == '__main__': 182 sys.exit(main()) 183