From 8e0ca9e4e2dc7bd766f7ec26598d7a1a217b98c3 Mon Sep 17 00:00:00 2001
From: log
Date: Thu, 23 Jul 2026 19:29:52 +0800
Subject: [PATCH] fix: harden daily master on Windows consoles
---
scripts/reset_stock_data.py | 90 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 90 insertions(+), 0 deletions(-)
diff --git a/scripts/reset_stock_data.py b/scripts/reset_stock_data.py
new file mode 100644
index 0000000..c4968e6
--- /dev/null
+++ b/scripts/reset_stock_data.py
@@ -0,0 +1,90 @@
+#!/usr/bin/env python3
+"""Safely reset generated stock data by moving it into a timestamped backup."""
+
+import argparse
+import json
+import shutil
+import sys
+from datetime import datetime
+from pathlib import Path
+
+
+PROJECT_ROOT = Path(__file__).resolve().parents[1]
+DATA_DIR = PROJECT_ROOT / "data"
+BACKUP_ROOT = DATA_DIR / "backups"
+RESET_FILES = (
+ "akshare_real_data_fixed.pkl",
+ "akshare_real_data_fixed_metadata.json",
+ "enhanced_factors.pkl",
+ "selection_result.json",
+)
+
+
+def parse_args(argv=None):
+ parser = argparse.ArgumentParser(
+ description="安全重置行情和派生结果;原文件会移动到 data/backups"
+ )
+ parser.add_argument(
+ "--yes",
+ action="store_true",
+ help="确认执行;仅用于用户已经明确确认的自动化场景",
+ )
+ return parser.parse_args(argv)
+
+
+def safe_reset_data(data_dir=DATA_DIR, backup_root=BACKUP_ROOT, now=None):
+ data_dir = Path(data_dir)
+ backup_root = Path(backup_root)
+ candidates = [data_dir / name for name in RESET_FILES]
+ existing = [path for path in candidates if path.exists()]
+ if not existing:
+ return None, []
+
+ timestamp = (now or datetime.now()).strftime("%Y%m%d_%H%M%S")
+ backup_dir = backup_root / f"stock_data_{timestamp}"
+ backup_dir.mkdir(parents=True, exist_ok=False)
+
+ moved = []
+ try:
+ for source in existing:
+ destination = backup_dir / source.name
+ shutil.move(str(source), str(destination))
+ moved.append(destination)
+ manifest = {
+ "created_at": (now or datetime.now()).isoformat(),
+ "files": [path.name for path in moved],
+ }
+ with (backup_dir / "manifest.json").open("w", encoding="utf-8") as handle:
+ json.dump(manifest, handle, ensure_ascii=False, indent=2)
+ except Exception:
+ for destination in reversed(moved):
+ if destination.exists():
+ shutil.move(str(destination), str(data_dir / destination.name))
+ raise
+
+ return backup_dir, moved
+
+
+def main(argv=None):
+ args = parse_args(argv)
+ if not args.yes:
+ print("此操作会重置正式行情、增强因子和选股结果。")
+ print("原文件不会永久删除,而会移动到 data/backups。")
+ confirmation = input("请输入 RESET 确认: ").strip()
+ if confirmation != "RESET":
+ print("已取消。")
+ return 1
+
+ backup_dir, moved = safe_reset_data()
+ if not moved:
+ print("没有找到需要重置的数据文件。")
+ return 0
+
+ print(f"已安全重置 {len(moved)} 个文件。")
+ print(f"备份目录: {backup_dir}")
+ print("下次运行 update_stock_data.py 将自动执行全量初始化。")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
--
Gitblit v1.8.0