fix(cache): support clear image cache

This commit is contained in:
InfinityPacer
2024-10-20 01:05:06 +08:00
parent c6febe4755
commit 2c8ecdfcb9
3 changed files with 17 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ import subprocess
import sys
from glob import glob
from pathlib import Path
from typing import List, Union, Tuple, Optional
from typing import List, Optional, Tuple, Union
import docker
import psutil
@@ -537,11 +537,18 @@ class SystemUtils:
@staticmethod
def clear(temp_path: Path, days: int):
"""
清理临时目录中指定天数前的文件
清理指定目录中指定天数前的文件,递归删除子文件及空文件夹
"""
if not temp_path.exists():
return
for file in temp_path.glob('*'):
if file.is_file() \
and (datetime.datetime.now() - datetime.datetime.fromtimestamp(file.stat().st_mtime)).days > days:
# 遍历目录及子目录中的所有文件和文件夹
for file in temp_path.rglob('*'):
# 如果是文件并且符合时间条件,则删除
if file.is_file() and (
datetime.datetime.now() - datetime.datetime.fromtimestamp(file.stat().st_mtime)).days > days:
file.unlink()
# 删除空的文件夹
for folder in sorted(temp_path.rglob('*'), reverse=True):
# 确保是空文件夹
if folder.is_dir() and not any(folder.iterdir()):
folder.rmdir()