|
33 | 33 | :author: Fotis Georgatos (Uni.Lu, NTUA) |
34 | 34 | :author: Damian Alvarez (Forschungszentrum Juelich GmbH) |
35 | 35 | """ |
| 36 | +import copy |
36 | 37 | import os |
37 | 38 | import re |
38 | 39 | import sys |
|
45 | 46 | from easybuild.tools.build_log import EasyBuildError |
46 | 47 | from easybuild.tools.config import build_option, get_module_syntax, install_path |
47 | 48 | from easybuild.tools.filetools import convert_name, mkdir, read_file, remove_file, resolve_path, symlink, write_file |
48 | | -from easybuild.tools.modules import ROOT_ENV_VAR_NAME_PREFIX, modules_tool |
| 49 | +from easybuild.tools.modules import ROOT_ENV_VAR_NAME_PREFIX, EnvironmentModulesC, modules_tool |
49 | 50 | from easybuild.tools.utilities import quote_str |
50 | 51 |
|
51 | 52 |
|
@@ -197,6 +198,53 @@ def prepend_paths(self, key, paths, allow_abs=False, expand_relpaths=True): |
197 | 198 | """ |
198 | 199 | return self.update_paths(key, paths, prepend=True, allow_abs=allow_abs, expand_relpaths=expand_relpaths) |
199 | 200 |
|
| 201 | + def modulerc(self, module_version=None): |
| 202 | + """ |
| 203 | + Generate contents of .modulerc file, in Tcl syntax (compatible with all module tools, incl. Lmod) |
| 204 | +
|
| 205 | + :param module_version: specs for module-version statement (dict with 'modname', 'sym_version' & 'version' keys) |
| 206 | + """ |
| 207 | + modulerc = [ModuleGeneratorTcl.MODULE_SHEBANG] |
| 208 | + |
| 209 | + if module_version: |
| 210 | + if isinstance(module_version, dict): |
| 211 | + expected_keys = ['modname', 'sym_version', 'version'] |
| 212 | + if sorted(module_version.keys()) == expected_keys: |
| 213 | + |
| 214 | + module_version_statement = "module-version %(modname)s %(sym_version)s" |
| 215 | + |
| 216 | + # for Environment Modules we need to guard the module-version statement, |
| 217 | + # to avoid "Duplicate version symbol" warning messages where EasyBuild trips over, |
| 218 | + # which occur because the .modulerc is parsed twice |
| 219 | + # "module-info version <arg>" returns its argument if that argument is not a symbolic version (yet), |
| 220 | + # and returns the corresponding real version in case the argument is an existing symbolic version |
| 221 | + # cfr. https://sourceforge.net/p/modules/mailman/message/33399425/ |
| 222 | + if modules_tool().__class__ == EnvironmentModulesC: |
| 223 | + |
| 224 | + modname, sym_version, version = [module_version[key] for key in expected_keys] |
| 225 | + |
| 226 | + # determine module name with symbolic version |
| 227 | + if version in modname: |
| 228 | + # take a copy so we don't modify original value |
| 229 | + module_version = copy.copy(module_version) |
| 230 | + module_version['sym_modname'] = modname.replace(version, sym_version) |
| 231 | + else: |
| 232 | + raise EasyBuildError("Version '%s' does not appear in module name '%s'", version, modname) |
| 233 | + |
| 234 | + module_version_statement = '\n'.join([ |
| 235 | + 'if {"%(sym_modname)s" eq [module-info version %(sym_modname)s]} {', |
| 236 | + ' ' * 4 + module_version_statement, |
| 237 | + "}", |
| 238 | + ]) |
| 239 | + |
| 240 | + modulerc.append(module_version_statement % module_version) |
| 241 | + else: |
| 242 | + raise EasyBuildError("Incorrect module_version spec, expected keys: %s", expected_keys) |
| 243 | + else: |
| 244 | + raise EasyBuildError("Incorrect module_version value type: %s", type(module_version)) |
| 245 | + |
| 246 | + return '\n'.join(modulerc) |
| 247 | + |
200 | 248 | # From this point on just not implemented methods |
201 | 249 |
|
202 | 250 | def check_group(self, group, error_msg=None): |
|
0 commit comments