Skip to content

Commit fba153e

Browse files
committed
shutil.move entire folder
1 parent 011665c commit fba153e

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/zarr/storage/_local.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,10 @@ async def move(self, dest_root: Path | str) -> None:
259259
"""
260260
if isinstance(dest_root, str):
261261
dest_root = Path(dest_root)
262-
os.makedirs(dest_root, exist_ok=True)
263-
for src_file in self.root.rglob("*"):
264-
if src_file.is_file():
265-
relative_path = src_file.relative_to(self.root)
266-
dest_file_path = dest_root / relative_path
267-
dest_file_path.parent.mkdir(parents=True, exist_ok=True)
268-
shutil.move(str(src_file), str(dest_file_path))
269-
shutil.rmtree(self.root)
262+
os.makedirs(dest_root.parent, exist_ok=True)
263+
if os.path.exists(dest_root):
264+
raise FileExistsError(f"Destination root {dest_root} already exists.")
265+
shutil.move(self.root, dest_root)
270266
self.root = dest_root
271267

272268
async def getsize(self, key: str) -> int:

tests/test_store/test_local.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
import pathlib
44

55
import numpy as np
66
import pytest
@@ -12,9 +12,6 @@
1212
from zarr.testing.store import StoreTests
1313
from zarr.testing.utils import assert_bytes_equal
1414

15-
if TYPE_CHECKING:
16-
import pathlib
17-
1815

1916
class TestLocalStore(StoreTests[LocalStore, cpu.Buffer]):
2017
store_cls = LocalStore
@@ -77,11 +74,20 @@ async def test_get_with_prototype_default(self, store: LocalStore) -> None:
7774
observed = await store.get(key, prototype=None)
7875
assert_bytes_equal(observed, data_buf)
7976

80-
@pytest.mark.parametrize("ndim", [0, 1, 2, 3])
81-
async def test_move(self, tmp_path: pathlib.Path, ndim):
77+
@pytest.mark.parametrize("ndim", [0, 1, 3])
78+
@pytest.mark.parametrize(
79+
"destination", ["destination", "foo/bar/destintion", pathlib.Path("foo/bar/destintion")]
80+
)
81+
async def test_move(
82+
self, tmp_path: pathlib.Path, ndim: int, destination: pathlib.Path | str
83+
) -> None:
8284
origin = tmp_path / "origin"
83-
destination = tmp_path / "destintion"
85+
if isinstance(destination, str):
86+
destination = str(tmp_path / destination)
87+
else:
88+
destination = tmp_path / destination
8489

90+
print(type(destination))
8591
store = await LocalStore.open(root=origin)
8692
shape = (4,) * ndim
8793
chunks = (2,) * ndim
@@ -90,9 +96,13 @@ async def test_move(self, tmp_path: pathlib.Path, ndim):
9096
data = data.reshape(*shape)
9197
array = create_array(store, data=data, chunks=chunks or "auto")
9298

93-
await store.move(str(destination))
99+
await store.move(destination)
94100

95-
assert store.root == destination
96-
assert destination.exists()
101+
assert store.root == pathlib.Path(destination)
102+
assert pathlib.Path(destination).exists()
97103
assert not origin.exists()
98104
assert np.array_equal(array[...], data)
105+
106+
store2 = await LocalStore.open(root=origin)
107+
with pytest.raises(FileExistsError, match=f"Destination root {destination} already exists"):
108+
await store2.move(destination)

tests/test_store/test_zip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_externally_zipped_store(self, tmp_path: Path) -> None:
142142
assert isinstance(group := zipped["foo"], Group)
143143
assert list(group.keys()) == list(group.keys())
144144

145-
async def test_move(self, tmp_path: Path):
145+
async def test_move(self, tmp_path: Path) -> None:
146146
origin = tmp_path / "origin.zip"
147147
destination = tmp_path / "some_folder" / "destination.zip"
148148

0 commit comments

Comments
 (0)