Skip to content

Commit

Permalink
resetdb: fix and simplify the process of clearing media files.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Benjamin Moody committed Sep 19, 2024
1 parent 3db9223 commit e50276a
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions physionet-django/user/management/commands/resetdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down

0 comments on commit e50276a

Please sign in to comment.