Skip to content

Commit 98db9dd

Browse files
Fli/improve archive download (#170)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 22972c0 commit 98db9dd

File tree

3 files changed

+115
-9
lines changed

3 files changed

+115
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fli/improve archive download

doc/source/user-guide.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ the project, and then download the project archive to the client:
165165

166166
.. code-block:: python
167167
168-
wb.download_project_archive(archive_name="my_project_archive")
168+
wb.download_project_archive(
169+
archive_name="my_project_archive", include_solution_result_files=False
170+
)
169171
170172
All methods for uploading and downloading files display a progress bar by default. You can
171173
turn off the progress bar with an optional argument:
@@ -197,6 +199,13 @@ and establishes a PyMechanical client.
197199
server_port = wb.start_mechanical_server(system_name=sys_name)
198200
mechanical = connect_to_mechanical(ip="localhost", port=server_port)
199201
202+
The PyMechanical service can be stopped for a given system:
203+
204+
.. code-block:: python
205+
206+
wb.stop_mechanical_server(system_name=sys_name)
207+
208+
200209
PyFluent
201210
--------
202211

@@ -214,6 +223,12 @@ This code starts the PyFluent service and client for a Fluent system created in
214223
server_info_file = wb.start_fluent_server(system_name=sys_name)
215224
fluent = pyfluent.connect_to_fluent(server_info_file_name=server_info_file)
216225
226+
The PyFluent service can be stopped for a given system:
227+
228+
.. code-block:: python
229+
230+
wb.stop_fluent_server(system_name=sys_name)
231+
217232
PySherlock
218233
----------
219234

@@ -230,3 +245,10 @@ This code starts the PySherlock service and client for a Sherlock system created
230245
)
231246
server_port = wb.start_sherlock_server(system_name=sys_name)
232247
sherlock = pysherlock.connect_grpc_channel(port=server_port)
248+
249+
The PySherlock service can be stopped for a given system:
250+
251+
.. code-block:: python
252+
253+
wb.stop_sherlock_server(system_name=sys_name)
254+

src/ansys/workbench/core/workbench_client.py

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from logging.handlers import WatchedFileHandler
3030
import os
3131
import re
32+
import time
3233

3334
import grpc
3435
import tqdm
@@ -372,35 +373,66 @@ def download_file(self, file_name, show_progress=True, target_dir=None):
372373
if os.path.exists(file_path):
373374
os.remove(file_path)
374375
started = True
375-
with open(file_path, mode="ab") as f:
376-
f.write(response.file_content)
376+
377+
try_more = 3
378+
while try_more > 0:
379+
try:
380+
with open(file_path, mode="ab") as f:
381+
f.write(response.file_content)
382+
try_more = 0
383+
except PermissionError:
384+
# intermittent "Permission Denied" error can happen
385+
if try_more <= 0:
386+
raise
387+
try_more -= 1
388+
time.sleep(0.1)
389+
377390
if pbar is not None:
378391
pbar.update(size)
379392
logging.info(f"Dwnloaded the file {file_name}.")
380393
if pbar is not None:
381394
pbar.close()
382395
return file_name
383396

384-
def download_project_archive(self, archive_name, show_progress=True):
397+
def download_project_archive(
398+
self, archive_name, include_solution_result_files=True, show_progress=True
399+
):
385400
"""Create and download the project archive.
386401
387402
Parameters
388403
----------
389404
archive_name : str
390405
Name of the project archive to use, without the file extension.
406+
include_solution_result_files : bool, default: True
407+
Whether to include solution and result files in the archive.
391408
show_progress : bool, default: True
392409
Whether to show a progress bar during the download.
393410
"""
394411
if not re.match(r"^\w+$", archive_name):
395412
logging.error("archive name should contain only alphanumeric characters")
396413
return
397414
script = f"""import os
415+
import json
416+
successful = False
398417
wd = GetServerWorkingDirectory()
399418
if os.path.basename(GetProjectFile()).StartsWith("wbnew."):
400419
Save(FilePath=os.path.join(wd, "{archive_name}.wbpj"), Overwrite=True)
401-
Archive(FilePath=os.path.join(wd, "{archive_name}.wbpz"))
420+
else:
421+
Save(Overwrite=True)
422+
Archive(FilePath=os.path.join(wd, "{archive_name}.wbpz"),
423+
IncludeSkippedFiles={include_solution_result_files})
424+
successful = True
425+
wb_script_result =json.dumps(successful)
402426
"""
403-
self.run_script_string(script)
427+
archive_created = self.run_script_string(script)
428+
if not archive_created:
429+
logging.error(
430+
(
431+
"Failed to create the project archive. "
432+
"Make sure that the solver PyAnsys sessions are closed."
433+
)
434+
)
435+
return
404436
self.download_file(archive_name + ".wbpz", show_progress=show_progress)
405437

406438
def __python_logging(self, log_level, msg):
@@ -490,7 +522,7 @@ def start_mechanical_server(self, system_name):
490522
491523
Examples
492524
--------
493-
Start a PyMechanical session for the given system name.
525+
Start a PyMechanical session for the given system.
494526
495527
>>> from ansys.mechanical.core import connect_to_mechanical
496528
>>> server_port=wb.start_mechanical_server(system_name=mech_system_name)
@@ -505,6 +537,23 @@ def start_mechanical_server(self, system_name):
505537
)
506538
return pymech_port
507539

540+
def stop_mechanical_server(self, system_name):
541+
"""Stop the PyMechanical server for the given system in the Workbench project.
542+
543+
Parameters
544+
----------
545+
system_name : str
546+
Name of the system in the Workbench project.
547+
548+
Examples
549+
--------
550+
Stop the PyMechanical session for the given system.
551+
552+
>>> wb.stop_mechanical_server(system_name=mech_system_name)
553+
554+
"""
555+
self.run_script_string(f"""StopMechanicalServerOnSystem(SystemName="{system_name}")""")
556+
508557
def start_fluent_server(self, system_name):
509558
"""Start the PyFluent server for the given system in the Workbench project.
510559
@@ -521,7 +570,7 @@ def start_fluent_server(self, system_name):
521570
522571
Examples
523572
--------
524-
Start a PyFluent session for the given system name.
573+
Start a PyFluent session for the given system.
525574
526575
>>> import ansys.fluent.core as pyfluent
527576
>>> server_info_file=wb.start_fluent_server(system_name=fluent_sys_name)
@@ -540,6 +589,23 @@ def start_fluent_server(self, system_name):
540589
self.download_file(server_info_file_name, show_progress=False)
541590
return local_copy
542591

592+
def stop_fluent_server(self, system_name):
593+
"""Stop the Fluent server for the given system in the Workbench project.
594+
595+
Parameters
596+
----------
597+
system_name : str
598+
Name of the system in the Workbench project.
599+
600+
Examples
601+
--------
602+
Stop the Fluent session for the given system.
603+
604+
>>> wb.stop_fluent_server(system_name=mech_system_name)
605+
606+
"""
607+
self.run_script_string(f"""StopFluentServerOnSystem(SystemName="{system_name}")""")
608+
543609
def start_sherlock_server(self, system_name):
544610
"""Start the PySherlock server for the given system in the Workbench project.
545611
@@ -555,7 +621,7 @@ def start_sherlock_server(self, system_name):
555621
556622
Examples
557623
--------
558-
Start a PySherlock session for the given system name.
624+
Start a PySherlock session for the given system.
559625
560626
>>> from ansys.sherlock.core import pysherlock
561627
>>> server_port=wb.start_sherlock_server(system_name=sherlock_system_name)
@@ -571,5 +637,22 @@ def start_sherlock_server(self, system_name):
571637
)
572638
return pysherlock_port
573639

640+
def stop_sherlock_server(self, system_name):
641+
"""Stop the Sherlock server for the given system in the Workbench project.
642+
643+
Parameters
644+
----------
645+
system_name : str
646+
Name of the system in the Workbench project.
647+
648+
Examples
649+
--------
650+
Stop the Sherlock session for the given system.
651+
652+
>>> wb.stop_sherlock_server(system_name=mech_system_name)
653+
654+
"""
655+
self.run_script_string(f"""StopSherlockServerOnSystem(SystemName="{system_name}")""")
656+
574657

575658
__all__ = ["WorkbenchClient"]

0 commit comments

Comments
 (0)