Skip to content
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

Upload files to unique folder on server to avoid conflicts #409

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ansys/dpf/composites/example_helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ContinuousFiberCompositesFiles,
ShortFiberCompositesFiles,
)
from ..server_helpers import upload_file_to_unique_folder

EXAMPLE_REPO = "https://github.com/ansys/example-data/raw/master/pydpf-composites/"

Expand Down Expand Up @@ -145,7 +146,7 @@ def _download_and_upload_file(
urllib.request.urlretrieve(file_url, local_path)
if server.local_server:
return local_path
return cast(str, dpf.upload_file_in_tmp_folder(local_path, server=server))
return upload_file_to_unique_folder(local_path, server=server)


def get_short_fiber_example_files(
Expand Down
2 changes: 2 additions & 0 deletions src/ansys/dpf/composites/server_helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ._load_plugin import load_composites_plugin
from ._upload_files_to_server import (
upload_continuous_fiber_composite_files_to_server,
upload_file_to_unique_folder,
upload_short_fiber_composite_files_to_server,
)
from ._versions import version_equal_or_later, version_older_than
Expand All @@ -16,6 +17,7 @@
"load_composites_plugin",
"connect_to_or_start_server",
"upload_short_fiber_composite_files_to_server",
"upload_file_to_unique_folder",
"upload_continuous_fiber_composite_files_to_server",
"version_older_than",
"version_equal_or_later",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pathlib
from typing import cast
import uuid

import ansys.dpf.core as dpf
from ansys.dpf.core.server_types import BaseServer
Expand All @@ -11,6 +13,31 @@
)


def upload_file_to_unique_folder(path_on_client: _PATH, server: BaseServer) -> str:
"""Upload file to a unique folder on the server.
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
path_on_client:
file path on the client side to upload
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved
server:
Dpf server
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved
"""
tmp_dir = pathlib.Path(
dpf.make_tmp_dir_server()
) # Returns the dpf tmp folder (only one per server)
path_on_client = pathlib.Path(path_on_client)
tmp_dir_unique = tmp_dir / str(uuid.uuid4())
path_on_server = (tmp_dir_unique / path_on_client.name).as_posix()
uploaded_path = cast(str, dpf.upload_file(path_on_client, path_on_server, server=server))
if uploaded_path == "":
raise RuntimeError(
f"Failed to upload file {path_on_client} to server. "
f"Attempted to upload to {path_on_server}"
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved
)
return uploaded_path


def upload_short_fiber_composite_files_to_server(
data_files: ShortFiberCompositesFiles, server: BaseServer
) -> ShortFiberCompositesFiles:
Expand All @@ -27,7 +54,7 @@ def upload_short_fiber_composite_files_to_server(
return data_files

def upload(filename: _PATH) -> str:
return cast(str, dpf.upload_file_in_tmp_folder(filename, server=server))
return upload_file_to_unique_folder(filename, server=server)

return ShortFiberCompositesFiles(
rst=[upload(filename) for filename in data_files.rst],
Expand Down Expand Up @@ -55,7 +82,7 @@ def upload_continuous_fiber_composite_files_to_server(
return data_files

def upload(filename: _PATH) -> _PATH:
return cast(str, dpf.upload_file_in_tmp_folder(filename, server=server))
return upload_file_to_unique_folder(filename, server=server)

all_composite_files = {}
for key, composite_files_by_scope in data_files.composite.items():
Expand Down
8 changes: 5 additions & 3 deletions tests/basic_workflow_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ansys.dpf.core as dpf
import pytest

from ansys.dpf.composites.server_helpers import upload_file_to_unique_folder

from .utils import get_basic_combined_failure_criterion


Expand All @@ -20,10 +22,10 @@ def test_basic_workflow(dpf_server, distributed_rst):
material_path = os.path.join(TEST_DATA_ROOT_DIR, "material.engd")

if not dpf_server.local_server:
rst_paths = [dpf.upload_file_in_tmp_folder(path, server=dpf_server) for path in rst_paths]
rst_paths = [upload_file_to_unique_folder(path, server=dpf_server) for path in rst_paths]

h5_path = dpf.upload_file_in_tmp_folder(h5_path, server=dpf_server)
material_path = dpf.upload_file_in_tmp_folder(material_path, server=dpf_server)
h5_path = upload_file_to_unique_folder(h5_path, server=dpf_server)
material_path = upload_file_to_unique_folder(material_path, server=dpf_server)

eng_data_path = material_path
composite_definitions_path = h5_path
Expand Down
5 changes: 3 additions & 2 deletions tests/element_info_output_all_element_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get_selected_indices,
get_selected_indices_by_dpf_material_ids,
)
from ansys.dpf.composites.server_helpers import upload_file_to_unique_folder


@dataclass(frozen=True)
Expand Down Expand Up @@ -112,7 +113,7 @@ def get_expected_output(
def check_output(rst_file, expected_output):
rst_path = TEST_DATA_ROOT_DIR / "all_element_types" / rst_file
if not dpf_server.local_server:
rst_path = dpf.upload_file_in_tmp_folder(rst_path, server=dpf_server)
rst_path = upload_file_to_unique_folder(rst_path, server=dpf_server)

rst_data_source = dpf.DataSources(rst_path)

Expand Down Expand Up @@ -189,7 +190,7 @@ def get_layup_info_for_rst(rst_file):
rst_path = TEST_DATA_ROOT_DIR / "all_element_types" / rst_file

if not dpf_server.local_server:
rst_path = dpf.upload_file_in_tmp_folder(rst_path, server=dpf_server)
rst_path = upload_file_to_unique_folder(rst_path, server=dpf_server)

rst_data_source = dpf.DataSources(rst_path)

Expand Down
Loading