Skip to content

Commit 87bc182

Browse files
committed
Determine last file edit in templates to compare against public files.
1 parent ed95282 commit 87bc182

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

logya/commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from logya.content import write_collection, write_page
88
from logya.core import Logya
9-
from logya.util import paths
9+
from logya.util import latest_file_change, paths
1010

1111

1212
def clean(dir_site: str, verbose: bool, **_kwargs) -> None:
@@ -73,7 +73,7 @@ def generate(dir_site: str, verbose: bool, **_kwargs):
7373
L = Logya(dir_site=dir_site, verbose=verbose)
7474
L.build()
7575

76-
mtime_templates = L.paths.templates.stat().st_mtime
76+
mtime_templates = latest_file_change(L.paths.templates.as_posix())
7777

7878
print(f'Generate site in directory: {L.paths.public.as_posix()}')
7979
if L.paths.static.exists():

logya/util.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23
import sys
34
from functools import lru_cache
@@ -65,3 +66,27 @@ def slugify(s: str) -> str:
6566
Different input strings may result in the same output.
6667
"""
6768
return re.sub(re_forbidden, '-', s.strip()).strip('-')
69+
70+
71+
def latest_file_change(root: str):
72+
"""Return mtime of the most recently edited file in root."""
73+
74+
latest_mtime = 0.0
75+
76+
def walk_dir(path):
77+
with os.scandir(path) as it:
78+
for entry in it:
79+
if entry.is_dir(follow_symlinks=False):
80+
yield from walk_dir(entry.path)
81+
else:
82+
yield entry
83+
84+
for entry in walk_dir(root):
85+
try:
86+
mtime = entry.stat().st_mtime
87+
except FileNotFoundError: # skip race condition
88+
continue
89+
if mtime > latest_mtime:
90+
latest_mtime = mtime
91+
92+
return latest_mtime

0 commit comments

Comments
 (0)