[packages/pycharm-community: 2/6] add python wrapper

glen glen at pld-linux.org
Mon Feb 1 17:00:54 CET 2016


commit 3c9d149cd0dc0879ba2c54b4e1f6ec3eb0dc6934
Author: Elan Ruusamäe <glen at delfi.ee>
Date:   Fri Jan 22 22:16:22 2016 +0200

    add python wrapper
    
    this can be used to launch existing instance
    file obtained from it's own runtime install

 charm.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
---
diff --git a/charm.py b/charm.py
new file mode 100755
index 0000000..fb0df9f
--- /dev/null
+++ b/charm.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import socket
+import struct
+import sys
+import os
+import time
+
+# see com.intellij.idea.SocketLock for the server side of this interface
+
+RUN_PATH = u'/usr/lib/pycharm-community/bin/pycharm.sh'
+CONFIG_PATH = u'/home/glen/.config/PyCharm'
+
+args = []
+skip_next = False
+for i, arg in enumerate(sys.argv[1:]):
+    if arg == '-h' or arg == '-?' or arg == '--help':
+        print(('Usage:\n' +
+               '  {0} -h |-? | --help\n' +
+               '  {0} [-l|--line line] file[:line]\n' +
+               '  {0} diff <left> <right>' +
+               '  {0} merge <local> <remote> [base] <merged>').format(sys.argv[0]))
+        exit(0)
+    elif arg == 'diff' and i == 0:
+        args.append(arg)
+    elif arg == 'merge' and i == 0:
+        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:
+        if ':' in arg:
+            file_path, line_number = arg.rsplit(':', 1)
+            if line_number.isdigit():
+                args.append('-l')
+                args.append(line_number)
+                args.append(os.path.abspath(file_path))
+            else:
+                args.append(os.path.abspath(arg))
+        else:
+            args.append(os.path.abspath(arg))
+
+
+def launch_with_port(port):
+    found = False
+
+    s = socket.socket()
+    s.settimeout(0.3)
+    try:
+        s.connect(('127.0.0.1', port))
+    except:
+        return False
+
+    while True:
+        try:
+            path_len = struct.unpack(">h", s.recv(2))[0]
+            path = s.recv(path_len)
+            if os.path.abspath(path) == os.path.abspath(CONFIG_PATH):
+                found = True
+                break
+        except:
+            break
+
+    if found:
+        if args:
+            cmd = "activate " + os.getcwd() + "\0" + "\0".join(args)
+            encoded = struct.pack(">h", len(cmd)) + cmd
+            s.send(encoded)
+            time.sleep(0.5)  # don't close socket immediately
+        return True
+
+    return False
+
+
+port = -1
+try:
+    f = open(os.path.join(CONFIG_PATH, 'port'))
+    port = int(f.read())
+except Exception:
+    type, value, traceback = sys.exc_info()
+    print('No IDE instance has been found. New one will be started.')
+    port = -1
+
+if port == -1:
+    # SocketLock actually allows up to 50 ports, but the checking takes too long
+    for port in range(6942, 6942 + 10):
+        if launch_with_port(port):
+            exit()
+else:
+    if launch_with_port(port):
+        exit()
+
+if sys.platform == "darwin":
+    # OS X: RUN_PATH is *.app path
+    if len(args):
+        args.insert(0, "--args")
+    os.execvp("open", ["-a", RUN_PATH] + args)
+else:
+    # unix common
+    bin_dir, bin_file = os.path.split(RUN_PATH)
+    os.execv(RUN_PATH, [bin_file] + args)
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/pycharm-community.git/commitdiff/9fc6a42b010b53691be52a089f4704e328f75a95



More information about the pld-cvs-commit mailing list