-
Notifications
You must be signed in to change notification settings - Fork 1
Added spack module #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v8
Are you sure you want to change the base?
Added spack module #367
Changes from all commits
599fabd
f1671aa
5e5a658
fa0f5e9
e45f18d
3d69f7c
a8db29f
c8afc0c
775fb86
c9c781f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment on this file: This is an example. If we choose this method, this file in the repo should probably be dynamic, with the version set at deployment time. Other solutions (which would not require a dynamic file) could be:
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #%Module1.0 | ||
| module-version spack/1.1 default |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # Set TCL variables passed as arguments | ||
| # old environment env var | ||
| old_env_var_name="$1" | ||
| # setup-env script | ||
| setup_env_script="$2" | ||
| # python path used by spack | ||
| spack_python="$3" | ||
| # spack root | ||
| spack_root="$4" | ||
|
|
||
| # Create a name reference variable, so we can use the reference to dynamically set/update the variable | ||
| declare -n old_env_var_reference="$old_env_var_name" | ||
|
|
||
| # Set regex for bash functions in the environment printout | ||
| function_regex='^BASH_FUNC_(.+)%%=\(\) (.*)' | ||
|
|
||
| # ============================================================================== | ||
| # Capture the environment and aliases before sourcing the setup-env script | ||
| # ============================================================================== | ||
| # Functions and variables | ||
| declare -A before_functions | ||
| declare -A before_variables | ||
| while IFS= read -r -d '' entry; do | ||
| if [[ "$entry" =~ $function_regex ]]; then | ||
| # Set function | ||
| name="${BASH_REMATCH[1]}" | ||
| value="${BASH_REMATCH[2]}" | ||
| before_functions["$name"]="$value" | ||
| else | ||
| # Set variable | ||
| name="${entry%%=*}" | ||
| value="${entry#*=}" | ||
| before_variables["$name"]="$value" | ||
| fi | ||
| done < <(env -0) | ||
| #Aliases | ||
| declare -A before_aliases | ||
| while IFS= read -r entry; do | ||
| entry="${entry#alias }" | ||
| name="${entry%%=*}" | ||
| value="${entry#*=}" | ||
| before_aliases["$name"]="$value" | ||
| done < <(alias -p) | ||
|
|
||
| # ============================================================================== | ||
| # Source the setup-env script | ||
| # ============================================================================== | ||
| # Set up SPACK_PYTHON | ||
| export SPACK_PYTHON="$spack_python" | ||
| # Set up SPACK_ROOT | ||
| export SPACK_ROOT="$spack_root" | ||
| source "$setup_env_script" > /dev/null | ||
|
|
||
| # ============================================================================== | ||
| # Capture the environment and aliases after sourcing the setup-env script | ||
| # ============================================================================== | ||
| # Functions and variables | ||
| declare -A after_functions | ||
| declare -A after_variables | ||
| while IFS= read -r -d '' entry; do | ||
| if [[ "$entry" =~ $function_regex ]]; then | ||
| # Set function | ||
| name="${BASH_REMATCH[1]}" | ||
| value="${BASH_REMATCH[2]}" | ||
| after_functions["$name"]="$value" | ||
| else | ||
| # Set variable | ||
| name="${entry%%=*}" | ||
| value="${entry#*=}" | ||
| after_variables["$name"]="$value" | ||
| fi | ||
| done < <(env -0) | ||
| # Aliases | ||
| declare -A after_aliases | ||
| while IFS= read -r entry; do | ||
| entry="${entry#alias }" | ||
| name="${entry%%=*}" | ||
| value="${entry#*=}" | ||
| after_aliases["$name"]="$value" | ||
| done < <(alias -p) | ||
|
|
||
| # ============================================================================== | ||
| # Parse before and after variables, functions and aliases to find addition, changes and removals | ||
| # ============================================================================== | ||
| # FUNCTIONS | ||
| # Added and changed | ||
| for name in "${!after_functions[@]}"; do | ||
| value="${after_functions[$name]}" | ||
| if [[ ! -v before_functions["$name"] ]]; then | ||
| # Added function: print command for it to be exported and record the command for it to be unset | ||
| printf '%s ; ' "${name}() $value ; export -f $name" | ||
| old_env_var_reference+=$(printf '%s ;' "unset -f $name") | ||
| elif [[ "${before_functions[$name]}" != "$value" ]]; then | ||
| # Changed function: print command for it to be exported and record command for it to be reinstated | ||
| printf '%s ; ' "${name}() $value ; export -f $name" | ||
| old_value="${before_functions["$name"]}" | ||
| old_env_var_reference+=$(printf '%s ; ' "${name}() $old_value ; export -f $name") | ||
| fi | ||
| done | ||
| # Removed | ||
| for name in "${!before_functions[@]}"; do | ||
| if [[ ! -v after_functions["$name"] ]]; then | ||
| # Removed function: record command for it to be reinstated | ||
| old_value="${before_functions[$name]}" | ||
| old_env_var_reference+=$(printf '%s ; ' "${name}() $old_value ; export -f $name") | ||
| fi | ||
| done | ||
| # VARIABLES | ||
| # Added and changed | ||
| for name in "${!after_variables[@]}"; do | ||
| value="${after_variables[$name]}" | ||
| if [[ ! -v before_variables["$name"] ]]; then | ||
| # Added variable: print command for it to be exported and record the command for it to be unset | ||
| printf '%s ; ' "export $name=$value" | ||
| old_env_var_reference+=$(printf '%s ; ' "unset $name") | ||
| elif [[ "${before_variables[$name]}" != "$value" ]]; then | ||
| # Changed variable: print command for it to be exported and record command for it to be reinstated | ||
| printf '%s ; ' "export $name=$value" | ||
| old_value="${before_variables["$name"]}" | ||
| old_env_var_reference+=$(printf '%s ; ' "export $name=$old_value") | ||
| fi | ||
| done | ||
| # Removed | ||
| for name in "${!before_variables[@]}"; do | ||
| if [[ ! -v after_variables["$name"] ]]; then | ||
| # Removed variable: record command for it to be reinstated | ||
| old_value="${before_variables["$name"]}" | ||
| old_env_var_reference+=$(printf '%s ; ' "export $name=$old_value") | ||
| fi | ||
| done | ||
| # ALIASES | ||
| # Added and changed | ||
| for name in "${!after_aliases[@]}"; do | ||
| value="${after_aliases[$name]}" | ||
| if [[ ! -v before_aliases["$name"] ]]; then | ||
| # Added aliase: print command for it to be set and record the command for it to be unset | ||
| printf '%s ; ' "alias $name=$value" | ||
| old_env_var_reference+=$(printf '%s ; ' "unalias $name") | ||
| elif [[ "${before_aliases[$name]}" != "$value" ]]; then | ||
| # Changed aliase: print command for it to be set and record command for it to be reinstated | ||
| printf '%s ; ' "alias $name=$value" | ||
| old_value="${before_aliases["$name"]}" | ||
| old_env_var_reference+=$(printf '%s ; ' "alias $name=$old_value") | ||
| fi | ||
| done | ||
| # Removed | ||
| for name in "${!before_aliases[@]}"; do | ||
| if [[ ! -v after_aliases["$name"] ]]; then | ||
| # Removed aliase: record command for it to be unset | ||
| old_value="${before_aliases["$name"]}" | ||
| old_env_var_reference+=$(printf '%s ; ' "unalias $name") | ||
| fi | ||
| done | ||
|
|
||
| # Print a null character as a separator, to help splitting the commands to set the new environment | ||
| # from those to reinstate the old environment | ||
| printf '\x00' | ||
| # Print the commands to reinstate the old environment | ||
| echo "${!old_env_var_name}" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #%Module1.0 | ||
|
|
||
| # Get module name and version | ||
| lassign [split [module-info name] {/}] module_name module_version | ||
|
|
||
| conflict $module_name | ||
|
|
||
| # Get the spack root directory for the spack instance. | ||
| # First, we get this modulefile parent directory, then we construct the spack root as | ||
| # <parent-directory>/../../apps/spack/<version>/spack | ||
| set spack_module_dir [file dirname [info script]] | ||
| set base_dir [file normalize "$spack_module_dir/../../apps/spack/$module_version"] | ||
| set spack_root "$base_dir/spack" | ||
|
|
||
| # Set spack setup-env.sh | ||
| set setup_env "$spack_root/share/spack/setup-env.sh" | ||
|
|
||
| # Set Python to be used with spack | ||
| set spack_python /bin/python3 | ||
|
|
||
| # Set the name of the env variable used to store the current environment information so | ||
| # the environment can be reinstated when this module is unloaded | ||
| set old_env_var_name _SPACK_MODULE_OLD_ENV | ||
|
|
||
| # If the module is loaded | ||
| if {[module-info mode load]} { | ||
| ### Set spack environment | ||
| # Since the Module version is < 5.0 we cannot directly source spack's setup-env script within this modulefile. | ||
| # A work-around is to run a bash script that computes the environment before and after sourcing the spack's | ||
| # setup-env and: | ||
| # - When the module is loaded, set the new variables and functions. | ||
| # When the module is unloaded, unset them. | ||
| # - When the module is loaded, set the variables and functions that changed to their new values. | ||
| # When the module is unloaded, re-set them to their old values. | ||
|
|
||
| # The bash script is in the same directory as this modulefile, and is named .<module_version>.sh | ||
| set bash_script "$spack_module_dir/.$module_version.sh" | ||
|
|
||
| # Run the bash script and capture its output into a variable | ||
| set bash_commands [exec /bin/bash $bash_script \ | ||
| $old_env_var_name \ | ||
| $setup_env \ | ||
| $spack_python \ | ||
| $spack_root] | ||
|
|
||
| # Split the variable with null character (\x00) to get the commands to set the new environment | ||
| # and those to reinstate the old environment | ||
| lassign [split $bash_commands "\x00"] new_env_commands old_env_commands | ||
|
|
||
| # Set the environment variable to reinstate the old environment | ||
| puts "export $old_env_var_name='$old_env_commands'" | ||
|
|
||
| # Set the new environment | ||
| puts $new_env_commands | ||
|
|
||
| puts stderr "Spack environment set up using $setup_env" | ||
| } else { | ||
| # If the module is unloaded, we use the old_env_var_name env variable to reinstate the old | ||
| # environment | ||
| if {[info exists env($old_env_var_name)]} { | ||
| puts $env($old_env_var_name) | ||
| # Unset the old_env_var_name | ||
| puts "unset $old_env_var_name" | ||
| } else { | ||
| # If the old_env_var_name env variable doesn't exist, print out a warning message | ||
| puts stderr [string cat \ | ||
| "WARNING: The $old_env_var_name env variable which stores the environment " \ | ||
| "information prior to loading the $module_name module is not set. Environment restoration failed." | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| # Set spack user cache path | ||
| set spack_admin_config_env_var_name ACCESS_SPACK_ADMIN | ||
| if { [info exists env($spack_admin_config_env_var_name)] } { | ||
| setenv SPACK_USER_CACHE_PATH "$base_dir/spack-admin-cache" | ||
| } else { | ||
| setenv SPACK_USER_CACHE_PATH "/g/data/$env(PROJECT)/$env(USER)/spack/$module_version/spack-user-cache" | ||
| if {[module-info mode load]} { | ||
| puts stderr "Spack user cache set to $env(SPACK_USER_CACHE_PATH)" | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.