Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better detection of empty directories when download-syncing Zarrs #961

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions dandi/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,19 +872,16 @@ def digest_callback(path: str, algoname: str, d: str) -> None:
is_empty = False
elif p.is_dir():
dirs.append(p)
is_empty = False
else:
is_empty = False
if is_empty and d != zarr_basepath:
empty_dirs.append(d)
while empty_dirs:
d = empty_dirs.popleft()
try:
d.rmdir()
except OSError:
pass
else:
if d.parent != zarr_basepath and not any(d.parent.iterdir()):
empty_dirs.append(d.parent)
d.rmdir()
if d.parent != zarr_basepath and not any(d.parent.iterdir()):
empty_dirs.append(d.parent)

if "skipped" not in final_out["message"]:
zarr_checksum = asset.get_digest().value
Expand Down
17 changes: 17 additions & 0 deletions dandi/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,23 @@ def test_download_zarr_asset_id_only(
assert_dirtrees_eq(zarr_dandiset.dspath / "sample.zarr", tmp_path / "sample.zarr")


def test_download_zarr_subdir_has_only_subdirs(
tmp_path: Path, new_dandiset: SampleDandiset
) -> None:
zf = new_dandiset.dspath / "sample.zarr"
zf.mkdir()
(zf / "dirs").mkdir()
(zf / "dirs" / "apple").mkdir()
(zf / "dirs" / "apple" / "file.txt").write_text("Apple\n")
(zf / "dirs" / "banana").mkdir()
(zf / "dirs" / "banana" / "file.txt").write_text("Banana\n")
(zf / "dirs" / "coconut").mkdir()
(zf / "dirs" / "coconut" / "file.txt").write_text("Coconut\n")
new_dandiset.upload(validation="skip")
download(new_dandiset.dandiset.version_api_url, tmp_path)
assert_dirtrees_eq(zf, tmp_path / new_dandiset.dandiset_id / "sample.zarr")


@pytest.mark.parametrize(
"file_qty,inputs,expected",
[
Expand Down