Skip to content

Commit da0d9e8

Browse files
committed
s3: implement isfile()
1 parent 2e1f311 commit da0d9e8

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

dvc/remote/s3.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,24 @@ def _list_paths(self, path_info, max_items=None):
207207
def list_cache_paths(self):
208208
return self._list_paths(self.path_info)
209209

210-
def exists(self, path_info):
211-
dir_path = path_info / ""
210+
def isfile(self, path_info):
211+
if path_info.path.endswith("/"):
212+
return False
212213

213-
if self.isdir(dir_path):
214-
return True
214+
try:
215+
self.get_head_object(self.s3, path_info.bucket, path_info.path)
216+
except DvcException:
217+
return False
215218

216-
for fname in self._list_paths(path_info):
217-
if path_info.path == fname:
218-
return True
219+
return True
220+
221+
def exists(self, path_info):
222+
"""Check if the blob exists. If it does not exist,
223+
it could be a part of a directory path.
219224
220-
return False
225+
eg: if `data/file.txt` exists, check for `data` should return True
226+
"""
227+
return True if self.isfile(path_info) else self.isdir(path_info)
221228

222229
def makedirs(self, path_info):
223230
# We need to support creating empty directories, which means

tests/unit/remote/test_s3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,23 @@ def test_makedirs(remote):
118118
assert not remote.exists(empty_dir)
119119
remote.makedirs(empty_dir)
120120
assert remote.exists(empty_dir)
121+
122+
123+
def test_isfile(remote):
124+
test_cases = [
125+
(False, "empty_dir/"),
126+
(True, "empty_file"),
127+
(True, "foo"),
128+
(True, "data/alice"),
129+
(True, "data/alpha"),
130+
(True, "data/subdir/1"),
131+
(True, "data/subdir/2"),
132+
(True, "data/subdir/3"),
133+
(False, "data/subdir/empty_dir/"),
134+
(False, "data/subdir/1/"),
135+
(False, "something-that-does-not-exist"),
136+
(False, "empty_dir"),
137+
]
138+
139+
for expected, path in test_cases:
140+
assert remote.isfile(remote.path_info / path) == expected

0 commit comments

Comments
 (0)