定位章节根目录下最新一次运行的输出目录。 扫描 `chapter_root` 下所有子目录,筛选出包含 `manifest.json` 的候选,按修改时间倒序取最新一条。若目录不存在或没有有效 manifest,会记录错误并返回 None。 参数: chapter_root: 章节输出的根目录(通常是 settings.CHAPTER_OUTPUT_DIR) 返回: Path | None: 最新的 run 目录路径;若未找到则为 None。
(chapter_root: Path)
| 18 | |
| 19 | |
| 20 | def find_latest_run_dir(chapter_root: Path): |
| 21 | """ |
| 22 | 定位章节根目录下最新一次运行的输出目录。 |
| 23 | |
| 24 | 扫描 `chapter_root` 下所有子目录,筛选出包含 `manifest.json` |
| 25 | 的候选,按修改时间倒序取最新一条。若目录不存在或没有有效 |
| 26 | manifest,会记录错误并返回 None。 |
| 27 | |
| 28 | 参数: |
| 29 | chapter_root: 章节输出的根目录(通常是 settings.CHAPTER_OUTPUT_DIR) |
| 30 | |
| 31 | 返回: |
| 32 | Path | None: 最新的 run 目录路径;若未找到则为 None。 |
| 33 | """ |
| 34 | if not chapter_root.exists(): |
| 35 | logger.error(f"章节目录不存在: {chapter_root}") |
| 36 | return None |
| 37 | |
| 38 | run_dirs = [] |
| 39 | for candidate in chapter_root.iterdir(): |
| 40 | if not candidate.is_dir(): |
| 41 | continue |
| 42 | manifest_path = candidate / "manifest.json" |
| 43 | if manifest_path.exists(): |
| 44 | run_dirs.append((candidate, manifest_path.stat().st_mtime)) |
| 45 | |
| 46 | if not run_dirs: |
| 47 | logger.error("未找到带 manifest.json 的章节目录") |
| 48 | return None |
| 49 | |
| 50 | latest_dir = sorted(run_dirs, key=lambda item: item[1], reverse=True)[0][0] |
| 51 | logger.info(f"找到最新run目录: {latest_dir.name}") |
| 52 | return latest_dir |
| 53 | |
| 54 | |
| 55 | def load_manifest(run_dir: Path): |