|
| 1 | +import re |
| 2 | +from collections import defaultdict |
| 3 | + |
| 4 | +from lxml import etree |
| 5 | + |
| 6 | + |
| 7 | +def extract_machine_config(xml_file, machine, compiler, mpilib): |
| 8 | + """ |
| 9 | + Extract the machine configuration from the XML file. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + xml_file : str |
| 14 | + Path to the XML file. |
| 15 | + machine : str |
| 16 | + Machine name. |
| 17 | + compiler : str |
| 18 | + Compiler name. |
| 19 | + mpilib : str |
| 20 | + MPI library name. |
| 21 | +
|
| 22 | + Returns |
| 23 | + ------- |
| 24 | + etree.Element or None |
| 25 | + The XML element of the machine configuration if found, otherwise None. |
| 26 | + """ |
| 27 | + tree = etree.parse(xml_file) |
| 28 | + root = tree.getroot() |
| 29 | + |
| 30 | + for mach in root.findall('machine'): |
| 31 | + if mach.get('MACH') == machine: |
| 32 | + for mod_sys in mach.findall('module_system'): |
| 33 | + for mod in mod_sys.findall('modules'): |
| 34 | + if not ( |
| 35 | + re.match(mod.get('compiler', '.*'), compiler) |
| 36 | + and re.match(mod.get('mpilib', '.*'), mpilib) |
| 37 | + and re.match(mod.get('DEBUG', '.*'), 'FALSE') |
| 38 | + ): |
| 39 | + mod_sys.remove(mod) |
| 40 | + for env_vars in mach.findall('environment_variables'): |
| 41 | + if not ( |
| 42 | + re.match(env_vars.get('compiler', '.*'), compiler) |
| 43 | + and re.match(env_vars.get('mpilib', '.*'), mpilib) |
| 44 | + and re.match(env_vars.get('DEBUG', '.*'), 'FALSE') |
| 45 | + ): |
| 46 | + mach.remove(env_vars) |
| 47 | + return mach |
| 48 | + return None |
| 49 | + |
| 50 | + |
| 51 | +def config_to_shell_script(config, shell_type): |
| 52 | + """ |
| 53 | + Convert the machine configuration to a shell script. |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + config : etree.Element |
| 58 | + The XML element of the machine configuration. |
| 59 | + shell_type : str |
| 60 | + The type of shell script to generate ('sh' or 'csh'). |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + str |
| 65 | + The shell script as a string. |
| 66 | + """ |
| 67 | + script_lines = [] |
| 68 | + |
| 69 | + # TODO: possibly replace \: with : |
| 70 | + init_path = None |
| 71 | + for init in config.findall('.//module_system/init_path'): |
| 72 | + if init.get('lang') == shell_type: |
| 73 | + init_path = init.text |
| 74 | + break |
| 75 | + |
| 76 | + if init_path is not None: |
| 77 | + init_path = init_path.replace(';', '\n') |
| 78 | + script_lines.append(f'source {init_path}') |
| 79 | + script_lines.append('') |
| 80 | + |
| 81 | + module_commands = defaultdict(list) |
| 82 | + e3sm_hdf5_netcdf_modules = defaultdict(list) |
| 83 | + for module in config.findall('.//module_system/modules'): |
| 84 | + for command in module.findall('command'): |
| 85 | + name = command.get('name') |
| 86 | + value = command.text |
| 87 | + if value: |
| 88 | + if 'command' != 'unload' and 'python' in value: |
| 89 | + # we don't want to load E3SM's python module |
| 90 | + continue |
| 91 | + elif 'command' != 'unload' and re.search( |
| 92 | + r'hdf5|netcdf', value, re.IGNORECASE |
| 93 | + ): |
| 94 | + # we want to remove hdf5 and netcdf in all cases |
| 95 | + e3sm_hdf5_netcdf_modules[name].append(value) |
| 96 | + else: |
| 97 | + module_commands[name].append(value) |
| 98 | + elif name not in module_commands: |
| 99 | + module_commands[name] = [] |
| 100 | + |
| 101 | + script_lines.extend( |
| 102 | + _convert_module_commands_to_script_lines(module_commands, shell_type) |
| 103 | + ) |
| 104 | + script_lines.append('') |
| 105 | + |
| 106 | + if e3sm_hdf5_netcdf_modules: |
| 107 | + script_lines.append('{% if e3sm_hdf5_netcdf %}') |
| 108 | + script_lines.extend( |
| 109 | + _convert_module_commands_to_script_lines( |
| 110 | + e3sm_hdf5_netcdf_modules, shell_type |
| 111 | + ) |
| 112 | + ) |
| 113 | + script_lines.append('{% endif %}') |
| 114 | + script_lines.append('') |
| 115 | + |
| 116 | + script_lines.extend(_convert_env_vars_to_script_lines(config, shell_type)) |
| 117 | + |
| 118 | + script_lines.append('') |
| 119 | + |
| 120 | + return '\n'.join(script_lines) |
| 121 | + |
| 122 | + |
| 123 | +def extract_spack_from_config_machines( |
| 124 | + machine, compiler, mpilib, shell, output |
| 125 | +): |
| 126 | + """ |
| 127 | + Extract machine configuration from XML and write it to a shell script. |
| 128 | +
|
| 129 | + Parameters |
| 130 | + ---------- |
| 131 | + machine : str |
| 132 | + Machine name. |
| 133 | + compiler : str |
| 134 | + Compiler name. |
| 135 | + mpilib : str |
| 136 | + MPI library name. |
| 137 | + shell : str |
| 138 | + Shell script type ('sh' or 'csh'). |
| 139 | + output : str |
| 140 | + Output file to write the shell script. |
| 141 | + """ |
| 142 | + config_filename = 'mache/cime_machine_config/config_machines.xml' |
| 143 | + |
| 144 | + config = extract_machine_config(config_filename, machine, compiler, mpilib) |
| 145 | + if config is None: |
| 146 | + raise ValueError( |
| 147 | + f'No configuration found for machine={machine}, ' |
| 148 | + f'compiler={compiler}, mpilib={mpilib}' |
| 149 | + ) |
| 150 | + |
| 151 | + script = config_to_shell_script(config, shell) |
| 152 | + with open(output, 'w') as f: |
| 153 | + f.write(script) |
| 154 | + |
| 155 | + |
| 156 | +def _convert_module_commands_to_script_lines(module_commands, shell_type): |
| 157 | + """ |
| 158 | + Convert module commands to script lines. |
| 159 | +
|
| 160 | + Parameters |
| 161 | + ---------- |
| 162 | + module_commands : dict |
| 163 | + Dictionary of module commands. |
| 164 | + shell_type : str |
| 165 | + The type of shell script to generate ('sh' or 'csh'). |
| 166 | +
|
| 167 | + Returns |
| 168 | + ------- |
| 169 | + list |
| 170 | + List of script lines. |
| 171 | + """ |
| 172 | + script_lines = [] |
| 173 | + for i, (name, values) in enumerate(module_commands.items()): |
| 174 | + if name == 'unload': |
| 175 | + name = 'rm' |
| 176 | + # the module rm commands produce distracting output so we |
| 177 | + # silence them |
| 178 | + suffix = ' &> /dev/null' |
| 179 | + else: |
| 180 | + suffix = '' |
| 181 | + if values: |
| 182 | + # same code regardless of shell |
| 183 | + if name == 'switch': |
| 184 | + # switch commands need to be handled one at a time |
| 185 | + for value in values: |
| 186 | + script_lines.append(f'module {name} {value}') |
| 187 | + else: |
| 188 | + script_lines.append(f'module {name} \\') |
| 189 | + for value in values: |
| 190 | + script_lines.append(f' {value} \\') |
| 191 | + script_lines[-1] = script_lines[-1].rstrip(' \\') + suffix |
| 192 | + else: |
| 193 | + script_lines.append(f'module {name}{suffix}') |
| 194 | + if i < len(module_commands) - 1: |
| 195 | + script_lines.append('') |
| 196 | + return script_lines |
| 197 | + |
| 198 | + |
| 199 | +def _convert_env_vars_to_script_lines(config, shell_type): |
| 200 | + """ |
| 201 | + Convert environment variables to script lines. |
| 202 | +
|
| 203 | + Parameters |
| 204 | + ---------- |
| 205 | + config : etree.Element |
| 206 | + The XML element of the machine configuration. |
| 207 | + shell_type : str |
| 208 | + The type of shell script to generate ('sh' or 'csh'). |
| 209 | +
|
| 210 | + Returns |
| 211 | + ------- |
| 212 | + list |
| 213 | + List of script lines. |
| 214 | + """ |
| 215 | + script_lines = [] |
| 216 | + e3sm_hdf5_netcdf_env_vars = [] |
| 217 | + for env_var in config.findall('.//environment_variables/env'): |
| 218 | + name = env_var.get('name') |
| 219 | + value = env_var.text |
| 220 | + if value is None: |
| 221 | + value = '' |
| 222 | + if '$SHELL' in value: |
| 223 | + # at least for now, these $SHELL environment variables are |
| 224 | + # E3SM-specific so we'll leave them out |
| 225 | + continue |
| 226 | + if 'perl' in value: |
| 227 | + # we don't want to add E3SM's perl path |
| 228 | + continue |
| 229 | + if name.startswith('OMP_'): |
| 230 | + # OpenMP environment variables cause trouble with ESMF |
| 231 | + continue |
| 232 | + |
| 233 | + value = re.sub(r'\$ENV{([^}]+)}', r'${\1}', value) |
| 234 | + if 'NETCDF' in name and 'PATH' in name: |
| 235 | + e3sm_hdf5_netcdf_env_vars.append((name, value)) |
| 236 | + if name == 'NETCDF_PATH': |
| 237 | + # also set the NETCDF_C_PATH and NETCDF_FORTRAN_PATH, needed |
| 238 | + # by MPAS standalone components |
| 239 | + e3sm_hdf5_netcdf_env_vars.append(('NETCDF_C_PATH', value)) |
| 240 | + e3sm_hdf5_netcdf_env_vars.append( |
| 241 | + ('NETCDF_FORTRAN_PATH', value) |
| 242 | + ) |
| 243 | + else: |
| 244 | + if shell_type == 'sh': |
| 245 | + script_lines.append(f'export {name}="{value}"') |
| 246 | + elif shell_type == 'csh': |
| 247 | + script_lines.append(f'setenv {name} "{value}"') |
| 248 | + |
| 249 | + script_lines.append('') |
| 250 | + |
| 251 | + if e3sm_hdf5_netcdf_env_vars: |
| 252 | + script_lines.append('{% if e3sm_hdf5_netcdf %}') |
| 253 | + for name, value in e3sm_hdf5_netcdf_env_vars: |
| 254 | + if shell_type == 'sh': |
| 255 | + script_lines.append(f'export {name}="{value}"') |
| 256 | + elif shell_type == 'csh': |
| 257 | + script_lines.append(f'setenv {name} "{value}"') |
| 258 | + script_lines.append('{% endif %}') |
| 259 | + |
| 260 | + return script_lines |
0 commit comments