51 lines
2.3 KiB
Python
51 lines
2.3 KiB
Python
from typing import Set
|
|
from pathlib import Path
|
|
import subprocess
|
|
import re
|
|
import logging
|
|
|
|
EnvModule = str # eg compilers/ifort/latest
|
|
|
|
|
|
def get_available_modules() -> Set[EnvModule]:
|
|
available_modules = set()
|
|
completed_process = subprocess.run('module avail --terse', executable='/bin/bash', shell=True, capture_output=True, check=True) # for some reason, module is only found when executable is /bin/bash
|
|
# (iprbench.venv) graffy@alambix50:/opt/ipr/cluster/work.local/graffy/bug3958/iprbench.git$ module avail --terse
|
|
# /usr/share/modules/modulefiles:
|
|
# compilers/ifort/15.0.2
|
|
# compilers/ifort/17.0.1
|
|
# ...
|
|
# lib/mpi/intelmpi/2021.13.0
|
|
# lib/mpi/intelmpi/latest
|
|
# module-git
|
|
# module-info
|
|
# modules
|
|
# null
|
|
stdout = completed_process.stdout.decode('utf-8')
|
|
logging.debug('get_available_modules: stdout="%s"', stdout)
|
|
for line in stdout.splitlines():
|
|
logging.debug('available module line: "%s"', line)
|
|
if not re.search(r'\:$', line): # ignore the directories such as '/usr/share/modules/modulefiles:'
|
|
available_modules.add(EnvModule(line))
|
|
return available_modules
|
|
|
|
|
|
def get_module_file_path(env_module: EnvModule) -> Path:
|
|
# graffy@alambix-frontal:~$ module help compilers/ifort/latest
|
|
# -------------------------------------------------------------------
|
|
# Module Specific Help for /usr/share/modules/modulefiles/compilers/ifort/latest:
|
|
|
|
# Provides the same functionality as the command '/opt/intel/oneapi-2024.2.1/compiler/latest/env/vars.sh intel64'
|
|
# -------------------------------------------------------------------
|
|
module_file_path = None
|
|
completed_process = subprocess.run(f'module help {env_module}', executable='/bin/bash', shell=True, capture_output=True, check=True) # for some reason, module is only found when executable is /bin/bash
|
|
stdout = completed_process.stdout.decode('utf-8')
|
|
for line in stdout.splitlines():
|
|
match = re.match(r'^Module Specific Help for (?P<module_file_path>[^:]+):', line)
|
|
if match:
|
|
module_file_path = Path(match['module_file_path'])
|
|
break
|
|
if module_file_path is None:
|
|
raise ValueError(f'failed to find the file path of the environment module {env_module}')
|
|
return module_file_path
|