Skip to content

Commit

Permalink
Use more robust mechanism for unsetting environment variables
Browse files Browse the repository at this point in the history
Parsing the output of "env" can be fragile for certain complex
environment.
Using compgen should be safer.
  • Loading branch information
Micket committed Oct 16, 2024
1 parent 8fc631d commit 4b14a6e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions easybuild/tools/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ def create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file):
with open(env_fp, 'w') as fid:
# unset all environment variables in current environment first to start from a clean slate;
# we need to be careful to filter out functions definitions, so first undefine those
fid.write("unset -f $(env | grep '%=' | cut -f1 -d'%' | sed 's/BASH_FUNC_//g')\n")
fid.write("unset $(env | cut -f1 -d=)\n")
fid.write("for var in $(compgen -e); do\n unset \"$var\"\ndone\n")
# also unset any bash functions
fid.write("for func in $(compgen -A function); do\n if [[ $func != _* ]]; then\n"
" unset -f \"$func\"\n fi\ndone\n")

# excludes bash functions (environment variables ending with %)
fid.write('\n'.join(f'export {key}={shlex.quote(value)}' for key, value in sorted(env.items())
Expand Down

0 comments on commit 4b14a6e

Please sign in to comment.