Closed boegel closed 1 month ago
@Micket Any ideas here?
hm, well, it would risk making it not identical environment as to what run_shell_cmd runs as?
hm, well, it would risk making it not identical environment as to what run_shell_cmd runs as?
Hmm right, good point.
It is a bit annoying that the module
command is not defined though.
I guess we should at least document this, and perhaps print a suggestion on this (I guess sourcing /etc/profile.d/modules.sh
should be pretty universal?)...
I really should have just figured out how to solve that from the start, but i guess we just deal with the %% variables, which should be all the functions for bash;
# excludes bash functions (environment variables ending with %)
fid.write('\n'.join(f'export {key}={shlex.quote(value)}' for key, value in sorted(env.items())
if not key.endswith('%')) + '\n')
These variables "BASH_FUNC_module%%" can't be exported, and i don't think just including them as they are would produce working functions, so instead
for key, value in env.items():
if key.startswith('BASH_FUNC_') and key.endswith('%%'):
func_name = key[10:-2]
fid.write(f'declare -f {func_name} {value}\n')
which should just define the function again
or maybe something a bit hacky like
run_subprocess('declare -f >> env.sh', shell=True) # to lazy to figure out the code here, but the principle is clear
Note: completely untested.
Having tested some things here, i think we should consider just letting bash do the writing of the files. While i haven't found a way to break bash functions, they can be long and complicated and the associated environment variables aren't a 1-to-1 match with the source code. There may also be some complex escaping necessary for special characters.
Maybe we just replace the code that exports the environment variables with just subprocesses that run
declare -x > env.sh # or keep the current python code
declare -f > func.sh
This lets bash parse them, and it will do the correct job of it always.
@Micket That approach makes sense to me. We now also call a subprocess btw, since the proceduce to collect the contents for env.sh
uses the env
command (which is provided by coreutils
).
With that in mind, declare -x
is better, since declare
is a bash builtin, no so hidden dependency?
fixed with #4662
It would be better if
cmd.sh
starts a login shell, to avoid this issue:It's not as trivial as combining
--login
with--rcfile
though, it seems...