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 all 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_tmp_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_tmp_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_tmp_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_tmp_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,34 @@
)


def upload_file_to_unique_tmp_folder(path_on_client: _PATH, server: BaseServer) -> str:
"""Upload file to a unique temporary folder on the server.

Parameters
----------
path_on_client:
Client side path of the file which should be uploaded to the server.
server:
DPF server.
"""
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)
if server.os == "posix":
path_on_server = str(tmp_dir) + "/" + str(uuid.uuid4()) + "/" + path_on_client.name
roosre marked this conversation as resolved.
Show resolved Hide resolved
else:
path_on_server = str(tmp_dir) + "\\" + str(uuid.uuid4()) + "\\" + path_on_client.name

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}."
)
return uploaded_path


def upload_short_fiber_composite_files_to_server(
data_files: ShortFiberCompositesFiles, server: BaseServer
) -> ShortFiberCompositesFiles:
Expand All @@ -27,7 +57,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_tmp_folder(filename, server=server)

return ShortFiberCompositesFiles(
rst=[upload(filename) for filename in data_files.rst],
Expand Down Expand Up @@ -55,7 +85,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_tmp_folder(filename, server=server)

all_composite_files = {}
for key, composite_files_by_scope in data_files.composite.items():
Expand Down
10 changes: 7 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_tmp_folder

from .utils import get_basic_combined_failure_criterion


Expand All @@ -20,10 +22,12 @@ 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_tmp_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_tmp_folder(h5_path, server=dpf_server)
material_path = upload_file_to_unique_tmp_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_tmp_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_tmp_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_tmp_folder(rst_path, server=dpf_server)

rst_data_source = dpf.DataSources(rst_path)

Expand Down
11 changes: 8 additions & 3 deletions tests/element_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@ def test_section_definitions_from_multiple_sources(dpf_server):
files, server=dpf_server, default_unit_system=unit_systems.solver_nmm
)

ud_mat_id = composite_model.material_names["Epoxy Carbon UD (230 GPa) Wet"]
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved
woven_mat_id = composite_model.material_names["Epoxy Carbon Woven (230 GPa) Prepreg"]
element_1 = composite_model.get_element_info(1)
assert element_1.is_layered
assert all(element_1.dpf_material_ids == np.array([2, 2, 1, 1, 2, 2]))
assert all(
element_1.dpf_material_ids
== np.array([ud_mat_id, ud_mat_id, woven_mat_id, woven_mat_id, ud_mat_id, ud_mat_id])
)
element_2 = composite_model.get_element_info(2)
assert element_2.is_layered
assert all(element_2.dpf_material_ids == np.array([1, 2, 1]))
assert all(element_2.dpf_material_ids == np.array([woven_mat_id, ud_mat_id, woven_mat_id]))
element_3 = composite_model.get_element_info(3)
assert element_3.is_layered
assert all(element_3.dpf_material_ids == np.array([2]))
assert all(element_3.dpf_material_ids == np.array([ud_mat_id]))

combined_failure_criterion = CombinedFailureCriterion(
"max stress", failure_criteria=[MaxStressCriterion()]
Expand Down
Loading