Skip to content

Commit

Permalink
s3 fake directory fix (#206)
Browse files Browse the repository at this point in the history
* [WIP]: taking care of the corner case folder created from S3

* Fix format issues

* [WIP]:updated test case for the s3 file directory test

* removed the linting issue

* Empty commit to rerun the test cases

Co-authored-by: Sabu George <[email protected]>
  • Loading branch information
2 people authored and pjbull committed Feb 6, 2022
1 parent 2084e20 commit 471c6fc
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cloudpathlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, local_cache_dir: Optional[Union[str, os.PathLike]] = None):

def __del__(self) -> None:
# make sure temp is cleaned up if we created it
if self._cache_tmp_dir is not None:
if hasattr(self, "_cache_tmp_dir") and self._cache_tmp_dir is not None:
self._cache_tmp_dir.cleanup()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def __init__(self, cloud_path: Union[str, "CloudPath"], client: Optional["Client

def __del__(self):
# make sure that file handle to local path is closed
if self._handle is not None:
if hasattr(self, "_handle") and self._handle is not None:
self._handle.close()

@property
Expand Down
9 changes: 5 additions & 4 deletions cloudpathlib/s3/s3client.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ def _list_dir(self, cloud_path: S3Path, recursive=False) -> Iterable[Tuple[S3Pat

# files in the directory
for result_key in result.get("Contents", []):
yield (
self.CloudPath(f"s3://{cloud_path.bucket}/{result_key.get('Key')}"),
False,
)
if result_key.get("Size") > 0:
yield (
self.CloudPath(f"s3://{cloud_path.bucket}/{result_key.get('Key')}"),
False
)

def _move_file(self, src: S3Path, dst: S3Path, remove_src: bool = True) -> S3Path:
# just a touch, so "REPLACE" metadata
Expand Down
1 change: 1 addition & 0 deletions tests/assets/dir_0/file0_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file0_0.txt
1 change: 1 addition & 0 deletions tests/assets/dir_0/file0_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file0_1.txt
1 change: 1 addition & 0 deletions tests/assets/dir_0/file0_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file0_2.txt
1 change: 1 addition & 0 deletions tests/assets/dir_1/dir_1_0/file_1_0_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file_1_0_0.txt
1 change: 1 addition & 0 deletions tests/assets/dir_1/file_1_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file_1_0.txt
5 changes: 4 additions & 1 deletion tests/mock_clients/mock_s3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import collections
from datetime import datetime
from pathlib import Path, PurePosixPath
Expand Down Expand Up @@ -205,6 +206,8 @@ def paginate(self, Bucket=None, Prefix="", Delimiter=None):
{"Prefix": str(_.relative_to(self.root).as_posix())} for _ in page if _.is_dir()
]
files = [
{"Key": str(_.relative_to(self.root).as_posix())} for _ in page if _.is_file()
{"Key": str(_.relative_to(self.root).as_posix()), "Size": os.stat(_).st_size}
for _ in page
if _.is_file()
]
yield {"CommonPrefixes": dirs, "Contents": files}

0 comments on commit 471c6fc

Please sign in to comment.