-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request #2: Allow to make API calls passing instances as arguments
Merge in ESSS/pyrocky from fb-ROCKY-20124-pyrocky-obj-as-args to main * commit '07d8e162aca16b7d57b51b5f8e9cc21ebdd2eb44': Allow to make API calls passing instances as arguments
- Loading branch information
Showing
4 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class PyRockyError(Exception): | ||
"""Generic exception for PyRocky API""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import contextlib | ||
import os | ||
from pathlib import Path | ||
import subprocess | ||
import time | ||
from typing import Optional | ||
|
||
from Pyro5.errors import CommunicationError | ||
|
||
from ansys.rocky.prepost.client import RockyClient, connect_to_rocky | ||
from ansys.rocky.prepost.exceptions import PyRockyError | ||
|
||
|
||
def launch_rocky(rocky_exe: Optional[Path] = None) -> RockyClient: | ||
""" | ||
Launch Rocky executable with PyRocky server enabled, wait Rocky to start up and | ||
return a `RockyClient` instance. | ||
""" | ||
if rocky_exe is None: | ||
for awp_root in ["AWP_ROOT241", "AWP_ROOT232"]: | ||
if awp_root not in os.environ: | ||
continue | ||
|
||
rocky_exe = Path(os.environ[awp_root]) / "Rocky/bin/Rocky.exe" | ||
if rocky_exe.is_file(): | ||
break | ||
else: | ||
raise FileNotFoundError("Rocky executable not found") | ||
else: | ||
if not rocky_exe.is_file(): | ||
raise FileNotFoundError(f"Rocky executable not found at {rocky_exe}") | ||
|
||
cmd = [rocky_exe, "--pyrocky"] | ||
with contextlib.suppress(subprocess.TimeoutExpired): | ||
rocky_process = subprocess.Popen(cmd) | ||
rocky_process.wait(timeout=3) | ||
|
||
# Rocky.exe call returned to soon, something happen | ||
if rocky_process.returncode is not None: | ||
raise RockyLaunchError(f"Error launching Rocky:\n {' '.join(cmd)}") | ||
|
||
client = connect_to_rocky() | ||
|
||
# TODO: A more elegant way to find out that Rocky Pyro server started. | ||
for _ in range(_WAIT_ROCKY_START): | ||
try: | ||
client.api.GetProject() | ||
except CommunicationError: | ||
time.sleep(1) | ||
else: | ||
break | ||
else: | ||
raise RockyLaunchError("Could not connect Rocky remote server: timed out") | ||
|
||
client._process = rocky_process | ||
return client | ||
|
||
|
||
_WAIT_ROCKY_START = 60 | ||
|
||
|
||
class RockyLaunchError(PyRockyError): | ||
"""Raised for errors occurred during Rocky application launch""" |