HosipLan

Untitled

Jan 18th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. #!/usr/bin/python
  2. import socket
  3. import struct
  4. import sys
  5. import os
  6. import os.path
  7. import time
  8.  
  9. # see com.intelij.idea.SocketLock for the server side of this interface
  10.  
  11. RUN_PATH = '/opt/phpstorm/bin/phpstorm.sh'
  12. CONFIG_PATH = '/home/hosiplan/.WebIde10/config'
  13.  
  14. args = []
  15. skip_next = False
  16. for arg in sys.argv[1:]:
  17.     if arg == '-l' or arg == '--line':
  18.         args.append(arg)
  19.         skip_next = True
  20.     elif skip_next:
  21.         args.append(arg)
  22.         skip_next = False
  23.     else:
  24.         args.append(os.path.abspath(arg))
  25.  
  26. def launch_with_port(port):
  27.     found = False
  28.  
  29.     s = socket.socket()
  30.     s.settimeout(0.3)
  31.     try:
  32.         s.connect(('127.0.0.1', port))
  33.     except:
  34.         return False
  35.    
  36.     while True:
  37.         try:
  38.             path_len = struct.unpack(">h", s.recv(2))[0]
  39.             path = s.recv(path_len)
  40.             path = os.path.abspath(path)
  41.             if os.path.abspath(path) == os.path.abspath(CONFIG_PATH):
  42.                 found = True
  43.                 break
  44.         except:
  45.             break
  46.    
  47.     if found:
  48.         if args:
  49.             cmd = "activate " + "\0".join(args)
  50.             encoded = struct.pack(">h", len(cmd)) + cmd
  51.             s.send(encoded)
  52.             time.sleep(0.5)   # don't close socket immediately
  53.         return True
  54.  
  55.     return False
  56.  
  57. port = -1
  58. try:
  59.     f = open(os.path.join(CONFIG_PATH, 'port'))
  60.     port = int(f.read())
  61. except Exception:
  62.     type, value, traceback = sys.exc_info()
  63.     print(value)
  64.     port = -1
  65.  
  66. if port == -1:
  67.     # SocketLock actually allows up to 50 ports, but the checking takes too long
  68.     for port in range(6942, 6942+10):
  69.         if launch_with_port(port): exit()
  70. else:
  71.     if launch_with_port(port): exit()
  72.  
  73. if sys.platform == "darwin":
  74.     # Mac OS: RUN_PATH is *.app path
  75.     if len(args):
  76.         args.insert(0, "--args")
  77.     os.execvp("open", ["-a", RUN_PATH] + args)
  78. else:
  79.     # unix common
  80.     bin_dir, bin_file = os.path.split(RUN_PATH)
  81.     os.chdir(bin_dir)
  82.     os.execv(bin_file, [bin_file] + args)
Advertisement
Add Comment
Please, Sign In to add comment