""" 统一测试脚本:根据参数选择运行单个帖子或批量处理 功能: 1. 支持 single 模式:处理单个待解构帖子 2. 支持 batch 模式:批量处理作者历史帖子 3. 自动加载对应目录的数据 """ import sys import subprocess from pathlib import Path import argparse def main(): """主函数""" # 解析命令行参数 parser = argparse.ArgumentParser( description='运行What解构工作流测试脚本', formatter_class=argparse.RawDescriptionHelpFormatter, epilog=""" 使用示例: # 单个帖子模式 python examples/run_test.py single 阿里多多酱 python examples/run_test.py single G88818 # 批量处理模式 python examples/run_test.py batch 阿里多多酱 python examples/run_test.py batch G88818 """ ) parser.add_argument( 'mode', type=str, choices=['single', 'batch'], help='运行模式:single(单个帖子)或 batch(批量处理)' ) parser.add_argument( 'directory', type=str, help='帖子目录名(如"阿里多多酱"或"G88818")' ) args = parser.parse_args() # 获取脚本目录 script_dir = Path(__file__).parent # 根据模式选择对应的脚本 if args.mode == 'single': script_path = script_dir / "run_single.py" mode_name = "单个帖子处理" else: # batch script_path = script_dir / "run_batch.py" mode_name = "批量处理" # 检查脚本是否存在 if not script_path.exists(): print(f"❌ 错误:脚本不存在 {script_path}") return 1 # 检查目录是否存在 data_dir = script_dir / args.directory if not data_dir.exists(): print(f"❌ 错误:目录不存在 {data_dir}") print(f" 请确保以下目录存在:") print(f" - {data_dir}") return 1 # 显示运行信息 print("=" * 80) print(f"运行模式: {mode_name}") print(f"目录: {args.directory}") print(f"脚本: {script_path.name}") print("=" * 80) print() # 构建命令 cmd = [sys.executable, str(script_path), args.directory] # 执行命令 try: result = subprocess.run(cmd, check=False) return result.returncode except KeyboardInterrupt: print("\n\n⚠️ 用户中断执行") return 130 except Exception as e: print(f"\n❌ 执行失败: {e}") return 1 if __name__ == "__main__": sys.exit(main())