Handle more cases

This should now handle:

- adding a sub-directory
- removing a sub-directory
This commit is contained in:
tcely authored and GitHub committed 2025-02-28 22:51:52 -05:00
1 parent f41faa444d
commit 8c465f0afe
1 file changed
+30 -9
+30 -9
View File
@@ -1,4 +1,5 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from django.conf import settings
from django.db.models.signals import pre_save, post_save, pre_delete, post_delete
from django.dispatch import receiver
@@ -29,15 +30,35 @@ def source_pre_save(sender, instance, **kwargs):
return
existing_dirpath = existing_source.directory_path.resolve(strict=True)
new_dirpath = instance.directory_path.resolve(strict=False)
rename_source_directory = (
existing_dirpath != new_dirpath and
not new_dirpath.exists()
)
if rename_source_directory:
tmp_dirpath = existing_dirpath.parent / ('.tmp.' + existing_dirpath.name)
existing_dirpath.rename(tmp_dirpath)
mkdir_p(new_dirpath.parent)
tmp_dirpath.rename(new_dirpath)
if existing_dirpath != new_dirpath:
path_name = lambda p: p.name
relative_dir = existing_source.directory
rd_parents = Path(relative_dir).parents
rd_parents_set = set(map(path_name, rd_parents))
ad_parents = existing_dirpath.parents
ad_parents_set = set(map(path_name, ad_parents))
# the names in the relative path are also in the absolute path
parents_count = len(ad_parents_set.intersection(rd_parents_set))
work_directory = existing_dirpath
while parents_count > 0:
work_directory = work_directory.parent
parents_count -= 1
with TemporaryDirectory(suffix='.'+new_dirpath.name, prefix='.tmp.', dir=work_directory) as tmp_dir:
tmp_dirpath = Path(tmp_dir)
existed = None
if new_dirpath.exists():
existed = tmp_dirpath / 'existed'
new_dirpath.rename(existed)
previous = tmp_dirpath / 'previous'
existing_dirpath.rename(previous)
mkdir_p(new_dirpath.parent)
previous.rename(new_dirpath)
if existed and existed.is_dir():
existed.rename(new_dirpath / '.existed')
# TODO: merge the contents of the directories
pass
elif existed:
existed.rename(new_dirpath / '.existed')
recreate_index_source_task = (
existing_source.name != instance.name or
existing_source.index_schedule != instance.index_schedule