|
4 | 4 | """ |
5 | 5 | import argparse |
6 | 6 | import logging |
| 7 | +import shutil |
7 | 8 | import sys |
8 | 9 | from pathlib import Path |
9 | 10 |
|
|
22 | 23 |
|
23 | 24 | # Paths |
24 | 25 | SCRIPT_DIR = Path(__file__).parent |
25 | | -PROJECT_ROOT = SCRIPT_DIR.parent |
26 | | -CONTENT_ROOT = PROJECT_ROOT.parent / "content" |
| 26 | +PROJECT_ROOT = SCRIPT_DIR.parent.resolve() |
27 | 27 |
|
28 | 28 | CRAWLER_MAP = { |
29 | 29 | "gitbook": GitBookCrawler, |
30 | 30 | "retype": RetypeCrawler, |
31 | 31 | } |
32 | 32 |
|
33 | 33 |
|
| 34 | +def resolve_output_dir(config: dict) -> Path | None: |
| 35 | + """Resolve and validate output_dir relative to the project root.""" |
| 36 | + raw_output_dir = str(config.get("output_dir", "")).strip() |
| 37 | + if not raw_output_dir: |
| 38 | + logger.error("配置缺少 output_dir") |
| 39 | + return None |
| 40 | + |
| 41 | + output_dir = (PROJECT_ROOT / raw_output_dir).resolve() |
| 42 | + |
| 43 | + try: |
| 44 | + output_dir.relative_to(PROJECT_ROOT) |
| 45 | + except ValueError: |
| 46 | + logger.error(f"output_dir 超出项目目录: {output_dir}") |
| 47 | + return None |
| 48 | + |
| 49 | + return output_dir |
| 50 | + |
| 51 | + |
| 52 | +def create_staging_dir(output_dir: Path) -> Path: |
| 53 | + """Create a clean staging directory next to the final output directory.""" |
| 54 | + staging_dir = output_dir.parent / f".{output_dir.name}.staging" |
| 55 | + if staging_dir.exists(): |
| 56 | + shutil.rmtree(staging_dir) |
| 57 | + staging_dir.mkdir(parents=True, exist_ok=True) |
| 58 | + return staging_dir |
| 59 | + |
| 60 | + |
| 61 | +def publish_staging_dir(staging_dir: Path, output_dir: Path) -> None: |
| 62 | + """Atomically replace the final output directory with staged content.""" |
| 63 | + if output_dir.exists(): |
| 64 | + shutil.rmtree(output_dir) |
| 65 | + staging_dir.rename(output_dir) |
| 66 | + |
| 67 | + |
34 | 68 | def run_crawler(name: str, config: dict) -> tuple[int, int]: |
35 | 69 | """Run a crawler for the given wiki config.""" |
36 | 70 | crawler_type = config.get("type") |
37 | 71 | crawler_class = CRAWLER_MAP.get(crawler_type) |
38 | 72 |
|
39 | 73 | if not crawler_class: |
40 | 74 | logger.error(f"未知爬虫类型: {crawler_type}") |
41 | | - return 0, 0 |
| 75 | + return 0, 1 |
42 | 76 |
|
43 | | - # Resolve output_dir relative to PROJECT_ROOT |
44 | | - raw_output_dir = config.get("output_dir", "") |
45 | | - abs_output_dir = (PROJECT_ROOT / raw_output_dir).resolve() |
| 77 | + output_dir = resolve_output_dir(config) |
| 78 | + if output_dir is None: |
| 79 | + return 0, 1 |
| 80 | + |
| 81 | + staging_dir = create_staging_dir(output_dir) |
| 82 | + logger.info(f"输出目录: {output_dir.relative_to(PROJECT_ROOT)}") |
46 | 83 |
|
47 | 84 | crawl_config = config.copy() |
48 | | - crawl_config["output_dir"] = abs_output_dir |
| 85 | + crawl_config["output_dir"] = staging_dir |
| 86 | + |
| 87 | + crawler = crawler_class(name, crawl_config) |
| 88 | + |
| 89 | + try: |
| 90 | + success, failed = crawler.run() |
| 91 | + |
| 92 | + if success == 0 and failed == 0: |
| 93 | + logger.error("未抓取到任何页面,保留现有输出目录") |
| 94 | + return 0, 1 |
| 95 | + |
| 96 | + if failed > 0: |
| 97 | + logger.error("存在抓取失败页面,保留现有输出目录") |
| 98 | + return success, failed |
49 | 99 |
|
50 | | - crawler = crawler_class(name, crawl_config, CONTENT_ROOT) |
51 | | - return crawler.run() |
| 100 | + publish_staging_dir(staging_dir, output_dir) |
| 101 | + logger.info(f"📦 已发布到: {output_dir.relative_to(PROJECT_ROOT)}") |
| 102 | + return success, failed |
| 103 | + finally: |
| 104 | + if staging_dir.exists(): |
| 105 | + shutil.rmtree(staging_dir) |
52 | 106 |
|
53 | 107 |
|
54 | 108 | def main(): |
|
0 commit comments