From e50276a0cda73035c16bd1fe2e7ca75ebd7d9f34 Mon Sep 17 00:00:00 2001 From: Benjamin Moody Date: Thu, 11 Jul 2024 16:24:15 -0400 Subject: [PATCH] resetdb: fix and simplify the process of clearing media files. Previously, resetdb would fail if the media directory contained something that wasn't a subdirectory (such as a file called "ethics" that was created by a buggy version of loaddemo.) We can simplify this function enormously - what we really want is to delete *everything* except for ".gitkeep" files. --- .../user/management/commands/resetdb.py | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/physionet-django/user/management/commands/resetdb.py b/physionet-django/user/management/commands/resetdb.py index 7b35de57fa..8d203cc636 100644 --- a/physionet-django/user/management/commands/resetdb.py +++ b/physionet-django/user/management/commands/resetdb.py @@ -87,22 +87,19 @@ def clear_media_files(): """ Remove all media files. - Removes all content in the media root, excluding the immediate - subfolders themselves and the .gitkeep files. + Remove all content in the media root, except that if a file called + ".gitkeep" is found, preserve that file and the subdirectory + containing it. """ - for subdir in os.listdir(settings.MEDIA_ROOT): - media_subdir = os.path.join(settings.MEDIA_ROOT, subdir) - subdir_items = [os.path.join(media_subdir, item) for item in os.listdir(media_subdir) if item != '.gitkeep'] - for item in subdir_items: - if os.path.isdir(item): - for root, dirs, files in os.walk(item): - for d in dirs: - os.chmod(os.path.join(root, d), 0o755) - for f in files: - os.chmod(os.path.join(root, f), 0o755) - shutil.rmtree(item) - else: - os.remove(item) + for path, subdirs, files in os.walk(settings.MEDIA_ROOT, topdown=False): + os.chmod(path, 0o755) + for name in files: + if name != '.gitkeep': + os.remove(os.path.join(path, name)) + for name in subdirs: + if not os.listdir(os.path.join(path, name)): + os.rmdir(os.path.join(path, name)) + def clear_created_static_files(): """