[Lmod-users] Hooking lmod methods
A Lua based environment module system that reads TCL modulefiles.
Brought to you by:
rtmclay
|
From: <mal...@ci...> - 2026-02-04 09:44:42
|
Hey, I hope this finds you well,
I'm trying to hook prepend_path & append_path so that if a given environment variable is used, I also append it into another variable.
--------
prepend_path( "LD_LIBRARY_PATH","/opt/apps/ddt/5.0.1/lib") becomes:
prepend_path( "LD_LIBRARY_PATH","/opt/apps/ddt/5.0.1/lib")
prepend_path( "MY_OTHER_VARIABLE","/opt/apps/ddt/5.0.1/lib")
--------
The code doing this hooking like like that:
--------
local function Main(context)
local function HookPathMethods(function_name, source, target)
local original_alter_path = context[function_name]
local alter_path = function(...)
local parameters = {...}
if parameters[1] == source then
parameters[1] = target
original_alter_path(table.unpack(parameters))
end
return original_alter_path(...)
end
context[function_name] = alter_path
end
HookPathMethods("prepend_path", "LD_LIBRARY_PATH", "MY_LD_LIBRARY_PATH")
HookPathMethods("append_path", "LD_LIBRARY_PATH", "MY_LD_LIBRARY_PATH")
end
return { Main = Main }
--------
Now, I currently have this code, which is injected through:
--------
assert(loadfile("/path/to/hook.lua"))().Main(_ENV)
--------
This does not work when purging modules, the effect of the hooked prepend_path path remains. I then tried using the sitePackage.lua, like so:
--------
local function HookPathMethods(function_name, source, target)
local original_function = _ENV[function_name]
return function(...)
local parameters = {...}
if parameters[1] == source then
parameters[1] = target
original_function(table.unpack(parameters))
end
return original_function(...)
end
end
sandbox_registration{
prepend_path = HookPathMethods("prepend_path", "LD_LIBRARY_PATH", "MY_LD_LIBRARY_PATH"),
append_path = HookPathMethods("append_path", "LD_LIBRARY_PATH", "MY_LD_LIBRARY_PATH")
}
--------
This works like a charm (so it seems), but it is easy fora user to usnet the LMOD site package env variable and so its brittle. I think I'm goingto stay with SitePackage, but is there a wait to make the solution above work ? Or an alternative way of doing this whole "shenanigans".
I'm very much looking for advice or more conventional ways of doing that. The fact is that we have a lot of common things between our modules and wish to factorize.
Have a nice day, regards,
--
Etienne Malaboeuf
Ingénieur de recherche HPC, Département Calcul Intensif (DCI)
Centre Informatique National de l'Enseignement Supérieur (CINES)
950 rue de Saint Priest, 34097 Montpellier
tel : (334) 67 14 14 02
web : https://www.cines.fr | https://dci.dci-gitlab.cines.fr/webextranet
|