import os import re import shutil import ast from pathlib import Path def find_plugin_class(process_file: str) -> str: """从process.py中找到继承BasePlugin的类名""" with open(process_file, "r", encoding="utf-8") as f: content = f.read() # 使用AST解析Python代码,比正则更可靠 tree = ast.parse(content) for node in ast.walk(tree): if isinstance(node, ast.ClassDef): # 检查是否继承BasePlugin for base in node.bases: if isinstance(base, ast.Name) and base.id == "BasePlugin": return node.name raise ValueError("❌ 未找到继承自BasePlugin的类!请检查process.py") def create_zip_package(class_name: str): """打包成ZIP文件,类名全小写""" src_path = Path("src") zip_name = class_name.lower() # 类名转全小写作为ZIP名 temp_dir = Path(f"temp_{zip_name}") # 临时打包目录 # 1. 创建临时目录 temp_dir.mkdir(exist_ok=True) # 2. 复制需要的文件 shutil.copytree(src_path / "packages", temp_dir / "packages") shutil.copy(src_path / "process.py", temp_dir) shutil.copy(src_path / "config.toml", temp_dir) # 3. 生成ZIP shutil.make_archive(f"dist/{zip_name}", "zip", temp_dir) # 4. 清理临时目录 shutil.rmtree(temp_dir) print(f"✅ 打包完成:dist/{zip_name}.zip") def main(): process_file = "src/process.py" if not os.path.exists(process_file): print(f"❌ 错误:{process_file} 文件不存在!") return try: plugin_class = find_plugin_class(process_file) print(f"找到插件类: {plugin_class} → 包名: {plugin_class.lower()}.zip") Path("dist").mkdir(exist_ok=True) # 创建dist目录 create_zip_package(plugin_class) except Exception as e: print(f"❌ 打包失败:{e}") if __name__ == "__main__": main()