Skip to content
Snippets Groups Projects
Commit fe494c94 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

[backend] Simplify cache clean-up by using a bottom-up approach

parent c56681c1
No related branches found
No related tags found
1 merge request!194Scheduler
...@@ -89,13 +89,7 @@ def cleanup_cache(path, age_in_minutes=0, delete=False): ...@@ -89,13 +89,7 @@ def cleanup_cache(path, age_in_minutes=0, delete=False):
removed_files = [] removed_files = []
def _remove_dirs(p): for p, dirs, files in os.walk(path, topdown=False):
d = os.path.dirname(p)
if not os.listdir(d):
logger.info("[rmdir] `%s'", d)
os.removedirs(d)
for p, dirs, files in os.walk(path, topdown=True):
files = [f for f in files if not f.startswith('.')] files = [f for f in files if not f.startswith('.')]
dirs[:] = [d for d in dirs if not d.startswith('.')] #note: in-place dirs[:] = [d for d in dirs if not d.startswith('.')] #note: in-place
...@@ -114,7 +108,6 @@ def cleanup_cache(path, age_in_minutes=0, delete=False): ...@@ -114,7 +108,6 @@ def cleanup_cache(path, age_in_minutes=0, delete=False):
if delete: if delete:
logger.info("[rm] `%s' (dangling)", fullpath) logger.info("[rm] `%s' (dangling)", fullpath)
os.remove(fullpath) os.remove(fullpath)
_remove_dirs(fullpath)
removed_files.append(fullpath) removed_files.append(fullpath)
continue continue
...@@ -124,7 +117,6 @@ def cleanup_cache(path, age_in_minutes=0, delete=False): ...@@ -124,7 +117,6 @@ def cleanup_cache(path, age_in_minutes=0, delete=False):
if delete: if delete:
logger.info("[rm] `%s'", fullpath) logger.info("[rm] `%s'", fullpath)
os.remove(fullpath) os.remove(fullpath)
_remove_dirs(fullpath)
removed_files.append(fullpath) removed_files.append(fullpath)
else: else:
logger.debug("[skip] `%s' (%f >= %f)", fullpath, logger.debug("[skip] `%s' (%f >= %f)", fullpath,
...@@ -132,7 +124,8 @@ def cleanup_cache(path, age_in_minutes=0, delete=False): ...@@ -132,7 +124,8 @@ def cleanup_cache(path, age_in_minutes=0, delete=False):
for d in dirs: #also remove empty directories for d in dirs: #also remove empty directories
fullpath = os.path.join(p, d) fullpath = os.path.join(p, d)
if not os.listdir(fullpath) and delete: os.removedirs(fullpath) if not os.listdir(fullpath) and delete:
os.rmdir(fullpath)
return removed_files return removed_files
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment