Skip to content

Commit 86fae46

Browse files
committed
Enhance _latest_mtime function to ignore specified directories and improve modification time calculation
1 parent 3829555 commit 86fae46

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

sdk/python/packages/flet/src/flet/docs_plugins/examples_gallery.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import subprocess
3+
from collections.abc import Iterable
34
from pathlib import Path
45
from typing import Optional
56

@@ -8,13 +9,33 @@
89
from mkdocs.plugins import BasePlugin
910

1011

11-
def _latest_mtime(root: Path) -> float:
12-
"""Return the latest modification time for all files under a directory."""
13-
mtimes = []
14-
for path in root.rglob("*"):
15-
if path.is_file():
16-
mtimes.append(path.stat().st_mtime)
17-
return max(mtimes) if mtimes else 0.0
12+
def _is_relative_to(path: Path, base: Path) -> bool:
13+
try:
14+
path.relative_to(base)
15+
return True
16+
except ValueError:
17+
return False
18+
19+
20+
def _latest_mtime(root: Path, ignored: Optional[Iterable[Path]] = None) -> float:
21+
"""Return the newest mtime under ``root`` ignoring any ``ignored`` directories."""
22+
root = root.resolve()
23+
ignore_roots = tuple(p.resolve() for p in ignored or ())
24+
latest = 0.0
25+
for dirpath, dirnames, filenames in os.walk(root):
26+
current_dir = Path(dirpath)
27+
if any(_is_relative_to(current_dir, skipped) for skipped in ignore_roots):
28+
dirnames[:] = []
29+
continue
30+
for name in filenames:
31+
file_path = current_dir / name
32+
if any(_is_relative_to(file_path, skipped) for skipped in ignore_roots):
33+
continue
34+
try:
35+
latest = max(latest, file_path.stat().st_mtime)
36+
except FileNotFoundError:
37+
continue
38+
return latest
1839

1940

2041
class ExamplesGalleryPlugin(BasePlugin):
@@ -68,7 +89,16 @@ def on_pre_build(self, config: MkDocsConfig) -> None:
6889
if not src.exists():
6990
return
7091

71-
src_mtime = _latest_mtime(src.parent)
92+
# Accept either a specific entry file or a folder and consistently inspect
93+
# the directory that actually contains the source files.
94+
src_root = src if src.is_dir() else src.parent
95+
ignore_roots: list[Path] = []
96+
if _is_relative_to(dist_dir, src_root):
97+
# Publishing into the source tree dirties files for every build, so
98+
# ignore the generated dist folder when computing the latest mtime.
99+
ignore_roots.append(dist_dir)
100+
101+
src_mtime = _latest_mtime(src_root, ignore_roots)
72102
dist_mtime = dist_index.stat().st_mtime if dist_index.exists() else 0.0
73103
if dist_mtime >= src_mtime:
74104
return

0 commit comments

Comments
 (0)