1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2024 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 os 17import json 18 19 20def ask_question(prompt, default_value=None): 21 """提示用户输入,若没有输入则使用默认值""" 22 value = input(f"{prompt} [{default_value}]: ").strip() 23 return value or default_value 24 25 26def ask_for_list(prompt): 27 """提示用户输入一个列表,以逗号分隔""" 28 value = input(f"{prompt} (多个项请用逗号分隔): ").strip() 29 return [item.strip() for item in value.split(",")] if value else [] 30 31 32def process_license_info(): 33 """处理许可证信息和对应的文件路径""" 34 licenses = ask_question("请输入许可证名称(如有多个,用分号分隔)") 35 license_files = ask_question("请输入许可证文件路径(如果有多个,请使用分号分隔)") 36 37 license_list = ( 38 [license.strip() for license in licenses.split(";")] if licenses else [] 39 ) 40 license_file_list = ( 41 [file.strip() for file in license_files.split(";")] if license_files else [] 42 ) 43 44 # 检查输入是否为空 45 if not license_list or not license_file_list: 46 raise ValueError("许可证和许可证文件路径不能为空。") 47 48 # 检查许可证和文件路径的匹配情况 49 if len(license_list) != len(license_file_list): 50 # 只有在以下两种特殊情况下允许不相等: 51 # 1. 一个许可证对应多个文件 52 # 2. 多个许可证对应一个文件 53 if not ( 54 (len(license_list) == 1 and len(license_file_list) > 1) 55 or (len(license_list) > 1 and len(license_file_list) == 1) 56 ): 57 raise ValueError( 58 "许可证和许可证文件的数量不匹配,必须是一对一、一对多或多对一的关系。" 59 ) 60 61 return license_list, license_file_list 62 63 64def generate_readme_opensource(output_dir): 65 """ 66 生成 README.OpenSource 文件,支持多个开源组件的信息输入。 67 """ 68 components = [] 69 fields = [ 70 "Name", 71 "License", 72 "License File", 73 "Version Number", 74 "Owner", 75 "Upstream URL", 76 "Description", 77 "Dependencies", 78 ] 79 80 print("请输入开源组件的信息(输入完成后,可选择继续添加另一个组件):") 81 while True: 82 component = {} 83 # 获取组件的基本信息 84 component["Name"] = ask_question("Name: ") 85 86 # 获取许可证信息 87 license_list, license_file_list = process_license_info() 88 component["License"] = "; ".join(license_list) 89 component["License File"] = "; ".join(license_file_list) 90 91 component["Version Number"] = ask_question("Version Number: ") 92 component["Owner"] = ask_question("Owner: ") 93 component["Upstream URL"] = ask_question("Upstream URL: ") 94 component["Description"] = ask_question("Description: ") 95 96 # 获取依赖信息(可选) 97 dependencies = ask_for_list("请输入该软件的依赖项(如果有多个,请用逗号分隔)") 98 if dependencies: 99 component["Dependencies"] = dependencies 100 101 # 将组件信息添加到列表 102 components.append(component) 103 104 # 是否继续添加组件 105 add_more = ask_question("是否添加另一个组件?(y/n): ").lower() 106 if add_more != "y": 107 break 108 109 # 确保输出目录存在 110 if not os.path.exists(output_dir): 111 os.makedirs(output_dir) 112 113 # 输出 README.OpenSource 文件 114 readme_path = os.path.join(output_dir, "README.OpenSource") 115 with open(readme_path, "w", encoding="utf-8") as f: 116 json.dump(components, f, indent=2, ensure_ascii=False) 117 print(f"已生成 {readme_path}") 118 119 120def main(): 121 output_dir = ask_question("请输入输出目录(默认当前目录):") or "." 122 generate_readme_opensource(output_dir) 123 124 125if __name__ == "__main__": 126 main() 127