run_test.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. 统一测试脚本:根据参数选择运行单个帖子或批量处理
  3. 功能:
  4. 1. 支持 single 模式:处理单个待解构帖子
  5. 2. 支持 batch 模式:批量处理作者历史帖子
  6. 3. 自动加载对应目录的数据
  7. """
  8. import sys
  9. import subprocess
  10. from pathlib import Path
  11. import argparse
  12. def main():
  13. """主函数"""
  14. # 解析命令行参数
  15. parser = argparse.ArgumentParser(
  16. description='运行What解构工作流测试脚本',
  17. formatter_class=argparse.RawDescriptionHelpFormatter,
  18. epilog="""
  19. 使用示例:
  20. # 单个帖子模式
  21. python examples/run_test.py single 阿里多多酱
  22. python examples/run_test.py single G88818
  23. # 批量处理模式
  24. python examples/run_test.py batch 阿里多多酱
  25. python examples/run_test.py batch G88818
  26. """
  27. )
  28. parser.add_argument(
  29. 'mode',
  30. type=str,
  31. choices=['single', 'batch'],
  32. help='运行模式:single(单个帖子)或 batch(批量处理)'
  33. )
  34. parser.add_argument(
  35. 'directory',
  36. type=str,
  37. help='帖子目录名(如"阿里多多酱"或"G88818")'
  38. )
  39. args = parser.parse_args()
  40. # 获取脚本目录
  41. script_dir = Path(__file__).parent
  42. # 根据模式选择对应的脚本
  43. if args.mode == 'single':
  44. script_path = script_dir / "run_single.py"
  45. mode_name = "单个帖子处理"
  46. else: # batch
  47. script_path = script_dir / "run_batch.py"
  48. mode_name = "批量处理"
  49. # 检查脚本是否存在
  50. if not script_path.exists():
  51. print(f"❌ 错误:脚本不存在 {script_path}")
  52. return 1
  53. # 检查目录是否存在
  54. data_dir = script_dir / args.directory
  55. if not data_dir.exists():
  56. print(f"❌ 错误:目录不存在 {data_dir}")
  57. print(f" 请确保以下目录存在:")
  58. print(f" - {data_dir}")
  59. return 1
  60. # 显示运行信息
  61. print("=" * 80)
  62. print(f"运行模式: {mode_name}")
  63. print(f"目录: {args.directory}")
  64. print(f"脚本: {script_path.name}")
  65. print("=" * 80)
  66. print()
  67. # 构建命令
  68. cmd = [sys.executable, str(script_path), args.directory]
  69. # 执行命令
  70. try:
  71. result = subprocess.run(cmd, check=False)
  72. return result.returncode
  73. except KeyboardInterrupt:
  74. print("\n\n⚠️ 用户中断执行")
  75. return 130
  76. except Exception as e:
  77. print(f"\n❌ 执行失败: {e}")
  78. return 1
  79. if __name__ == "__main__":
  80. sys.exit(main())