()
| 30 | |
| 31 | |
| 32 | def main() -> None: |
| 33 | parser = argparse.ArgumentParser(description=__doc__) |
| 34 | parser.add_argument("doc_root", type=Path, help="Path to the developer guide root directory") |
| 35 | parser.add_argument( |
| 36 | "--image-dir", |
| 37 | type=Path, |
| 38 | default=None, |
| 39 | help="Directory containing images (defaults to <doc_root>/img)", |
| 40 | ) |
| 41 | parser.add_argument( |
| 42 | "--output", |
| 43 | type=Path, |
| 44 | default=None, |
| 45 | help="Optional path to write a JSON report", |
| 46 | ) |
| 47 | args = parser.parse_args() |
| 48 | |
| 49 | doc_root = args.doc_root.resolve() |
| 50 | image_dir = (args.image_dir or (doc_root / "img")).resolve() |
| 51 | |
| 52 | if not image_dir.exists(): |
| 53 | raise SystemExit(f"Image directory '{image_dir}' does not exist") |
| 54 | |
| 55 | adoc_files = list(iter_text_files(doc_root)) |
| 56 | contents = [path.read_text(encoding="utf-8", errors="ignore") for path in adoc_files] |
| 57 | |
| 58 | unused: List[str] = [] |
| 59 | for image_path in sorted(image_dir.rglob("*")): |
| 60 | if not image_path.is_file(): |
| 61 | continue |
| 62 | # Skip non-image artifacts that may live next to images (.gitkeep, |
| 63 | # OS-generated thumbnails, editor metadata). The unused-images check |
| 64 | # only meaningfully applies to image files referenced from prose. |
| 65 | if image_path.suffix.lower() not in IMAGE_EXTENSIONS: |
| 66 | continue |
| 67 | rel_path = image_path.relative_to(doc_root).as_posix() |
| 68 | if any(rel_path in text for text in contents): |
| 69 | continue |
| 70 | # Also fall back to checking just the file name to catch references that rely on imagesdir. |
| 71 | filename = image_path.name |
| 72 | if any(filename in text for text in contents): |
| 73 | continue |
| 74 | unused.append(rel_path) |
| 75 | |
| 76 | report = {"unused_images": unused} |
| 77 | |
| 78 | if args.output: |
| 79 | args.output.parent.mkdir(parents=True, exist_ok=True) |
| 80 | args.output.write_text(json.dumps(report, indent=2), encoding="utf-8") |
| 81 | |
| 82 | if unused: |
| 83 | print("Unused images detected:") |
| 84 | for rel_path in unused: |
| 85 | print(f" - {rel_path}") |
| 86 | raise SystemExit(1) |
| 87 | print("No unused images found.") |
| 88 | |
| 89 |
no test coverage detected