Skip to content

Commit 6a64a99

Browse files
Improve automatic distro detection (#93)
Co-authored-by: Mario Ostieri <[email protected]>
1 parent 4e58d32 commit 6a64a99

File tree

1 file changed

+81
-34
lines changed

1 file changed

+81
-34
lines changed

src/ansys/dynamicreporting/core/adr_service.py

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515

1616
import atexit
17+
import glob
1718
import os
1819
import re
1920
import shutil
@@ -125,14 +126,14 @@ def __init__(
125126
Parameters
126127
----------
127128
ansys_installation : str, optional
128-
Locatation of the Ansys installation, including the version directory.
129+
Location of the Ansys installation, including the version directory.
129130
For example, r'C:\\Program Files\\ANSYS Inc\\v232'. The default is
130131
``None``. This parameter is needed only if the Service instance is
131132
to launch a dynamic Reporting service. It is not needed if connecting
132133
to an existing service. If there is no local Ansys installation and
133134
a Docker image is to be used instead, enter ``"docker"``.
134135
docker_image : str, optional
135-
Location of the Docker image for Ansys Dynamic Reporting. The defaults
136+
Location of the Docker image for Ansys Dynamic Reporting. The default
136137
is ghcr.io/ansys-internal/nexus. This parameter is used only if the
137138
value for the ``ansys_installation`` parameter is set to ``"docker"``.
138139
Default:
@@ -220,39 +221,85 @@ def __init__(
220221
self.logger.error(f"Error starting the Docker Container.\n{str(e)}\n")
221222
raise e
222223

223-
elif ansys_installation: # pragma: no cover
224-
# verify path
225-
if not os.path.isdir(ansys_installation):
226-
raise InvalidAnsysPath(ansys_installation)
227-
228-
if ansys_installation != "docker" and ansys_installation is not None: # pragma: no cover
229-
# Not using docker
230-
# Backward compatibility: if the path passed is only up to the version directory,
231-
# append the CEI directory
232-
if ansys_installation.endswith("CEI") is False:
233-
ansys_installation = os.path.join(ansys_installation, "CEI")
234-
self._ansys_installation = ansys_installation
235-
# verify new path
236-
if not os.path.isdir(ansys_installation):
237-
# Option for local development build
238-
if os.environ.get("CEIDEVROOTDOS") is not None:
239-
self._ansys_installation = os.environ.get("CEIDEVROOTDOS")
240-
else:
241-
raise InvalidAnsysPath(ansys_installation)
242-
if self._ansys_version is None:
243-
# try to get version from install path
244-
matches = re.search(r".*v([0-9]{3}).*", self._ansys_installation)
245-
if matches is None:
246-
# Option for local development build
247-
if os.environ.get("ANSYS_REL_INT_I") is not None:
248-
self._ansys_version = int(os.environ.get("ANSYS_REL_INT_I"))
224+
else: # pragma: no cover
225+
# Not using docker. Make a list of directory names to consider:
226+
# 1) any passed ansys_installation directory
227+
# 2) any passed ansys_installation directory with 'CEI' dir appended
228+
# 3) the 'enve.home()' directory (if enve will load)
229+
# 4) the latest ansys installation via AWP_ROOTxyz environmental variable
230+
# 5) CEIDEVROOTDOS environmental variable
231+
#
232+
dirs_to_check = []
233+
if ansys_installation:
234+
dirs_to_check.append(ansys_installation)
235+
dirs_to_check.append(os.path.join(ansys_installation, "CEI"))
236+
237+
# if we are running from a distro "EnSight" cpython, enve might be there
238+
try:
239+
import enve
240+
241+
dirs_to_check.append(enve.home())
242+
except ModuleNotFoundError:
243+
pass
244+
245+
# Find via AWP_ROOTxyz envvar... Most recent version installed
246+
env_name = "AWP_ROOT000"
247+
for name in os.environ.keys():
248+
if name.startswith("AWP_ROOT"):
249+
env_name = max(env_name, name)
250+
if env_name in os.environ:
251+
# add AWP_ROOT{max}/CEI to the list of directories to search
252+
dirs_to_check.append(os.path.join(os.environ[env_name], "CEI"))
253+
254+
# Option for local development build
255+
if os.environ.get("CEIDEVROOTDOS") is not None:
256+
dirs_to_check.append(os.environ.get("CEIDEVROOTDOS"))
257+
258+
# Check all the potential local directories for the distro
259+
# bin/cpython should be in the server distro
260+
found = False
261+
for install_dir in dirs_to_check:
262+
cpython_name = os.path.join(install_dir, "bin", "cpython")
263+
if os.path.isfile(cpython_name):
264+
self._ansys_installation = install_dir
265+
found = True
266+
break
267+
268+
# Should we raise an exception here? If no ansys_installation was
269+
# passed, then no but the user can only connect to existing servers
270+
# as per docs. If ansys_installation was passed, then there is an
271+
# exception if the passed value is not the root of the installation.
272+
# Basically, the passed install path was illegal.
273+
if ansys_installation:
274+
if not self._ansys_installation.startswith(ansys_installation):
275+
raise InvalidAnsysPath(ansys_installation)
276+
if not found:
277+
self._ansys_installation = None
278+
else:
279+
# populate the version number
280+
if self._ansys_version is None:
281+
# An ansys distro include a v??? directory name. This is the fallback.
282+
matches = re.search(r".*v([0-9]{3}).*", self._ansys_installation)
283+
# Try to get version from install path bin\cei_python??? name
284+
# Build the cpython path glob name:
285+
cpython_glob = os.path.join(
286+
self._ansys_installation, "bin", "cpython[0-9][0-9][0-9]"
287+
)
288+
cpython_name = glob.glob(cpython_glob)
289+
# is the file there (it should be...)
290+
if len(cpython_name):
291+
matches = re.search(r".*cpython([0-9]{3})", cpython_name[0])
292+
if matches is None:
293+
# Option for local development build
294+
if "ANSYS_REL_INT_I" in os.environ:
295+
self._ansys_version = int(os.environ.get("ANSYS_REL_INT_I"))
296+
else:
297+
raise AnsysVersionAbsentError
249298
else:
250-
raise AnsysVersionAbsentError
251-
else:
252-
try:
253-
self._ansys_version = int(matches.group(1))
254-
except IndexError:
255-
raise AnsysVersionAbsentError
299+
try:
300+
self._ansys_version = int(matches.group(1))
301+
except IndexError:
302+
raise AnsysVersionAbsentError
256303

257304
@property
258305
def session_guid(self):

0 commit comments

Comments
 (0)