From 9b16b80ff3a6fccba18cdfc25837b686e9dadad0 Mon Sep 17 00:00:00 2001 From: David de la Iglesia Castro Date: Thu, 12 Jan 2023 11:54:41 +0100 Subject: [PATCH] fs: Accept list of paths in `rm`. To comply with fsspec. --- src/dvc_objects/fs/base.py | 9 +++++++-- src/dvc_objects/fs/local.py | 5 ++++- tests/fs/test_localfs.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/dvc_objects/fs/base.py b/src/dvc_objects/fs/base.py index 8988514..8b0fab8 100644 --- a/src/dvc_objects/fs/base.py +++ b/src/dvc_objects/fs/base.py @@ -378,8 +378,13 @@ def rmdir(self, path: AnyFSPath) -> None: def rm_file(self, path: AnyFSPath) -> None: self.fs.rm_file(path) - def rm(self, path: AnyFSPath) -> None: - self.fs.rm(path, recursive=True) + def rm( + self, + path: Union[AnyFSPath, List[AnyFSPath]], + recursive: bool = False, + **kwargs, + ) -> None: + self.fs.rm(path, recursive=recursive, **kwargs) remove = rm diff --git a/src/dvc_objects/fs/local.py b/src/dvc_objects/fs/local.py index 71e76ef..b4a04bf 100644 --- a/src/dvc_objects/fs/local.py +++ b/src/dvc_objects/fs/local.py @@ -113,7 +113,10 @@ def rm_file(self, path): remove(path) def rm(self, path, recursive=False, maxdepth=None): - remove(path) + if isinstance(path, str): + path = [path] + for p in path: + remove(p) def cp_file(self, path1, path2, **kwargs): return self.copy(path1, path2, **kwargs) diff --git a/tests/fs/test_localfs.py b/tests/fs/test_localfs.py index 4571177..01acf4c 100644 --- a/tests/fs/test_localfs.py +++ b/tests/fs/test_localfs.py @@ -50,6 +50,16 @@ def test_local_fs_isfile(tmp_path): assert not fs.isfile(fspath(tmp_path / "not-existing-file")) +def test_local_fs_rm(tmp_path): + (tmp_path / "file").write_text("file", encoding="utf8") + (tmp_path / "file2").write_text("file2", encoding="utf8") + + fs = LocalFileSystem() + fs.remove([tmp_path / "file", tmp_path / "file2"]) + assert not fs.exists(tmp_path / "file") + assert not fs.exists(tmp_path / "file2") + + def convert_to_sets(walk_results): return [ (root, set(dirs), set(nondirs)) for root, dirs, nondirs in walk_results