fix save_attempts trimming (#24246)

This commit is contained in:
Josh Hawkins
2026-09-12 07:30:04 -06:00
committed by Nicolas Mowen
parent 7bc32fd4c9
commit 37f338d5d6
4 changed files with 89 additions and 24 deletions
+35
View File
@@ -269,6 +269,41 @@ def delete_event_thumbnail(event: Event) -> bool:
return True
### Training Images
TRAINING_IMAGE_EXTENSIONS = (".webp", ".png", ".jpg", ".jpeg")
def trim_oldest_files(folder: str, max_files: int) -> None:
"""Delete the oldest training images until at most max_files remain."""
try:
names = os.listdir(folder)
except OSError:
return
files: list[tuple[float, str]] = []
for name in names:
if not name.lower().endswith(TRAINING_IMAGE_EXTENSIONS):
continue
path = os.path.join(folder, name)
# the UI can move or delete an image between listdir and stat
try:
files.append((os.path.getctime(path), path))
except OSError:
continue
files.sort(reverse=True)
for _, path in files[max_files:]:
try:
os.unlink(path)
except OSError:
logger.debug("Unable to delete training image %s", path)
### File Locking