1import os 2import json 3 4def ask_question(prompt): 5 return input(prompt).strip() 6 7def generate_readme_opensource(output_dir): 8 """ 9 生成 README.OpenSource 文件,支持多个开源组件的信息输入。 10 """ 11 components = [] 12 fields = [ 13 "Name", 14 "License", 15 "License File", 16 "Version Number", 17 "Owner", 18 "Upstream URL", 19 "Description" 20 ] 21 22 print("请输入开源组件的信息(输入完成后,可选择继续添加另一个组件):") 23 while True: 24 component = {} 25 for field in fields: 26 value = ask_question(f"{field}: ") 27 component[field] = value 28 components.append(component) 29 30 add_more = ask_question("是否添加另一个组件?(y/n): ").lower() 31 if add_more != 'y': 32 break 33 34 if not os.path.exists(output_dir): 35 os.makedirs(output_dir) 36 37 readme_path = os.path.join(output_dir, 'README.OpenSource') 38 with open(readme_path, 'w', encoding='utf-8') as f: 39 json.dump(components, f, indent=2, ensure_ascii=False) 40 print(f"已生成 {readme_path}") 41 42def main(): 43 output_dir = ask_question("请输入输出目录(默认当前目录):") or '.' 44 generate_readme_opensource(output_dir) 45 46if __name__ == '__main__': 47 main() 48 49