Skip to content

Commit

Permalink
Issue #750: Modified convertSh2MF to convert export -f func to export…
Browse files Browse the repository at this point in the history
…_shell_function("func")
  • Loading branch information
Robert McLay committed Feb 21, 2025
1 parent b8073fb commit 818af38
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 56 deletions.
3 changes: 2 additions & 1 deletion README.new
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ Lmod 8.7+
* Issue #728: Disallow fast TCL interp with TCL 9+. This will hopely fixed in the future (;->)
W.I.P:
(8.7.57) * Issue #750: Adding support for new function export_shell_function()
* Pr #752: Ensure m_Module_Msgs_close has an ending newline
Modified convertSh2MF.lua support converting export -f func to export_shell_function("func")
* PR #752: Ensure m_Module_Msgs_close has an ending newline
* Issue #743: "module show noSuchModule" return an LmodError instead of
LmodWarning except when $LMOD_QUIET is set
* Issue #749: set display mode when executing "module update"
32 changes: 18 additions & 14 deletions rt/sh_to_mf/err.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
step 1
lua ProjectDIR/src/lmod.in.lua shell --regression_testing --version
===========================
Modules based on Lua: Version 8.7.49 2024-08-30 13:55 -06:00
Modules based on Lua: Version 8.7.56 2025-02-04 18:12 -07:00
by Robert McLay mclay@tacc.utexas.edu
===========================
step 2
Expand Down Expand Up @@ -39,6 +39,10 @@ If this is a bash script please try:
See https://lmod.readthedocs.io/en/latest/260_sh_to_modulefile.html for details.
===========================
step 8
buildSh2MF ProjectDIR/rt/sh_to_mf/spack_foo.sh
===========================
===========================
step 9
lua ProjectDIR/src/lmod.in.lua shell --regression_testing show wrapperSh/1.0
===========================
ProjectDIR/rt/sh_to_mf/mf/wrapperSh/1.0.lua:
Expand Down Expand Up @@ -76,11 +80,11 @@ setenv("MY_NAME","tstScript.sh")
source_sh("bash","ProjectDIR/rt/sh_to_mf/second.sh arg1")
setenv("SECOND","FOO_BAR")
===========================
step 9
step 10
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load wrapperSh/1.0
===========================
===========================
step 10
step 11
lua ProjectDIR/src/lmod.in.lua shell --regression_testing show wrapperSh/1.0
===========================
ProjectDIR/rt/sh_to_mf/mf/wrapperSh/1.0.lua:
Expand Down Expand Up @@ -118,17 +122,17 @@ setenv("MY_NAME","tstScript.sh")
source_sh("bash","ProjectDIR/rt/sh_to_mf/second.sh arg1")
setenv("SECOND","FOO_BAR")
===========================
step 11
step 12
lua ProjectDIR/src/lmod.in.lua shell --regression_testing refresh
===========================
===========================
step 12
step 13
lua ProjectDIR/src/lmod.in.lua shell --regression_testing list
===========================
Currently Loaded Modules:
1) wrapperSh/1.0
===========================
step 13
step 14
lua ProjectDIR/src/lmod.in.lua shell --regression_testing --mt
===========================
_ModuleTable_ = {
Expand Down Expand Up @@ -182,11 +186,11 @@ _ModuleTable_ = {
systemBaseMPATH = "ProjectDIR/rt/sh_to_mf/mf",
}
===========================
step 14
step 15
lua ProjectDIR/src/lmod.in.lua shell --regression_testing unload wrapperSh/1.0
===========================
===========================
step 15
step 16
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load brokenSh/1.0
===========================
Lmod has detected the following error: Error found in sourcing script "ProjectDIR/rt/sh_to_mf/broken.sh":
Expand All @@ -198,21 +202,21 @@ While processing the following module(s):
Module fullname Module Filename
brokenSh/1.0 ProjectDIR/rt/sh_to_mf/mf/brokenSh/1.0.lua
===========================
step 16
step 17
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load wrapperSh/1.0
===========================
===========================
step 17
step 18
lua ProjectDIR/src/lmod.in.lua shell --regression_testing save
===========================
Saved current collection of modules to: "default"
===========================
step 18
step 19
lua ProjectDIR/src/lmod.in.lua shell --regression_testing restore
===========================
Restoring modules from user's default
===========================
step 19
step 20
lua ProjectDIR/src/lmod.in.lua shell --regression_testing --mt
===========================
_ModuleTable_ = {
Expand Down Expand Up @@ -266,11 +270,11 @@ _ModuleTable_ = {
systemBaseMPATH = "ProjectDIR/rt/sh_to_mf/mf",
}
===========================
step 20
step 21
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load A
===========================
===========================
step 21
step 22
lua ProjectDIR/src/lmod.in.lua shell --regression_testing list
===========================
Currently Loaded Modules:
Expand Down
34 changes: 21 additions & 13 deletions rt/sh_to_mf/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ buildSh2MF ProjectDIR/rt/sh_to_mf/unalias.sh
===========================
===========================
step 8
buildSh2MF ProjectDIR/rt/sh_to_mf/spack_foo.sh
===========================
set_shell_function("_some_spack_func"," \
echo \"_some_spack_func\"\
","")
export_shell_function("_some_spack_func")
===========================
step 9
lua ProjectDIR/src/lmod.in.lua shell --regression_testing show wrapperSh/1.0
===========================
__LMOD_REF_COUNT_MODULEPATH=ProjectDIR/rt/sh_to_mf/mf:1;
Expand All @@ -114,7 +122,7 @@ export MODULEPATH;
_ModuleTable_='_ModuleTable_={MTversion=3,depthT={},family={},mT={},mpathA={"ProjectDIR/rt/sh_to_mf/mf",},systemBaseMPATH="ProjectDIR/rt/sh_to_mf/mf",}';
export _ModuleTable_;
===========================
step 9
step 10
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load wrapperSh/1.0
===========================
LOADEDMODULES=wrapperSh/1.0;
Expand Down Expand Up @@ -160,15 +168,15 @@ save_args () {
echo " ";
};
===========================
step 10
step 11
lua ProjectDIR/src/lmod.in.lua shell --regression_testing show wrapperSh/1.0
===========================
MODULEPATH=ProjectDIR/rt/sh_to_mf/mf;
export MODULEPATH;
_ModuleTable_='_ModuleTable_={MTversion=3,depthT={},family={},mT={wrapperSh={fn="ProjectDIR/rt/sh_to_mf/mf/wrapperSh/1.0.lua",fullName="wrapperSh/1.0",loadOrder=1,mcmdT={["ProjectDIR/rt/sh_to_mf/second.sh arg1"]={"setenv(\"SECOND\",\"FOO_BAR\")",},["ProjectDIR/rt/sh_to_mf/tstScript.sh"]={"setenv(\"MY_NAME\",\"tstScript.sh\")","setenv(\"PTH\",\"ProjectDIR/rt/sh_to_mf/bin:ProjectDIR/proj_mgmt:PATH_to_TM:ProjectDIR/rt/sh_to_mf/sbin\")","setenv(\"TST_SCRIPT\",\"1\")","set_alias(\"fooAlias\",\"foobin -q -l\")",[[set_shell_function("banner"," \\ local str=\\"$1\\";\\ local RED='\\033[1;31m';\\ local NONE='\\033[0m';\\ echo \\"${RED}${str}${NONE}\\"\\","")]],[[set_shell_function("fooFunc"," \\ echo \\"arg1: $1\\"\\","")]],[[set_shell_function("junk"," \\ if [ -n \\"${ZSH_VERSION:-}\\" ]; then\\ \\\\echo \\"junk\\";\\ fi\\","")]],[[set_shell_function("my_help"," \\ echo \\"do not forget \\\\\\"foo\\\\\\"\\"\\","")]],[[set_shell_function("save_args"," \\ for arg in \\"$@\\";\\ do\\ printf \\"%s\\\\n\\" \\"$arg\\" | sed -e \\"s/'/'\\\\\\\\\\\\\\\\''/g\\" -e \\"1s/^/'/\\" -e \\"\\\\$s/\\\\$/' \\\\\\\\\\\\\\\\/\\";\\ done;\\ echo \\" \\"\\","")]],"complete(\"bash\",\"XyZ123\",\"-F _xyz123\")",},},propT={},stackDepth=0,status="active",userName="wrapperSh/1.0",wV="000000001.*zfinal",},},mpathA={"ProjectDIR/rt/sh_to_mf/mf",},systemBaseMPATH="ProjectDIR/rt/sh_to_mf/mf",}';
export _ModuleTable_;
===========================
step 11
step 12
lua ProjectDIR/src/lmod.in.lua shell --regression_testing refresh
===========================
MODULEPATH=ProjectDIR/rt/sh_to_mf/mf;
Expand Down Expand Up @@ -202,19 +210,19 @@ save_args () {
echo " ";
};
===========================
step 12
step 13
lua ProjectDIR/src/lmod.in.lua shell --regression_testing list
===========================
MODULEPATH=ProjectDIR/rt/sh_to_mf/mf;
export MODULEPATH;
_ModuleTable_='_ModuleTable_={MTversion=3,depthT={},family={},mT={wrapperSh={fn="ProjectDIR/rt/sh_to_mf/mf/wrapperSh/1.0.lua",fullName="wrapperSh/1.0",loadOrder=1,mcmdT={["ProjectDIR/rt/sh_to_mf/second.sh arg1"]={"setenv(\"SECOND\",\"FOO_BAR\")",},["ProjectDIR/rt/sh_to_mf/tstScript.sh"]={"setenv(\"MY_NAME\",\"tstScript.sh\")","setenv(\"PTH\",\"ProjectDIR/rt/sh_to_mf/bin:ProjectDIR/proj_mgmt:PATH_to_TM:ProjectDIR/rt/sh_to_mf/sbin\")","setenv(\"TST_SCRIPT\",\"1\")","set_alias(\"fooAlias\",\"foobin -q -l\")",[[set_shell_function("banner"," \\ local str=\\"$1\\";\\ local RED='\\033[1;31m';\\ local NONE='\\033[0m';\\ echo \\"${RED}${str}${NONE}\\"\\","")]],[[set_shell_function("fooFunc"," \\ echo \\"arg1: $1\\"\\","")]],[[set_shell_function("junk"," \\ if [ -n \\"${ZSH_VERSION:-}\\" ]; then\\ \\\\echo \\"junk\\";\\ fi\\","")]],[[set_shell_function("my_help"," \\ echo \\"do not forget \\\\\\"foo\\\\\\"\\"\\","")]],[[set_shell_function("save_args"," \\ for arg in \\"$@\\";\\ do\\ printf \\"%s\\\\n\\" \\"$arg\\" | sed -e \\"s/'/'\\\\\\\\\\\\\\\\''/g\\" -e \\"1s/^/'/\\" -e \\"\\\\$s/\\\\$/' \\\\\\\\\\\\\\\\/\\";\\ done;\\ echo \\" \\"\\","")]],"complete(\"bash\",\"XyZ123\",\"-F _xyz123\")",},},propT={},stackDepth=0,status="active",userName="wrapperSh/1.0",wV="000000001.*zfinal",},},mpathA={"ProjectDIR/rt/sh_to_mf/mf",},systemBaseMPATH="ProjectDIR/rt/sh_to_mf/mf",}';
export _ModuleTable_;
===========================
step 13
step 14
lua ProjectDIR/src/lmod.in.lua shell --regression_testing --mt
===========================
===========================
step 14
step 15
lua ProjectDIR/src/lmod.in.lua shell --regression_testing unload wrapperSh/1.0
===========================
unset LOADEDMODULES;
Expand All @@ -235,12 +243,12 @@ unset -f junk 2> /dev/null || true;
unset -f my_help 2> /dev/null || true;
unset -f save_args 2> /dev/null || true;
===========================
step 15
step 16
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load brokenSh/1.0
===========================
false
===========================
step 16
step 17
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load wrapperSh/1.0
===========================
LOADEDMODULES=wrapperSh/1.0;
Expand Down Expand Up @@ -286,15 +294,15 @@ save_args () {
echo " ";
};
===========================
step 17
step 18
lua ProjectDIR/src/lmod.in.lua shell --regression_testing save
===========================
MODULEPATH=ProjectDIR/rt/sh_to_mf/mf;
export MODULEPATH;
_ModuleTable_='_ModuleTable_={MTversion=3,depthT={},family={},mT={wrapperSh={fn="ProjectDIR/rt/sh_to_mf/mf/wrapperSh/1.0.lua",fullName="wrapperSh/1.0",loadOrder=1,mcmdT={["ProjectDIR/rt/sh_to_mf/second.sh arg1"]={"setenv(\"SECOND\",\"FOO_BAR\")",},["ProjectDIR/rt/sh_to_mf/tstScript.sh"]={"setenv(\"MY_NAME\",\"tstScript.sh\")","setenv(\"PTH\",\"ProjectDIR/rt/sh_to_mf/bin:ProjectDIR/proj_mgmt:PATH_to_TM:ProjectDIR/rt/sh_to_mf/sbin\")","setenv(\"TST_SCRIPT\",\"1\")","set_alias(\"fooAlias\",\"foobin -q -l\")",[[set_shell_function("banner"," \\ local str=\\"$1\\";\\ local RED='\\033[1;31m';\\ local NONE='\\033[0m';\\ echo \\"${RED}${str}${NONE}\\"\\","")]],[[set_shell_function("fooFunc"," \\ echo \\"arg1: $1\\"\\","")]],[[set_shell_function("junk"," \\ if [ -n \\"${ZSH_VERSION:-}\\" ]; then\\ \\\\echo \\"junk\\";\\ fi\\","")]],[[set_shell_function("my_help"," \\ echo \\"do not forget \\\\\\"foo\\\\\\"\\"\\","")]],[[set_shell_function("save_args"," \\ for arg in \\"$@\\";\\ do\\ printf \\"%s\\\\n\\" \\"$arg\\" | sed -e \\"s/'/'\\\\\\\\\\\\\\\\''/g\\" -e \\"1s/^/'/\\" -e \\"\\\\$s/\\\\$/' \\\\\\\\\\\\\\\\/\\";\\ done;\\ echo \\" \\"\\","")]],"complete(\"bash\",\"XyZ123\",\"-F _xyz123\")",},},propT={},stackDepth=0,status="active",userName="wrapperSh/1.0",wV="000000001.*zfinal",},},mpathA={"ProjectDIR/rt/sh_to_mf/mf",},systemBaseMPATH="ProjectDIR/rt/sh_to_mf/mf",}';
export _ModuleTable_;
===========================
step 18
step 19
lua ProjectDIR/src/lmod.in.lua shell --regression_testing restore
===========================
MODULEPATH=ProjectDIR/rt/sh_to_mf/mf;
Expand Down Expand Up @@ -338,11 +346,11 @@ save_args () {
echo " ";
};
===========================
step 19
step 20
lua ProjectDIR/src/lmod.in.lua shell --regression_testing --mt
===========================
===========================
step 20
step 21
lua ProjectDIR/src/lmod.in.lua shell --regression_testing load A
===========================
LOADEDMODULES=A/1.0;
Expand Down Expand Up @@ -382,7 +390,7 @@ save_args () {
echo " ";
};
===========================
step 21
step 22
lua ProjectDIR/src/lmod.in.lua shell --regression_testing list
===========================
MODULEPATH=OutputDIR/mf;
Expand Down
25 changes: 13 additions & 12 deletions rt/sh_to_mf/sh_to_mf.tdesc
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ testdescript = {
runSh2MF --from csh $(testDir)/cshScript.csh # 5
runSh2MF $(testDir)/broken.sh # 6
runSh2MF $(testDir)/unalias.sh # 7
runLmod show wrapperSh/1.0 # 8
runLmod load wrapperSh/1.0 # 9
runLmod show wrapperSh/1.0 #10
runLmod refresh #11
runLmod list #12
runLmod --mt #13
runLmod unload wrapperSh/1.0 #14
runLmod load brokenSh/1.0 #15
runLmod load wrapperSh/1.0 #16
runLmod save #18
runLmod restore #19
runLmod --mt #20
runSh2MF $(testDir)/spack_foo.sh # 8
runLmod show wrapperSh/1.0 # 9
runLmod load wrapperSh/1.0 #10
runLmod show wrapperSh/1.0 #11
runLmod refresh #12
runLmod list #13
runLmod --mt #14
runLmod unload wrapperSh/1.0 #15
runLmod load brokenSh/1.0 #16
runLmod load wrapperSh/1.0 #18
runLmod save #19
runLmod restore #20
runLmod --mt #21
unsetMT
Expand Down
7 changes: 7 additions & 0 deletions rt/sh_to_mf/spack_foo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
_some_spack_func () {
echo "_some_spack_func"
}
export -f _some_spack_func


25 changes: 15 additions & 10 deletions src/convertSh2MF.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,28 @@ local function l_convertSh2MF(shellName, style, script)
"%{shellArgs}",
"\"",
"%{flgErr}",
"%{printEnvT} oldEnvT;",
"%{printEnvT} oldEnvT normal;",
"echo %{sep};",
"%{alias};",
"echo %{sep};",
"%{funcs};",
"echo %{sep};",
"%{complete};",
"echo %{sep};",
"%{printEnvT} oldEnvT exportFuncs;",
"echo %{sep};",
"%{source} %{script} %{redirect};",
"echo %{sep};",
"%{printEnvT} envT;",
"%{printEnvT} envT normal;",
"echo %{sep};",
"%{alias};",
"echo %{sep};",
"%{funcs};",
"echo %{sep};",
"%{complete};",
"echo %{sep};",
"%{printEnvT} envT exportFuncs;",
"echo %{sep};",
"\"",
}

Expand Down Expand Up @@ -151,8 +155,8 @@ local function l_convertSh2MF(shellName, style, script)
end
end

local processOrderA = { {"Vars", 1}, {"Aliases", 1}, {"Funcs", 1}, {"Complete", 1}, {"SourceScriptOutput", 0},
{"Vars", 2}, {"Aliases", 2}, {"Funcs", 2}, {"Complete", 2}, }
local processOrderA = { {"Vars", 1}, {"Aliases", 1}, {"Funcs", 1}, {"Complete", 1}, {"ExportFuncs", 1}, {"SourceScriptOutput", 0},
{"Vars", 2}, {"Aliases", 2}, {"Funcs", 2}, {"Complete", 2}, {"ExportFuncs", 2}, }

local blkA = {}
sep = sep:escape()
Expand All @@ -170,21 +174,22 @@ local function l_convertSh2MF(shellName, style, script)

if (not status) then
success = false
local msg = i18n("e_Sh_Error",{script=script,errorMsg=blkA[5]})
local msg = i18n("e_Sh_Error",{script=script,errorMsg=blkA[6]})
return success, msg, {}
end

if (#blkA ~= 9) then
if (#blkA < 9) then
success = false
local msg = i18n("e_Sh_convertSh2MF",{})
return success, msg, {}
end


local resultT = { Vars = {{},{}},
Aliases = {{},{}},
Funcs = {{},{}},
Complete = {{},{}},
local resultT = { Vars = {{},{}},
Aliases = {{},{}},
Funcs = {{},{}},
Complete = {{},{}},
ExportFuncs = {{},{}},
}

for i = 1, #processOrderA do
Expand Down
15 changes: 12 additions & 3 deletions src/printEnvT.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,18 @@ local setenv_posix = posix.setenv

function main()
local envT = getenv_posix()
for k,v in pairs(envT) do
if (k:find("^BASH_FUNC_.*%%")) then
envT[k] = nil
local kind = arg[2] or "normal"
if (kind == "normal") then
for k,v in pairs(envT) do
if (k:find("^BASH_FUNC_.*%%")) then
envT[k] = nil
end
end
elseif (kind == "exportFuncs") then
for k,v in pairs(envT) do
if (not k:find("^BASH_FUNC_.*%%")) then
envT[k] = nil
end
end
end
local name = arg[1] or "envT"
Expand Down
Loading

0 comments on commit 818af38

Please sign in to comment.