2
0
pycharm-professional/charm

108 lines
3.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import struct
import sys
2016-07-21 19:10:53 +03:00
import os
import time
# see com.intellij.idea.SocketLock for the server side of this interface
RUN_PATH = u'/opt/pycharm-professional/bin/pycharm.sh'
2017-11-30 01:52:36 +02:00
CONFIG_PATH = u'~/.PyCharm2017.3/config'
SYSTEM_PATH = u'~/.PyCharm2017.3/system'
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' +
2017-03-24 21:18:44 +02:00
' {0} [project_dir]\n' +
' {0} [-l|--line line] [project_dir|--temp-project] file[:line]\n' +
2016-11-23 18:49:18 +02:00
' {0} diff <left> <right>\n' +
' {0} merge <local> <remote> [base] <merged>').format(cmd))
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
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
2017-03-24 21:18:44 +02:00
with open(port_path) as pf:
2016-11-23 18:49:18 +02:00
port = int(pf.read())
2017-03-24 21:18:44 +02:00
with open(token_path) as tf:
2016-11-23 18:49:18 +02:00
token = tf.read()
s = socket.socket()
s.settimeout(0.3)
try:
s.connect(('127.0.0.1', port))
2016-11-23 18:49:18 +02:00
except (socket.error, IOError):
return False
2016-11-23 18:49:18 +02:00
found = False
while True:
try:
2016-11-23 18:49:18 +02:00
path_len = struct.unpack('>h', s.recv(2))[0]
2017-06-16 03:18:01 +03:00
path = s.recv(path_len).decode('utf-8')
if os.path.abspath(path) == os.path.abspath(CONFIG_PATH):
found = True
break
2016-11-23 18:49:18 +02:00
except (socket.error, IOError):
return False
if found:
2016-11-23 18:49:18 +02:00
cmd = 'activate ' + token + '\0' + os.getcwd() + '\0' + '\0'.join(args)
2017-06-16 03:18:01 +03:00
if sys.version_info.major >= 3: cmd = cmd.encode('utf-8')
2016-11-23 18:49:18 +02:00
encoded = struct.pack('>h', len(cmd)) + cmd
s.send(encoded)
time.sleep(0.5) # don't close the socket immediately
return True
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')
os.execvp('open', ['-a', 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)