2018-09-05 17:43:22 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
############################################################################
|
|
|
|
|
#
|
|
|
|
|
# Copyright (C) 2018 The Qt Company Ltd.
|
|
|
|
|
# Contact: https://www.qt.io/licensing/
|
|
|
|
|
#
|
|
|
|
|
# This file is part of Qt Creator.
|
|
|
|
|
#
|
|
|
|
|
# Commercial License Usage
|
|
|
|
|
# Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
# accordance with the commercial license agreement provided with the
|
|
|
|
|
# Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
# a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
# and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
# information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
#
|
|
|
|
|
# GNU General Public License Usage
|
|
|
|
|
# Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
# General Public License version 3 as published by the Free Software
|
|
|
|
|
# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
# included in the packaging of this file. Please review the following
|
|
|
|
|
# information to ensure the GNU General Public License requirements will
|
|
|
|
|
# be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
#
|
|
|
|
|
############################################################################
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import pipes
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
2018-09-06 12:42:49 +02:00
|
|
|
from tempfile import NamedTemporaryFile
|
2018-09-05 17:43:22 +02:00
|
|
|
|
2018-09-06 12:42:49 +02:00
|
|
|
def quote_applescript(arg):
|
2018-09-05 17:43:22 +02:00
|
|
|
return arg.replace('\\', '\\\\').replace('"', '\\"')
|
|
|
|
|
|
2018-09-06 12:42:49 +02:00
|
|
|
def quote_shell(arg):
|
|
|
|
|
return pipes.quote(arg)
|
2018-09-05 17:43:22 +02:00
|
|
|
|
2018-09-06 15:34:40 +02:00
|
|
|
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)
|
|
|
|
|
'''
|
|
|
|
|
|
2019-11-08 12:27:40 +01:00
|
|
|
def system_login_script_bash():
|
2018-09-06 15:34:40 +02:00
|
|
|
return 'if [ -f /etc/profile ]; then source /etc/profile; fi\n'
|
|
|
|
|
|
2019-11-08 12:27:40 +01:00
|
|
|
def login_script_bash():
|
2018-09-06 15:34:40 +02:00
|
|
|
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
|
|
|
|
|
'''
|
|
|
|
|
|
2019-11-08 12:27:40 +01:00
|
|
|
def system_login_script_zsh():
|
|
|
|
|
return '[ -r /etc/profile ] && source /etc/profile\n'
|
|
|
|
|
|
|
|
|
|
def login_script_zsh():
|
|
|
|
|
return '''
|
|
|
|
|
[ -r $HOME/.zprofile ] && source $HOME/.zprofile
|
|
|
|
|
[ -r $HOME/.zshrc ] && source $HOME/.zshrc
|
|
|
|
|
[ -r $HOME/.zlogin ] && source $HOME/.zlogin
|
|
|
|
|
'''
|
|
|
|
|
|
2018-09-06 15:34:40 +02:00
|
|
|
def environment_script():
|
|
|
|
|
return ''.join(['export ' + quote_shell(key + '=' + os.environ[key]) + '\n'
|
|
|
|
|
for key in os.environ])
|
|
|
|
|
|
2018-09-06 12:42:49 +02:00
|
|
|
def apple_script(shell_command):
|
2018-09-05 17:43:22 +02:00
|
|
|
return '''
|
|
|
|
|
--Terminal opens a window by default when it is not running, so check
|
|
|
|
|
on applicationIsRunning(applicationName)
|
|
|
|
|
tell application "System Events" to count (every process whose name is applicationName)
|
|
|
|
|
return result is greater than 0
|
|
|
|
|
end applicationIsRunning
|
|
|
|
|
set terminalWasRunning to applicationIsRunning("Terminal")
|
|
|
|
|
|
|
|
|
|
set cdScript to "{}"
|
|
|
|
|
tell application "Terminal"
|
|
|
|
|
--do script will open a new window if none given, but terminal already opens one if not running
|
|
|
|
|
if terminalWasRunning then
|
|
|
|
|
do script cdScript
|
|
|
|
|
else
|
|
|
|
|
do script cdScript in first window
|
|
|
|
|
end if
|
|
|
|
|
set currentTab to the result
|
|
|
|
|
set currentWindow to first window whose tabs contains currentTab
|
|
|
|
|
activate
|
|
|
|
|
end tell
|
2018-09-06 12:42:49 +02:00
|
|
|
'''.format(shell_command)
|
2018-09-05 17:43:22 +02:00
|
|
|
|
|
|
|
|
def main():
|
2018-09-06 12:42:49 +02:00
|
|
|
# create temporary file to be sourced into bash that deletes itself
|
|
|
|
|
with NamedTemporaryFile(delete=False) as shell_script:
|
2019-11-08 12:27:40 +01:00
|
|
|
shell = os.environ.get('SHELL')
|
|
|
|
|
shell_is_zsh = shell is not None and shell.endswith('/zsh')
|
|
|
|
|
system_login_script = system_login_script_zsh if shell_is_zsh else system_login_script_bash
|
|
|
|
|
login_script = login_script_zsh if shell_is_zsh else login_script_bash
|
2018-09-06 12:42:49 +02:00
|
|
|
quoted_shell_script = quote_shell(shell_script.name)
|
2018-09-06 15:34:40 +02:00
|
|
|
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' +
|
2018-09-06 12:42:49 +02:00
|
|
|
' '.join([quote_shell(arg) for arg in sys.argv[1:]]) + '\n' +
|
2019-08-26 18:00:52 +02:00
|
|
|
'rm ' + quoted_shell_script + '\n' +
|
2019-11-07 14:01:49 +01:00
|
|
|
('exit\n' if len(sys.argv) > 1 else '')
|
2018-09-06 12:42:49 +02:00
|
|
|
)
|
|
|
|
|
shell_script.write(commands)
|
|
|
|
|
shell_script.flush()
|
|
|
|
|
shell_command = quote_applescript('source ' + quoted_shell_script)
|
|
|
|
|
osascript_process = subprocess.Popen(['/usr/bin/osascript'], stdin=subprocess.PIPE)
|
|
|
|
|
osascript_process.communicate(apple_script(shell_command))
|
2018-09-05 17:43:22 +02:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|