| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- # -*- coding: utf-8 -*-
- """补章节编号缺口,并用 Word 刷新带页码的目录。"""
- from __future__ import annotations
- import hashlib
- import sys
- import tempfile
- import time
- from pathlib import Path
- HERE = Path(__file__).resolve().parent
- if str(HERE) not in sys.path:
- sys.path.insert(0, str(HERE))
- from _delivery_docx_polish import latest_formal_files, polish_file
- from _unify_ab_delivery_vnext import BASE
- TMP = Path(tempfile.gettempdir()) / "aidop_toc_word"
- WD_COLOR_BLACK = 0
- WD_STAT_PAGES = 2
- def refresh_one(src: Path) -> str:
- import subprocess
- worker = HERE / "_word_toc_one.py"
- last = ""
- timeout = 240 if src.stat().st_size < 8_000_000 else 600
- for attempt in range(3):
- proc = subprocess.run(
- [sys.executable, str(worker), str(src)],
- capture_output=True,
- text=True,
- timeout=timeout,
- check=False,
- )
- out = (proc.stdout or "").strip()
- err = (proc.stderr or "").strip()
- if proc.returncode == 0 and out:
- return out
- last = err or out or f"exit {proc.returncode}"
- time.sleep(2 + attempt * 2)
- raise RuntimeError(last)
- def main() -> None:
- files = latest_formal_files(BASE)
- flags = {x for x in sys.argv[1:] if x.startswith("-")}
- only = [x for x in sys.argv[1:] if not x.startswith("-")]
- if only:
- files = [p for p in files if any(k in p.name or k in str(p) for k in only)]
- files = sorted(files, key=lambda p: p.stat().st_size)
- if "--refresh-only" not in flags:
- print("prepare", len(files), flush=True)
- for p in files:
- try:
- print(polish_file(p), flush=True)
- except Exception as exc:
- print(f"FAIL prepare {p.parent.name}/{p.name}: {exc}", flush=True)
- print("word-refresh", len(files), flush=True)
- for p in files:
- t0 = time.time()
- try:
- msg = refresh_one(p)
- print(f"OK {p.parent.name}/{p.name} {msg} {time.time()-t0:.0f}s", flush=True)
- except Exception as exc:
- print(f"FAIL word {p.parent.name}/{p.name}: {exc}", flush=True)
- if __name__ == "__main__":
- main()
|