From 3db9223620eff6c991deecb42034e19d935d6856 Mon Sep 17 00:00:00 2001 From: Benjamin Moody Date: Thu, 11 Jul 2024 16:23:50 -0400 Subject: [PATCH 1/2] loaddemo: fix copying files from demo/media. If demo files are placed in first-level subdirectories of the media directory, they were not being copied correctly when loading the demo. For example, "demo-files/media/ethics/Ethics_Approval_....txt" was incorrectly copied as "media/ethics" rather than "media/ethics/Ethics_Approval_....txt". Additionally, ensure that the destination directories are created if they don't already exist. --- physionet-django/user/management/commands/loaddemo.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/physionet-django/user/management/commands/loaddemo.py b/physionet-django/user/management/commands/loaddemo.py index 5ce63753a5..ec452572ba 100644 --- a/physionet-django/user/management/commands/loaddemo.py +++ b/physionet-django/user/management/commands/loaddemo.py @@ -99,13 +99,15 @@ def copy_demo_media(): for subdir in os.listdir(demo_media_root): demo_subdir = os.path.join(demo_media_root, subdir) target_subdir = os.path.join(settings.MEDIA_ROOT, subdir) + os.makedirs(target_subdir, exist_ok=True) for item in [i for i in os.listdir(demo_subdir) if i != '.gitkeep']: path = os.path.join(demo_subdir, item) if os.path.isdir(path): shutil.copytree(os.path.join(demo_subdir, item), os.path.join(target_subdir, item)) else: - shutil.copy(path, target_subdir) + shutil.copy(os.path.join(demo_subdir, item), + os.path.join(target_subdir, item)) # Published project files should have been made read-only at # the time of publication From e50276a0cda73035c16bd1fe2e7ca75ebd7d9f34 Mon Sep 17 00:00:00 2001 From: Benjamin Moody Date: Thu, 11 Jul 2024 16:24:15 -0400 Subject: [PATCH 2/2] 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(): """