summaryrefslogtreecommitdiff
path: root/share/qtcreator/scripts/openTerminal.py
blob: 420e936d78707b49d3b5ca6d3bcb08611e740304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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
from tempfile import NamedTemporaryFile

def quote_applescript(arg):
    return arg.replace('\\', '\\\\').replace('"', '\\"')

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_bash():
    return 'if [ -f /etc/profile ]; then source /etc/profile; fi\n'

def login_script_bash():
    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 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
'''

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
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
'''.format(shell_command)

def main():
    # create temporary file to be sourced into bash that deletes itself
    with NamedTemporaryFile(delete=False) as shell_script:
        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
        quoted_shell_script = quote_shell(shell_script.name)
        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' +
                    ('exit\n' if len(sys.argv) > 1 else '')
                    )
        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))

if __name__ == "__main__":
    main()