CPP-5488 Swift plugin version detection is broken
[idea/community.git] / platform / platform-resources / src / launcher.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import socket
5 import struct
6 import sys
7 import os
8 import time
9
10 # see com.intellij.idea.SocketLock for the server side of this interface
11
12 RUN_PATH = u'$RUN_PATH$'
13 CONFIG_PATH = u'$CONFIG_PATH$'
14
15 args = []
16 skip_next = False
17 for i, arg in enumerate(sys.argv[1:]):
18     if arg == '-h' or arg == '-?' or arg == '--help':
19         print(('Usage:\n' +
20                '  {0} -h |-? | --help\n' +
21                '  {0} [-l|--line line] file[:line]\n' +
22                '  {0} diff <left> <right>' +
23                '  {0} merge <local> <remote> [base] <merged>').format(sys.argv[0]))
24         exit(0)
25     elif arg == 'diff' and i == 0:
26         args.append(arg)
27     elif arg == 'merge' and i == 0:
28         args.append(arg)
29     elif arg == '-l' or arg == '--line':
30         args.append(arg)
31         skip_next = True
32     elif skip_next:
33         args.append(arg)
34         skip_next = False
35     else:
36         if ':' in arg:
37             file_path, line_number = arg.rsplit(':', 1)
38             if line_number.isdigit():
39                 args.append('-l')
40                 args.append(line_number)
41                 args.append(os.path.abspath(file_path))
42             else:
43                 args.append(os.path.abspath(arg))
44         else:
45             args.append(os.path.abspath(arg))
46
47
48 def launch_with_port(port):
49     found = False
50
51     s = socket.socket()
52     s.settimeout(0.3)
53     try:
54         s.connect(('127.0.0.1', port))
55     except:
56         return False
57
58     while True:
59         try:
60             path_len = struct.unpack(">h", s.recv(2))[0]
61             path = s.recv(path_len)
62             if os.path.abspath(path) == os.path.abspath(CONFIG_PATH):
63                 found = True
64                 break
65         except:
66             break
67
68     if found:
69         if args:
70             cmd = "activate " + os.getcwd() + "\0" + "\0".join(args)
71             encoded = struct.pack(">h", len(cmd)) + cmd
72             s.send(encoded)
73             time.sleep(0.5)  # don't close socket immediately
74         return True
75
76     return False
77
78
79 port = -1
80 try:
81     f = open(os.path.join(CONFIG_PATH, 'port'))
82     port = int(f.read())
83 except Exception:
84     type, value, traceback = sys.exc_info()
85     print('No IDE instance has been found. New one will be started.')
86     port = -1
87
88 if port == -1:
89     # SocketLock actually allows up to 50 ports, but the checking takes too long
90     for port in range(6942, 6942 + 10):
91         if launch_with_port(port):
92             exit()
93 else:
94     if launch_with_port(port):
95         exit()
96
97 if sys.platform == "darwin":
98     # OS X: RUN_PATH is *.app path
99     if len(args):
100         args.insert(0, "--args")
101     os.execvp("open", ["-a", RUN_PATH] + args)
102 else:
103     # unix common
104     bin_dir, bin_file = os.path.split(RUN_PATH)
105     os.execv(RUN_PATH, [bin_file] + args)