Skip to content

Commit

Permalink
Add autodetect_cadet functionality from CADET-Process
Browse files Browse the repository at this point in the history
  • Loading branch information
schmoelder authored and ronald-jaepel committed Mar 18, 2024
1 parent bf7f26d commit 5346ab3
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions cadet/cadet.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,137 @@ def cadet_path(self, value):
def cadet_path(self):
del self._cadet_runner

def autodetect_cadet(self):
"""
Autodetect installation CADET based on operating system and API usage.
Returns
-------
cadet_root : Path
Installation path of the CADET program.
"""
executable = 'cadet-cli'
if platform.system() == 'Windows':
executable += '.exe'

# Searching for the executable in system path
path = shutil.which(executable)

if path is None:
raise FileNotFoundError(
"Could not autodetect CADET installation. Please provide path."
)

cli_path = Path(path)

cadet_root = None
if cli_path is not None:
cadet_root = cli_path.parent.parent
self.install_path = cadet_root

return cadet_root

@property
def install_path(self):
"""str: Path to the installation of CADET.
This can either be the root directory of the installation or the path to the
executable file 'cadet-cli'. If a file path is provided, the root directory will
be inferred.
Raises
------
FileNotFoundError
If CADET cannot be found at the specified path.
Warnings
--------
If the specified install_path is not the root of the CADET installation, it will
be inferred from the file path.
See Also
--------
check_cadet
"""
return self._install_path

@install_path.setter
def install_path(self, install_path):
"""
Set the installation path of CADET.
Parameters
----------
install_path : str or Path
Path to the root of the CADET installation.
It should either be the root directory of the installation or the path
to the executable file 'cadet-cli'.
If a file path is provided, the root directory will be inferred.
"""
if install_path is None:
self._install_path = None
self.cadet_cli_path = None
self.cadet_dll_path = None
self.cadet_create_lwe_path = None
return

install_path = Path(install_path)

if install_path.is_file():
cadet_root = install_path.parent.parent
warnings.warn(
"The specified install_path is not the root of the CADET installation. "
"It has been inferred from the file path."
)
else:
cadet_root = install_path

self._install_path = cadet_root

cli_executable = 'cadet-cli'
lwe_executable = 'createLWE'

if platform.system() == 'Windows':
cli_executable += '.exe'
lwe_executable += '.exe'

cadet_cli_path = cadet_root / 'bin' / cli_executable
if cadet_cli_path.is_file():
self.cadet_cli_path = cadet_cli_path
self.cadet_path = cadet_cli_path
else:
raise FileNotFoundError(
"CADET could not be found. Please check the path"
)

cadet_create_lwe_path = cadet_root / 'bin' / lwe_executable
if cadet_create_lwe_path.is_file():
self.cadet_create_lwe_path = cadet_create_lwe_path.as_posix()

if platform.system() == 'Windows':
dll_path = cadet_root / 'bin' / 'cadet.dll'
dll_debug_path = cadet_root / 'bin' / 'cadet_d.dll'
else:
dll_path = cadet_root / 'lib' / 'lib_cadet.so'
dll_debug_path = cadet_root / 'lib' / 'lib_cadet_d.so'

# Look for debug dll if dll is not found.
if not dll_path.is_file() and dll_debug_path.is_file():
dll_path = dll_debug_path

# Look for debug dll if dll is not found.
if dll_path.is_file():
self.cadet_dll_path = dll_path.as_posix()

if platform.system() != 'Windows':
try:
cadet_lib_path = cadet_root / 'lib'
if cadet_lib_path.as_posix() not in os.environ['LD_LIBRARY_PATH']:
os.environ['LD_LIBRARY_PATH'] += \
os.pathsep + cadet_lib_path.as_posix()
except KeyError:
os.environ['LD_LIBRARY_PATH'] = cadet_lib_path.as_posix()

def transform(self, x):
return str.upper(x)

Expand Down

0 comments on commit 5346ab3

Please sign in to comment.