Skip to content

Commit

Permalink
Fli/improve archive download (#170)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
  • Loading branch information
franknli and pyansys-ci-bot authored Feb 4, 2025
1 parent 22972c0 commit 98db9dd
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/170.documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fli/improve archive download
24 changes: 23 additions & 1 deletion doc/source/user-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ the project, and then download the project archive to the client:

.. code-block:: python
wb.download_project_archive(archive_name="my_project_archive")
wb.download_project_archive(
archive_name="my_project_archive", include_solution_result_files=False
)
All methods for uploading and downloading files display a progress bar by default. You can
turn off the progress bar with an optional argument:
Expand Down Expand Up @@ -197,6 +199,13 @@ and establishes a PyMechanical client.
server_port = wb.start_mechanical_server(system_name=sys_name)
mechanical = connect_to_mechanical(ip="localhost", port=server_port)
The PyMechanical service can be stopped for a given system:

.. code-block:: python
wb.stop_mechanical_server(system_name=sys_name)
PyFluent
--------

Expand All @@ -214,6 +223,12 @@ This code starts the PyFluent service and client for a Fluent system created in
server_info_file = wb.start_fluent_server(system_name=sys_name)
fluent = pyfluent.connect_to_fluent(server_info_file_name=server_info_file)
The PyFluent service can be stopped for a given system:

.. code-block:: python
wb.stop_fluent_server(system_name=sys_name)
PySherlock
----------

Expand All @@ -230,3 +245,10 @@ This code starts the PySherlock service and client for a Sherlock system created
)
server_port = wb.start_sherlock_server(system_name=sys_name)
sherlock = pysherlock.connect_grpc_channel(port=server_port)
The PySherlock service can be stopped for a given system:

.. code-block:: python
wb.stop_sherlock_server(system_name=sys_name)
99 changes: 91 additions & 8 deletions src/ansys/workbench/core/workbench_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from logging.handlers import WatchedFileHandler
import os
import re
import time

import grpc
import tqdm
Expand Down Expand Up @@ -372,35 +373,66 @@ def download_file(self, file_name, show_progress=True, target_dir=None):
if os.path.exists(file_path):
os.remove(file_path)
started = True
with open(file_path, mode="ab") as f:
f.write(response.file_content)

try_more = 3
while try_more > 0:
try:
with open(file_path, mode="ab") as f:
f.write(response.file_content)
try_more = 0
except PermissionError:
# intermittent "Permission Denied" error can happen
if try_more <= 0:
raise
try_more -= 1
time.sleep(0.1)

if pbar is not None:
pbar.update(size)
logging.info(f"Dwnloaded the file {file_name}.")
if pbar is not None:
pbar.close()
return file_name

def download_project_archive(self, archive_name, show_progress=True):
def download_project_archive(
self, archive_name, include_solution_result_files=True, show_progress=True
):
"""Create and download the project archive.
Parameters
----------
archive_name : str
Name of the project archive to use, without the file extension.
include_solution_result_files : bool, default: True
Whether to include solution and result files in the archive.
show_progress : bool, default: True
Whether to show a progress bar during the download.
"""
if not re.match(r"^\w+$", archive_name):
logging.error("archive name should contain only alphanumeric characters")
return
script = f"""import os
import json
successful = False
wd = GetServerWorkingDirectory()
if os.path.basename(GetProjectFile()).StartsWith("wbnew."):
Save(FilePath=os.path.join(wd, "{archive_name}.wbpj"), Overwrite=True)
Archive(FilePath=os.path.join(wd, "{archive_name}.wbpz"))
else:
Save(Overwrite=True)
Archive(FilePath=os.path.join(wd, "{archive_name}.wbpz"),
IncludeSkippedFiles={include_solution_result_files})
successful = True
wb_script_result =json.dumps(successful)
"""
self.run_script_string(script)
archive_created = self.run_script_string(script)
if not archive_created:
logging.error(
(
"Failed to create the project archive. "
"Make sure that the solver PyAnsys sessions are closed."
)
)
return
self.download_file(archive_name + ".wbpz", show_progress=show_progress)

def __python_logging(self, log_level, msg):
Expand Down Expand Up @@ -490,7 +522,7 @@ def start_mechanical_server(self, system_name):
Examples
--------
Start a PyMechanical session for the given system name.
Start a PyMechanical session for the given system.
>>> from ansys.mechanical.core import connect_to_mechanical
>>> server_port=wb.start_mechanical_server(system_name=mech_system_name)
Expand All @@ -505,6 +537,23 @@ def start_mechanical_server(self, system_name):
)
return pymech_port

def stop_mechanical_server(self, system_name):
"""Stop the PyMechanical server for the given system in the Workbench project.
Parameters
----------
system_name : str
Name of the system in the Workbench project.
Examples
--------
Stop the PyMechanical session for the given system.
>>> wb.stop_mechanical_server(system_name=mech_system_name)
"""
self.run_script_string(f"""StopMechanicalServerOnSystem(SystemName="{system_name}")""")

def start_fluent_server(self, system_name):
"""Start the PyFluent server for the given system in the Workbench project.
Expand All @@ -521,7 +570,7 @@ def start_fluent_server(self, system_name):
Examples
--------
Start a PyFluent session for the given system name.
Start a PyFluent session for the given system.
>>> import ansys.fluent.core as pyfluent
>>> server_info_file=wb.start_fluent_server(system_name=fluent_sys_name)
Expand All @@ -540,6 +589,23 @@ def start_fluent_server(self, system_name):
self.download_file(server_info_file_name, show_progress=False)
return local_copy

def stop_fluent_server(self, system_name):
"""Stop the Fluent server for the given system in the Workbench project.
Parameters
----------
system_name : str
Name of the system in the Workbench project.
Examples
--------
Stop the Fluent session for the given system.
>>> wb.stop_fluent_server(system_name=mech_system_name)
"""
self.run_script_string(f"""StopFluentServerOnSystem(SystemName="{system_name}")""")

def start_sherlock_server(self, system_name):
"""Start the PySherlock server for the given system in the Workbench project.
Expand All @@ -555,7 +621,7 @@ def start_sherlock_server(self, system_name):
Examples
--------
Start a PySherlock session for the given system name.
Start a PySherlock session for the given system.
>>> from ansys.sherlock.core import pysherlock
>>> server_port=wb.start_sherlock_server(system_name=sherlock_system_name)
Expand All @@ -571,5 +637,22 @@ def start_sherlock_server(self, system_name):
)
return pysherlock_port

def stop_sherlock_server(self, system_name):
"""Stop the Sherlock server for the given system in the Workbench project.
Parameters
----------
system_name : str
Name of the system in the Workbench project.
Examples
--------
Stop the Sherlock session for the given system.
>>> wb.stop_sherlock_server(system_name=mech_system_name)
"""
self.run_script_string(f"""StopSherlockServerOnSystem(SystemName="{system_name}")""")


__all__ = ["WorkbenchClient"]

0 comments on commit 98db9dd

Please sign in to comment.