|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +from compass import __version__ as compass_version |
| 7 | +from jinja2 import Template |
| 8 | +from importlib import resources |
| 9 | + |
| 10 | + |
| 11 | +def get_conda_base_and_env(): |
| 12 | + if 'CONDA_EXE' in os.environ: |
| 13 | + conda_exe = os.environ['CONDA_EXE'] |
| 14 | + conda_base = os.path.abspath( |
| 15 | + os.path.join(conda_exe, '..', '..')) |
| 16 | + else: |
| 17 | + raise ValueError('No conda executable detected.') |
| 18 | + |
| 19 | + if 'CONDA_DEFAULT_ENV' in os.environ: |
| 20 | + conda_env = os.environ['CONDA_DEFAULT_ENV'] |
| 21 | + else: |
| 22 | + raise ValueError('No conda environment detected.') |
| 23 | + |
| 24 | + return conda_base, conda_env |
| 25 | + |
| 26 | + |
| 27 | +def get_mpi(): |
| 28 | + |
| 29 | + for mpi in ['mpich', 'openmpi']: |
| 30 | + check = subprocess.check_output( |
| 31 | + ['conda', 'list', 'mpich']).decode('utf-8') |
| 32 | + if mpi in check: |
| 33 | + return mpi |
| 34 | + |
| 35 | + return None |
| 36 | + |
| 37 | + |
| 38 | +def main(): |
| 39 | + parser = argparse.ArgumentParser( |
| 40 | + description='Generate a load script for a Linux or OSX machine') |
| 41 | + parser.parse_args() |
| 42 | + |
| 43 | + conda_base, conda_env = get_conda_base_and_env() |
| 44 | + |
| 45 | + mpi = get_mpi() |
| 46 | + |
| 47 | + if mpi is None: |
| 48 | + suffix = '' |
| 49 | + else: |
| 50 | + suffix = f'_{mpi}' |
| 51 | + |
| 52 | + if sys.platform == 'Linux': |
| 53 | + env_vars = 'export MPAS_EXTERNAL_LIBS="-lgomp"' |
| 54 | + else: |
| 55 | + env_vars = '' |
| 56 | + |
| 57 | + script_filename = f'load_compass_{compass_version}{suffix}.sh' |
| 58 | + script_filename = os.path.abspath(script_filename) |
| 59 | + |
| 60 | + template = Template(resources.read_text( |
| 61 | + 'compass.load', 'load_script.template')) |
| 62 | + text = template.render(conda_base=conda_base, conda_env=conda_env, |
| 63 | + env_vars=env_vars, load_script=script_filename) |
| 64 | + |
| 65 | + with open(script_filename, 'w') as handle: |
| 66 | + handle.write(text) |
0 commit comments