MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / main

Function main

scripts/developer-guide/find_unused_images.py:32–87  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30
31
32def 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

Callers 1

Calls 6

iter_text_filesFunction · 0.85
existsMethod · 0.65
lowerMethod · 0.65
appendMethod · 0.65
resolveMethod · 0.45
mkdirMethod · 0.45

Tested by

no test coverage detected