gs: implement isfile

This commit is contained in:
Saugat Pachhai
2019-12-01 08:56:07 +05:45
parent 25f0e0765c
commit 7e62a7d7e5
2 changed files with 35 additions and 7 deletions
+13 -7
View File
@@ -163,14 +163,20 @@ class RemoteGS(RemoteBASE):
dir_path = path_info / ""
return bool(list(self._list_paths(dir_path, max_items=1)))
def exists(self, path_info):
blob = self.gs.bucket(path_info.bucket).blob(path_info.path)
if blob.exists():
return True
def isfile(self, path_info):
if path_info.path.endswith("/"):
return False
# if the blob does not exist, it could be a part of a directory path
# eg: if `data/file.txt` exists, check for `data` should return True
return self.isdir(path_info / "")
blob = self.gs.bucket(path_info.bucket).blob(path_info.path)
return blob.exists()
def exists(self, path_info):
"""Check if the blob exists. If it does not exist,
it could be a part of a directory path.
eg: if `data/file.txt` exists, check for `data` should return True
"""
return True if self.isfile(path_info) else self.isdir(path_info / "")
def _upload(self, from_file, to_info, name=None, no_progress_bar=True):
bucket = self.gs.bucket(to_info.bucket)
+22
View File
@@ -106,3 +106,25 @@ def test_makedirs(remote):
remote.makedirs(empty_dir)
assert remote.exists(empty_dir)
assert remote.isdir(empty_dir)
@pytest.mark.parametrize("remote", [GCP], indirect=True)
def test_isfile(remote):
test_cases = [
(False, "empty_dir/"),
(True, "empty_file"),
(True, "foo"),
(True, "data/alice"),
(True, "data/alpha"),
(True, "data/subdir/1"),
(True, "data/subdir/2"),
(True, "data/subdir/3"),
(False, "data/subdir/empty_dir/"),
(True, "data/subdir/empty_file"),
(False, "something-that-does-not-exist"),
(False, "data/subdir/empty-file/"),
(False, "empty_dir"),
]
for expected, path in test_cases:
assert remote.isfile(remote.path_info / path) == expected