[packages/phpstorm] add python wrapper which can open files from commandline

glen glen at pld-linux.org
Fri Apr 18 00:13:26 CEST 2014


commit 486dfff479f8fcd05601e0ca40dffdfeec124bce
Author: Elan Ruusamäe <glen at delfi.ee>
Date:   Fri Apr 18 00:18:35 2014 +0300

    add python wrapper which can open files from commandline
    
    wrapper itself created by phpstorm itself on first install, adjusted to
    pld fixed paths

 phpstorm.py   | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 phpstorm.spec |  5 +--
 2 files changed, 102 insertions(+), 2 deletions(-)
---
diff --git a/phpstorm.spec b/phpstorm.spec
index a2dd9dc..0efe033 100644
--- a/phpstorm.spec
+++ b/phpstorm.spec
@@ -10,6 +10,7 @@ Source0:	http://download.jetbrains.com/webide/PhpStorm-%{version}.tar.gz
 # NoSource0-md5:	5c68dce5fa53ce2ff42fa8a590561c40
 NoSource:	0
 Source1:	%{name}.desktop
+Source2:	%{name}.py
 Patch0:		pld.patch
 URL:		http://www.jetbrains.com/phpstorm/
 BuildRequires:	jpackage-utils
@@ -75,7 +76,7 @@ cp -a$l bin help lib license plugins $RPM_BUILD_ROOT%{_appdir}
 ln -s %{_pixmapsdir}/%{name}.png $RPM_BUILD_ROOT%{_appdir}/bin
 cp -p webide.png $RPM_BUILD_ROOT%{_pixmapsdir}/%{name}.png
 cp -p %{SOURCE1} $RPM_BUILD_ROOT%{_desktopdir}
-ln -s %{_appdir}/bin/phpstorm.sh $RPM_BUILD_ROOT%{_bindir}/phpstorm
+install -p %{SOURCE2} $RPM_BUILD_ROOT%{_bindir}/%{name}
 
 %clean
 rm -rf $RPM_BUILD_ROOT
@@ -89,9 +90,9 @@ rm -rf $RPM_BUILD_ROOT
 %{_appdir}/plugins
 %dir %{_appdir}/bin
 %{_appdir}/bin/%{name}*.vmoptions
+%{_appdir}/bin/%{name}.png
 %{_appdir}/bin/idea.properties
 %{_appdir}/bin/log.xml
-%{_appdir}/bin/%{name}.png
 %attr(755,root,root) %{_appdir}/bin/%{name}.sh
 %attr(755,root,root) %{_appdir}/bin/inspect.sh
 %attr(755,root,root) %{_appdir}/bin/fsnotifier*
diff --git a/phpstorm.py b/phpstorm.py
new file mode 100755
index 0000000..fc75c88
--- /dev/null
+++ b/phpstorm.py
@@ -0,0 +1,99 @@
+#!/usr/bin/python
+
+import socket
+import struct
+import sys
+import os
+import time
+
+# see com.intellij.idea.SocketLock for the server side of this interface
+
+RUN_PATH = '/usr/lib/phpstorm/bin/phpstorm.sh'
+CONFIG_PATH = os.path.expanduser('~/.config/PhpStorm')
+
+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 file1 file2').format(sys.argv[0]))
+        exit(0)
+    elif arg == 'diff' 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)
+            path = os.path.abspath(path)
+            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(value)
+    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":
+    # Mac OS: 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.chdir(bin_dir)
+    os.execv(bin_file, [bin_file] + args)
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/phpstorm.git/commitdiff/00f9a298f0b61133f95f5237bc617b019678ff89



More information about the pld-cvs-commit mailing list