diff options
author | Eike Ziller <eike.ziller@qt.io> | 2018-09-06 15:34:40 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2018-09-10 11:09:31 +0000 |
commit | a865fa513b6f5f5cf1556da524d0bf3552a9286f (patch) | |
tree | 8aa486aaba30f9be7a78fd645c1d09d2bc20367f /share | |
parent | 972cd5514f86305f5cf3e00a16ad506e2dde277f (diff) | |
download | qt-creator-a865fa513b6f5f5cf1556da524d0bf3552a9286f.tar.gz |
macOS: Fix passing environment to terminal
We do not actually start a new Terminal process, so we need to clean up
the environment after the fact.
Clean the environment in the started shell except for some essentials,
read the config files as if for a login shell, and re-export the
environment.
There still can be differences, since environment variables set in the
user's bash profile etc will "win". This is wrong if we want to open the
terminal in anything but the "system environment", especially if the
PATH is effected, but I don't see how to solve that without severely
crippling the shell setup. This is also the current state on Linux.
Change-Id: I1d3c8184ac3bf543675e96f73253085fa6b1b29d
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
Diffstat (limited to 'share')
-rwxr-xr-x | share/qtcreator/scripts/openTerminal.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/share/qtcreator/scripts/openTerminal.py b/share/qtcreator/scripts/openTerminal.py index 3ee0dc652c..afcb94d632 100755 --- a/share/qtcreator/scripts/openTerminal.py +++ b/share/qtcreator/scripts/openTerminal.py @@ -37,6 +37,41 @@ def quote_applescript(arg): def quote_shell(arg): return pipes.quote(arg) +def clean_environment_script(): + # keep some basic environment settings to ensure functioning terminal and config files + env_to_keep = ' '.join(['_', 'HOME', 'LOGNAME', 'PWD', 'SHELL', 'TMPDIR', 'USER', 'TERM', + 'TERM_PROGRAM', 'TERM_PROGRAM_VERSION', 'TERM_SESSION_CLASS_ID', + 'TERM_SESSION_ID']) + return ''' +function ignore() { + local keys="''' + env_to_keep + '''" + local v=$1 + for e in $keys; do [[ "$e" == "$v" ]] && return 0; done +} +while read -r line; do + key=$(echo $line | /usr/bin/cut -d '=' -f 1) + ignore $key || unset $key +done < <(env) +''' + +def system_login_script(): + return 'if [ -f /etc/profile ]; then source /etc/profile; fi\n' + +def login_script(): + return ''' +if [ -f $HOME/.bash_profile ]; then + source $HOME/.bash_profile +elif [ -f $HOME/.bash_login ]; then + source $HOME/.bash_login ] +elif [ -f $HOME/.profile ]; then + source $HOME/.profile +fi +''' + +def environment_script(): + return ''.join(['export ' + quote_shell(key + '=' + os.environ[key]) + '\n' + for key in os.environ]) + def apple_script(shell_command): return ''' --Terminal opens a window by default when it is not running, so check @@ -64,7 +99,11 @@ def main(): # create temporary file to be sourced into bash that deletes itself with NamedTemporaryFile(delete=False) as shell_script: quoted_shell_script = quote_shell(shell_script.name) - commands = ('cd ' + quote_shell(os.getcwd()) + '\n' + + commands = (clean_environment_script() + + system_login_script() + # /etc/profile by default resets the path, so do first + environment_script() + + login_script() + + 'cd ' + quote_shell(os.getcwd()) + '\n' + ' '.join([quote_shell(arg) for arg in sys.argv[1:]]) + '\n' + 'rm ' + quoted_shell_script + '\n' ) |