Skip to content

Extending data_manager #2196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 6, 2021
10 changes: 10 additions & 0 deletions packages/simcore-sdk/src/simcore_sdk/node_data/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ async def pull(file_or_folder: Path):
archive_to_extract=str(archive_file), destination_folder=file_or_folder
)
log.info("extraction completed")


async def is_file_present_in_storage(file_path: Path) -> bool:
"""
:retruns True if an entry is present inside the files_metadata else False
"""
return await filemanager.is_metadata_for_entry(
store_id=0, # this is for simcore.s3
s3_object=_create_s3_object(file_path),
)
22 changes: 22 additions & 0 deletions packages/simcore-sdk/src/simcore_sdk/node_ports/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,25 @@ async def upload_file(
return store_id, e_tag

raise exceptions.S3InvalidPathError(s3_object)


async def is_metadata_for_entry(store_id: str, s3_object: str) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: i wonder if this coroutine can be cancelled because you are capturing with a broad-except. See this example:

async def foo():
  ...
  await is_metadata_for_entry(..)
  ...
  
# somewhere else, a code calls to cancel task associated to foo
task_foo.cancel()
await task_foo  

"""Returns True if metadata for s3_object is present"""
user_id = config.USER_ID
with api_client() as client:
api = UsersApi(client)
try:
result = await api.get_file_metadata(s3_object, store_id, user_id)
log.debug("Metada request result %s, for s3_object %s", dict(result), s3_object)
is_metadata_present = (
dict(result).get("data", {}).get("object_name", "") == s3_object
)
return is_metadata_present
except Exception: # pylint: disable=broad-except
log.warning(
"There is no metadata for requested store_id=%s s3_object=%s",
store_id,
s3_object,
)
return False

44 changes: 44 additions & 0 deletions packages/simcore-sdk/tests/integration/test_filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,47 @@ async def test_invalid_store(
await filemanager.download_file_from_s3(
store_name=store, s3_object=file_id, local_folder=download_folder
)


async def test_valid_metadata(
tmpdir: Path,
bucket: str,
filemanager_cfg: None,
user_id: str,
file_uuid: str,
s3_simcore_location: str,
):
file_path = Path(tmpdir) / "test.test"
file_path.write_text("I am a test file")
assert file_path.exists()

file_id = file_uuid(file_path)
store_id, e_tag = await filemanager.upload_file(
store_id=s3_simcore_location, s3_object=file_id, local_file_path=file_path
)
assert store_id == s3_simcore_location
assert e_tag

is_metadata_present = await filemanager.is_metadata_for_entry(
store_id=store_id, s3_object=file_id
)

assert is_metadata_present is True


async def test_invaldvalid_metadata(
tmpdir: Path,
bucket: str,
filemanager_cfg: None,
user_id: str,
file_uuid: str,
s3_simcore_location: str,
):
file_path = Path(tmpdir) / "test.test"
file_id = file_uuid(file_path)

is_metadata_present = await filemanager.is_metadata_for_entry(
store_id=s3_simcore_location, s3_object=file_id
)

assert is_metadata_present is False