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>
This commit is contained in:
Eike Ziller
2018-09-06 15:34:40 +02:00
parent 972cd5514f
commit a865fa513b

View File

@@ -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'
)