@@ -1106,6 +1106,46 @@ def check_python_version():
11061106 return (python_maj_ver , python_min_ver )
11071107
11081108
1109+ def pick_system_specific_value (description , options_or_value , allow_none = False ):
1110+ """Pick an entry for the current system when the input has multiple options
1111+
1112+ :param description: Descriptive string about the value to be retrieved. Used for logging.
1113+ :param options_or_value: Either a dictionary with options to choose from or a value of any other type
1114+ :param allow_none: When True and no matching arch key was found, return None instead of an error
1115+
1116+ :return options_or_value when it is not a dictionary or the matching entry (if existing)
1117+ """
1118+ result = options_or_value
1119+ if isinstance (options_or_value , dict ):
1120+ if not options_or_value :
1121+ raise EasyBuildError ("Found empty dict as %s!" , description )
1122+ other_keys = [x for x in options_or_value .keys () if not x .startswith (ARCH_KEY_PREFIX )]
1123+ if other_keys :
1124+ other_keys = ',' .join (sorted (other_keys ))
1125+ raise EasyBuildError ("Unexpected keys in %s: %s (only '%s' keys are supported)" ,
1126+ description , other_keys , ARCH_KEY_PREFIX )
1127+ host_arch_key = ARCH_KEY_PREFIX + get_cpu_architecture ()
1128+ star_arch_key = ARCH_KEY_PREFIX + '*'
1129+ # check for specific 'arch=' key first
1130+ try :
1131+ result = options_or_value [host_arch_key ]
1132+ _log .info ("Selected %s from %s for %s (using key %s)" ,
1133+ result , options_or_value , description , host_arch_key )
1134+ except KeyError :
1135+ # fall back to 'arch=*'
1136+ try :
1137+ result = options_or_value [star_arch_key ]
1138+ _log .info ("Selected %s from %s for %s (using fallback key %s)" ,
1139+ result , options_or_value , description , star_arch_key )
1140+ except KeyError :
1141+ if allow_none :
1142+ result = None
1143+ else :
1144+ raise EasyBuildError ("No matches for %s in %s (looking for %s)" ,
1145+ description , options_or_value , host_arch_key )
1146+ return result
1147+
1148+
11091149def pick_dep_version (dep_version ):
11101150 """
11111151 Pick the correct dependency version to use for this system.
@@ -1115,39 +1155,14 @@ def pick_dep_version(dep_version):
11151155
11161156 Return value is the version to use.
11171157 """
1118- if isinstance (dep_version , string_type ):
1119- _log .debug ("Version is already a string ('%s'), OK" , dep_version )
1120- result = dep_version
1121-
1122- elif dep_version is None :
1158+ if dep_version is None :
11231159 _log .debug ("Version is None, OK" )
11241160 result = None
1125-
1126- elif isinstance (dep_version , dict ):
1127- arch_keys = [x for x in dep_version .keys () if x .startswith (ARCH_KEY_PREFIX )]
1128- other_keys = [x for x in dep_version .keys () if x not in arch_keys ]
1129- if other_keys :
1130- other_keys = ',' .join (sorted (other_keys ))
1131- raise EasyBuildError ("Unexpected keys in version: %s (only 'arch=' keys are supported)" , other_keys )
1132- if arch_keys :
1133- host_arch_key = ARCH_KEY_PREFIX + get_cpu_architecture ()
1134- star_arch_key = ARCH_KEY_PREFIX + '*'
1135- # check for specific 'arch=' key first
1136- if host_arch_key in dep_version :
1137- result = dep_version [host_arch_key ]
1138- _log .info ("Version selected from %s using key %s: %s" , dep_version , host_arch_key , result )
1139- # fall back to 'arch=*'
1140- elif star_arch_key in dep_version :
1141- result = dep_version [star_arch_key ]
1142- _log .info ("Version selected for %s using fallback key %s: %s" , dep_version , star_arch_key , result )
1143- else :
1144- raise EasyBuildError ("No matches for version in %s (looking for %s)" , dep_version , host_arch_key )
1145- else :
1146- raise EasyBuildError ("Found empty dict as version!" )
1147-
11481161 else :
1149- typ = type (dep_version )
1150- raise EasyBuildError ("Unknown value type for version: %s (%s), should be string value" , typ , dep_version )
1162+ result = pick_system_specific_value ("version" , dep_version )
1163+ if not isinstance (result , string_type ):
1164+ typ = type (dep_version )
1165+ raise EasyBuildError ("Unknown value type for version: %s (%s), should be string value" , typ , dep_version )
11511166
11521167 return result
11531168
0 commit comments