0

I am trying to create a module file for an application I have installed on the cluster. To run the program, I have to run a script named "phenix_env.sh" which contains the following:

#!/bin/sh
# 
export PHENIX="/src/phenix-1.19.2-4158"
export PHENIX_VERSION=1.19.2-4158
. $PHENIX/build/setpaths.sh

While the "setpaths.sh" contains the following

# THIS IS AN AUTOMATICALLY GENERATED FILE.
# DO NOT EDIT! CHANGES WILL BE LOST.
ocwd="`pwd`"
if [ -n "$LIBTBX_BUILD_RELOCATION_HINT" ]; then
  cd "$LIBTBX_BUILD_RELOCATION_HINT"
  LIBTBX_BUILD_RELOCATION_HINT=
  export LIBTBX_BUILD_RELOCATION_HINT
elif [ -n "$BASH_SOURCE" ]; then
  LIBTBX_BUILD=`dirname "$BASH_SOURCE[0]"`
  cd "$LIBTBX_BUILD"
else
  cd "/util/opt/phenix/1.19.2/gcc/9.1/phenix-1.19.2-4158/build"
fi
LIBTBX_BUILD=`pwd -P`
export LIBTBX_BUILD
LIBTBX_OPATH="$PATH"
export LIBTBX_OPATH
PATH="$LIBTBX_BUILD/bin:$PATH"
export PATH
# DIALS_ENABLE_COMMAND_LINE_COMPLETION
[ -n "$BASH_VERSION" ] && {
 source $(libtbx.find_in_repositories dials/util/autocomplete.sh) && source $LIBTBX_BUILD/dials/autocomplete/bash.sh || echo dials command line completion not available
}
cd "$ocwd"
ocwd=
alias libtbx.setpaths_all=". \"$LIBTBX_BUILD/setpaths_all.sh\""
alias libtbx.unsetpaths=". \"$LIBTBX_BUILD/unsetpaths.sh\""
if [ -n "$LIBTBX_OPATH" ]; then
  LIBTBX_TMPVAL="$LIBTBX_OPATH"
else
  LIBTBX_TMPVAL=
fi
export LIBTBX_TMPVAL
PATH=`libtbx.path_utility prepend LIBTBX_TMPVAL "$LIBTBX_BUILD/bin" < /dev/null`
export PATH
if [ "$PATH" = "L_I_B_T_B_X_E_M_P_T_Y" ]; then unset PATH; fi
LIBTBX_TMPVAL=
LIBTBX_OPATH=
LIBTBX_BUILD=

Any idea how to create a module file for it?

coatless
  • 20,011
  • 13
  • 69
  • 84
Tanash
  • 461
  • 1
  • 5
  • 8
  • what is this module supposed to do? run that script? please provide more context – Piglet May 06 '21 at 04:53
  • In the HPC cluster, we provide a module files to the user, which means include all of the application paths, so users can load the module and use the application. so the user can use the command "module load " which will add all executable paths to user path so user can run the application. – Tanash May 06 '21 at 04:59
  • so you want a Lua module that executes that shell script when loaded? – Piglet May 06 '21 at 05:23
  • Exactly! The Lua module that execute phenix_env.sh and "setpaths.sh" since "phenix_env.sh" calls " setpaths.sh" – Tanash May 06 '21 at 05:46
  • https://www.lua.org/manual/5.4/manual.html#pdf-os.execute – Piglet May 06 '21 at 05:53

2 Answers2

1

You can not.
Lua executes shell scripts in a subshell.
This means all cd and export commands will affect only that subshell but not the parent shell where the Lua script runs.
In other words, your changes will be nullified after returning from os.execute

You can workaround it if you are allowed to require() a Lua modules written on C.
You need a module which provides wrappers around OS-native functions for changing the current directory and setting env variable.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
0

With recent version of Environment Modules (>=4.6), the sh-to-mod sub-command enables you to generate a modulefile containing the resulting environment changes made by a script passed as argument

$ module sh-to-mod sh ./phenix_env.sh 
#%Module
prepend-path    PATH /home/user/bin
set-alias       libtbx.setpaths_all {. "/home/xa/setpaths_all.sh"}
set-alias       libtbx.unsetpaths {. "/home/xa/unsetpaths.sh"}
setenv          LIBTBX_BUILD {}
setenv          LIBTBX_OPATH {}
setenv          LIBTBX_TMPVAL {}
setenv          PHENIX /src/phenix-1.19.2-4158
setenv          PHENIX_VERSION 1.19.2-4158

Note that result obtained here is not totally accurate as I have just copied the script you described but I did not have the phenix software installed which is called to get the environment fully setup.

See also this answer : How to automatically generate the modulefile for Intel compilers