Skip to content
Snippets Groups Projects

Improve how the environment is sourced from modules.sh

Merged Benedikt Steinbusch requested to merge bs-env-filter into master
1 file
+ 8
11
Compare changes
  • Side-by-side
  • Inline
+ 8
11
@@ -18,23 +18,21 @@ def _splitsep(arg, sep):
@@ -18,23 +18,21 @@ def _splitsep(arg, sep):
re.split(f"({re.escape(sep)})", arg), [])
re.split(f"({re.escape(sep)})", arg), [])
def _update_env_with_system_modules(user_env, system_module_path):
def _create_modules_env(system_module_path):
try:
try:
out, err = create_process_and_wait_(
out, err = create_process_and_wait_(
["/bin/bash", "-c", f"source {system_module_path} ; env"])
["/bin/bash", "-c", f"source {system_module_path} ; env -0"])
# because env is hiding a error in source:
# because env is hiding a error in source:
if 'No such file or directory' in str(err):
if 'No such file or directory' in str(err):
raise IOError
raise IOError
except IOError:
except IOError:
return False
return False
system_env = map(lambda z: z.split("=", 1), str(out).split("\n"))
return {
for key, value in [x for x in system_env if len(x) == 2]:
x[0]: x[1] for x in
if re.match(r'.*MODULE.*', key) \
(y.split("=", 1) for y in str(out).split("\0"))
or re.match(r'.*LMOD.*', key) \
if len(x) == 2
or re.match(r'EB.*', key):
}
user_env[key] = value
return user_env
def _parse_cmdline(argv):
def _parse_cmdline(argv):
@@ -138,8 +136,7 @@ def main():
@@ -138,8 +136,7 @@ def main():
return 1 # E.g., -h
return 1 # E.g., -h
system_module_path = _parse_config()
system_module_path = _parse_config()
user_env = _update_env_with_system_modules(os.environ.copy(),
user_env = _create_modules_env(system_module_path)
system_module_path)
if not system_module_path or not user_env:
if not system_module_path or not user_env:
sys.stderr.write(
sys.stderr.write(
'Error with system configuration of xenv, contact admins.\n')
'Error with system configuration of xenv, contact admins.\n')
Loading