From 13e5dbafd0aa539f4f161ecbe4393f77412172d8 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Sat, 30 Nov 2019 19:20:32 +0545 Subject: [PATCH 1/5] s3: check for all files in given path --- dvc/remote/s3.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/dvc/remote/s3.py b/dvc/remote/s3.py index 8013f9bb9..e471af617 100644 --- a/dvc/remote/s3.py +++ b/dvc/remote/s3.py @@ -209,8 +209,15 @@ class RemoteS3(RemoteBASE): def exists(self, path_info): dir_path = path_info / "" - fname = next(self._list_paths(path_info, max_items=1), "") - return path_info.path == fname or fname.startswith(dir_path.path) + + if self.isdir(dir_path): + return True + + for fname in self._list_paths(path_info): + if path_info.path == fname: + return True + + return False def makedirs(self, path_info): # We need to support creating empty directories, which means @@ -279,7 +286,7 @@ class RemoteS3(RemoteBASE): ) def walk_files(self, path_info, max_items=None): - for fname in self._list_paths(path_info, max_items): + for fname in self._list_paths(path_info / "", max_items): if fname.endswith("/"): continue From 2e1f3114cd607f52da9ce4a1edcd9ef378c1995a Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Sat, 30 Nov 2019 19:47:30 +0545 Subject: [PATCH 2/5] dvc: write tests --- tests/unit/remote/test_s3.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/unit/remote/test_s3.py b/tests/unit/remote/test_s3.py index 49fc3dbb8..1d188b84c 100644 --- a/tests/unit/remote/test_s3.py +++ b/tests/unit/remote/test_s3.py @@ -13,10 +13,12 @@ def remote(): ├── data │ ├── alice │ ├── alpha + │ ├── subdir-file.txt │ └── subdir │ ├── 1 │ ├── 2 │ └── 3 + ├── data1.txt ├── empty_dir ├── empty_file └── foo @@ -26,6 +28,7 @@ def remote(): s3 = remote.s3 s3.create_bucket(Bucket="bucket") + s3.put_object(Bucket="bucket", Key="data1.txt", Body=b"") s3.put_object(Bucket="bucket", Key="empty_dir/") s3.put_object(Bucket="bucket", Key="empty_file", Body=b"") s3.put_object(Bucket="bucket", Key="foo", Body=b"foo") @@ -34,6 +37,9 @@ def remote(): s3.put_object(Bucket="bucket", Key="data/subdir/1", Body=b"1") s3.put_object(Bucket="bucket", Key="data/subdir/2", Body=b"2") s3.put_object(Bucket="bucket", Key="data/subdir/3", Body=b"3") + s3.put_object( + Bucket="bucket", Key="data/subdir-file.txt", Body=b"subdir" + ) yield remote @@ -66,6 +72,7 @@ def test_exists(remote): (True, "data/subdir/1"), (False, "data/al"), (False, "foo/"), + (True, "data1.txt"), ] for expected, path in test_cases: @@ -76,9 +83,11 @@ def test_walk_files(remote): files = [ remote.path_info / "data/alice", remote.path_info / "data/alpha", + remote.path_info / "data/subdir-file.txt", remote.path_info / "data/subdir/1", remote.path_info / "data/subdir/2", remote.path_info / "data/subdir/3", + remote.path_info / "data1.txt", remote.path_info / "empty_file", remote.path_info / "foo", ] From da0d9e8599357d86d3ed5ad2f45ce03df15d5e0f Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Sun, 1 Dec 2019 10:58:53 +0545 Subject: [PATCH 3/5] s3: implement isfile() --- dvc/remote/s3.py | 25 ++++++++++++++++--------- tests/unit/remote/test_s3.py | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/dvc/remote/s3.py b/dvc/remote/s3.py index e471af617..2f006135c 100644 --- a/dvc/remote/s3.py +++ b/dvc/remote/s3.py @@ -207,17 +207,24 @@ class RemoteS3(RemoteBASE): def list_cache_paths(self): return self._list_paths(self.path_info) + def isfile(self, path_info): + if path_info.path.endswith("/"): + return False + + try: + self.get_head_object(self.s3, path_info.bucket, path_info.path) + except DvcException: + return False + + return True + def exists(self, path_info): - dir_path = path_info / "" + """Check if the blob exists. If it does not exist, + it could be a part of a directory path. - if self.isdir(dir_path): - return True - - for fname in self._list_paths(path_info): - if path_info.path == fname: - return True - - return False + 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 makedirs(self, path_info): # We need to support creating empty directories, which means diff --git a/tests/unit/remote/test_s3.py b/tests/unit/remote/test_s3.py index 1d188b84c..bb0abe594 100644 --- a/tests/unit/remote/test_s3.py +++ b/tests/unit/remote/test_s3.py @@ -118,3 +118,23 @@ def test_makedirs(remote): assert not remote.exists(empty_dir) remote.makedirs(empty_dir) assert remote.exists(empty_dir) + + +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/"), + (False, "data/subdir/1/"), + (False, "something-that-does-not-exist"), + (False, "empty_dir"), + ] + + for expected, path in test_cases: + assert remote.isfile(remote.path_info / path) == expected From 4821aa2efa8536b860bde9f26d181b59edf363c7 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Sun, 1 Dec 2019 14:47:56 +0545 Subject: [PATCH 4/5] s3: catch specific exception when head_object() --- dvc/remote/s3.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dvc/remote/s3.py b/dvc/remote/s3.py index 2f006135c..66e53cebe 100644 --- a/dvc/remote/s3.py +++ b/dvc/remote/s3.py @@ -5,6 +5,7 @@ import logging import os import threading +from botocore.exceptions import ClientError from funcy import cached_property, wrap_prop from dvc.config import Config @@ -212,8 +213,8 @@ class RemoteS3(RemoteBASE): return False try: - self.get_head_object(self.s3, path_info.bucket, path_info.path) - except DvcException: + self.s3.head_object(Bucket=path_info.bucket, Key=path_info.path) + except ClientError: return False return True @@ -224,7 +225,7 @@ class RemoteS3(RemoteBASE): eg: if `data/file.txt` exists, check for `data` should return True """ - return True if self.isfile(path_info) else self.isdir(path_info) + return self.isfile(path_info) or self.isdir(path_info) def makedirs(self, path_info): # We need to support creating empty directories, which means From 37fcced4ad6a6c399717f55658280b4b5e991c3a Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Sun, 1 Dec 2019 16:28:31 +0545 Subject: [PATCH 5/5] s3: raise exception if not 404 status code --- dvc/remote/s3.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dvc/remote/s3.py b/dvc/remote/s3.py index 66e53cebe..8dd2bdf48 100644 --- a/dvc/remote/s3.py +++ b/dvc/remote/s3.py @@ -214,7 +214,9 @@ class RemoteS3(RemoteBASE): try: self.s3.head_object(Bucket=path_info.bucket, Key=path_info.path) - except ClientError: + except ClientError as exc: + if exc.response["Error"]["Code"] != "404": + raise return False return True