Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  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'/Applications/IntelliJ IDEA.app'
  13. CONFIG_PATH = u'/Users/izawa/Library/Preferences/IntelliJIdea2016.2'
  14. SYSTEM_PATH = u'/Users/izawa/Library/Caches/IntelliJIdea2016.2'
  15.  
  16. args = []
  17. skip_next = False
  18. for i, arg in enumerate(sys.argv[1:]):
  19. if arg == '-h' or arg == '-?' or arg == '--help':
  20. print(('Usage:\n' +
  21. ' {0} -h |-? | --help\n' +
  22. ' {0} [-l|--line line] file[:line]\n' +
  23. ' {0} diff <left> <right>\n' +
  24. ' {0} merge <local> <remote> [base] <merged>').format(sys.argv[0]))
  25. exit(0)
  26. elif arg == 'diff' and i == 0:
  27. args.append(arg)
  28. elif arg == 'merge' and i == 0:
  29. args.append(arg)
  30. elif arg == '-l' or arg == '--line':
  31. args.append(arg)
  32. skip_next = True
  33. elif skip_next:
  34. args.append(arg)
  35. skip_next = False
  36. else:
  37. if ':' in arg:
  38. file_path, line_number = arg.rsplit(':', 1)
  39. if line_number.isdigit():
  40. args.append('-l')
  41. args.append(line_number)
  42. args.append(os.path.abspath(file_path))
  43. else:
  44. args.append(os.path.abspath(arg))
  45. else:
  46. args.append(os.path.abspath(arg))
  47.  
  48.  
  49. def launch_with_port(port, token):
  50. found = False
  51.  
  52. s = socket.socket()
  53. s.settimeout(0.3)
  54. try:
  55. s.connect(('127.0.0.1', port))
  56. except:
  57. return False
  58.  
  59. while True:
  60. try:
  61. path_len = struct.unpack(">h", s.recv(2))[0]
  62. path = s.recv(path_len)
  63. if os.path.abspath(path) == os.path.abspath(CONFIG_PATH):
  64. found = True
  65. break
  66. except:
  67. break
  68.  
  69. if found:
  70. if args:
  71. cmd = "activate " + token + '\0' + os.getcwd() + "\0" + "\0".join(args)
  72. encoded = struct.pack(">h", len(cmd)) + cmd
  73. s.send(encoded)
  74. time.sleep(0.5) # don't close socket immediately
  75. return True
  76.  
  77. return False
  78.  
  79.  
  80. port_path = os.path.join(CONFIG_PATH, 'port')
  81. token_path = os.path.join(SYSTEM_PATH, 'token')
  82. if os.path.exists(port_path) and os.path.exists(token_path):
  83. try:
  84. f = open(port_path)
  85. port = int(f.read())
  86. f.close()
  87.  
  88. f = open(token_path)
  89. token = f.read()
  90. f.close()
  91.  
  92. launch_with_port(port, token)
  93. except:
  94. type, value, traceback = sys.exc_info()
  95. print('Cannot activate a running instance: ' + str(value))
  96. else:
  97. print('No IDE instance has been found. New one will be started.')
  98. if sys.platform == "darwin":
  99. # OS X: RUN_PATH is *.app path
  100. if len(args):
  101. args.insert(0, "--args")
  102. os.execvp("open", ["-a", RUN_PATH] + args)
  103. else:
  104. # Unix common
  105. bin_dir, bin_file = os.path.split(RUN_PATH)
  106. os.execv(RUN_PATH, [bin_file] + args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement