From fe494c94373bd864797b7dca2279265a8a3bf69a Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.dos.anjos@gmail.com> Date: Fri, 22 Apr 2016 11:49:46 +0200 Subject: [PATCH] [backend] Simplify cache clean-up by using a bottom-up approach --- beat/web/backend/utils.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/beat/web/backend/utils.py b/beat/web/backend/utils.py index beacf960c..2871ca269 100644 --- a/beat/web/backend/utils.py +++ b/beat/web/backend/utils.py @@ -89,13 +89,7 @@ def cleanup_cache(path, age_in_minutes=0, delete=False): removed_files = [] - def _remove_dirs(p): - 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): + for p, dirs, files in os.walk(path, topdown=False): files = [f for f in files if not f.startswith('.')] 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): if delete: logger.info("[rm] `%s' (dangling)", fullpath) os.remove(fullpath) - _remove_dirs(fullpath) removed_files.append(fullpath) continue @@ -124,7 +117,6 @@ def cleanup_cache(path, age_in_minutes=0, delete=False): if delete: logger.info("[rm] `%s'", fullpath) os.remove(fullpath) - _remove_dirs(fullpath) removed_files.append(fullpath) else: logger.debug("[skip] `%s' (%f >= %f)", fullpath, @@ -132,7 +124,8 @@ def cleanup_cache(path, age_in_minutes=0, delete=False): for d in dirs: #also remove empty directories 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 -- GitLab