2
0
pycharm-professional/charm

143 lines
4.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
2020-04-08 20:06:02 +03:00
# Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2019-07-24 19:49:25 +03:00
import os
import socket
import struct
import sys
2020-04-08 20:06:02 +03:00
import traceback
2020-04-08 20:06:02 +03:00
# See com.intellij.idea.SocketLock for the server side of this interface.
RUN_PATH = u'/opt/pycharm-professional/bin/pycharm.sh'
2022-04-13 21:51:56 +03:00
CONFIG_PATH = u'~/.config/JetBrains/PyCharm2022.1'
SYSTEM_PATH = u'~/.cache/JetBrains/PyCharm2022.1'
2016-11-23 18:49:18 +02:00
2017-06-16 03:18:01 +03:00
2016-11-23 18:49:18 +02:00
def print_usage(cmd):
print(('Usage:\n' +
' {0} -h | -? | --help\n' +
2019-07-24 19:49:25 +03:00
' {0} [project_dir] [-w|--wait]\n' +
' {0} [-l|--line line] [project_dir|--temp-project] [-w|--wait] file[:line]\n' +
2016-11-23 18:49:18 +02:00
' {0} diff <left> <right>\n' +
' {0} merge <local> <remote> [base] <merged>').format(cmd))
2020-04-08 20:06:02 +03:00
def write_to_sock(sock, data):
if sys.version_info[0] >= 3:
data = data.encode('utf-8')
sock.send(struct.pack('>h', len(data)) + data)
2019-07-24 19:49:25 +03:00
def read_from_sock(sock):
2020-04-08 20:06:02 +03:00
length = struct.unpack('>h', sock.recv(2))[0]
return sock.recv(length).decode('utf-8')
2019-07-24 19:49:25 +03:00
def read_sequence_from_sock(sock):
result = []
while True:
try:
2020-04-08 20:06:02 +03:00
data = read_from_sock(sock)
if data == '---':
2019-07-24 19:49:25 +03:00
break
2020-04-08 20:06:02 +03:00
result.append(data)
2019-07-24 19:49:25 +03:00
except (socket.error, IOError) as e:
print("I/O error({0}): {1} ({2})".format(e.errno, e.strerror, e))
traceback.print_exception(*sys.exc_info())
2020-04-08 20:06:02 +03:00
break
2019-07-24 19:49:25 +03:00
return result
2016-11-23 18:49:18 +02:00
def process_args(argv):
args = []
skip_next = False
for i, arg in enumerate(argv[1:]):
if arg == '-h' or arg == '-?' or arg == '--help':
print_usage(argv[0])
exit(0)
2017-06-16 03:18:01 +03:00
elif i == 0 and (arg == 'diff' or arg == 'merge' or arg == '--temp-project'):
2016-11-23 18:49:18 +02:00
args.append(arg)
elif arg == '-l' or arg == '--line':
args.append(arg)
skip_next = True
2019-07-24 19:49:25 +03:00
elif arg == '-w' or arg == '--wait':
args.append('--wait')
2020-07-29 21:14:53 +03:00
elif arg == '-p' or arg == '--project':
args.append(arg)
2020-12-03 09:02:45 +02:00
elif arg == '-e' or arg == '--edit':
args.append(arg)
2016-11-23 18:49:18 +02:00
elif skip_next:
args.append(arg)
skip_next = False
else:
2017-03-24 21:18:44 +02:00
path = arg
2016-11-23 18:49:18 +02:00
if ':' in arg:
file_path, line_number = arg.rsplit(':', 1)
if line_number.isdigit():
args.append('-l')
args.append(line_number)
2017-03-24 21:18:44 +02:00
path = file_path
args.append(os.path.abspath(path))
2016-11-23 18:49:18 +02:00
return args
2016-11-23 18:49:18 +02:00
def try_activate_instance(args):
port_path = os.path.join(CONFIG_PATH, 'port')
token_path = os.path.join(SYSTEM_PATH, 'token')
if not (os.path.exists(port_path) and os.path.exists(token_path)):
return False
2018-07-26 03:14:04 +03:00
try:
with open(port_path) as pf:
port = int(pf.read())
with open(token_path) as tf:
token = tf.read()
2020-04-08 20:06:02 +03:00
except ValueError:
2018-07-26 03:14:04 +03:00
return False
s = socket.socket()
2019-07-24 19:49:25 +03:00
s.settimeout(1.0)
try:
s.connect(('127.0.0.1', port))
2016-11-23 18:49:18 +02:00
except (socket.error, IOError):
return False
2019-07-24 19:49:25 +03:00
paths = read_sequence_from_sock(s)
2020-04-08 20:06:02 +03:00
found = CONFIG_PATH in paths or os.path.realpath(CONFIG_PATH) in paths
if found:
2019-07-24 19:49:25 +03:00
write_to_sock(s, 'activate ' + token + '\0' + os.getcwd() + '\0' + '\0'.join(args))
s.settimeout(None)
response = read_sequence_from_sock(s)
2020-04-08 20:06:02 +03:00
if len(response) < 2 or response[0] != 'ok':
print('bad response: ' + str(response))
2019-07-24 19:49:25 +03:00
exit(1)
if len(response) > 2:
print(response[2])
exit(int(response[1]))
return False
2016-11-23 18:49:18 +02:00
def start_new_instance(args):
if sys.platform == 'darwin':
if len(args) > 0:
args.insert(0, '--args')
2020-12-03 09:02:45 +02:00
if '--wait' in args:
args.insert(0, '-W')
2020-04-08 20:06:02 +03:00
os.execv('/usr/bin/open', ['open', '-na', RUN_PATH] + args)
2016-05-11 18:11:57 +03:00
else:
2016-11-23 18:49:18 +02:00
bin_file = os.path.split(RUN_PATH)[1]
2016-05-11 18:11:57 +03:00
os.execv(RUN_PATH, [bin_file] + args)
2016-11-23 18:49:18 +02:00
ide_args = process_args(sys.argv)
if not try_activate_instance(ide_args):
start_new_instance(ide_args)