OSDN Git Service

Fix problems with app module naming for apps hosted by javaw.exe.
authorJames Teh <jamie@nvaccess.org>
Wed, 12 Nov 2014 03:31:11 +0000 (13:31 +1000)
committerJames Teh <jamie@nvaccess.org>
Wed, 12 Nov 2014 03:31:11 +0000 (13:31 +1000)
The code previously didn't handle some quotes correctly, didn't handle -cp/-classpath and didn't know about -jar.

source/appModules/javaw.py

index c41e723..3d1a2b8 100644 (file)
@@ -7,11 +7,31 @@
 """Support for app specific modules for Java apps hosted by javaw.exe.\r
 """\r
 \r
-import re\r
+import os\r
+import shlex\r
 import appModuleHandler\r
 from appModuleHandler import AppModule\r
 \r
-RE_CMDLINE_CLASS = re.compile(r'(?:"[^"]+"|[^ ]+)(?: +-[^ "]+(?:"[^"]+")?)* +([^ ]+)')\r
+def _getEntryPoint(cmd):\r
+       cmd = iter(shlex.split(cmd))\r
+       # First argument is the executable.\r
+       next(cmd)\r
+       for arg in cmd:\r
+               if arg in ("-cp", "-classpath"):\r
+                       # This option consuems the next argument.\r
+                       next(cmd)\r
+                       continue\r
+               if arg.startswith("-jar"):\r
+                       # Next argument is the jar. Remove the extension.\r
+                       return os.path.splitext(next(cmd))[0]\r
+               if arg.startswith("-"):\r
+                       continue\r
+               if not arg:\r
+                       continue\r
+               # Class.\r
+               return arg\r
+       raise LookupError\r
+\r
 def getAppNameFromHost(processId):\r
        # Some apps have launcher executables which launch javaw.exe to run the app.\r
        # In this case, the parent process will usually be the launcher.\r
@@ -23,11 +43,11 @@ def getAppNameFromHost(processId):
                        # The parent isn't a shell, so it's probably a launcher.\r
                        return name\r
 \r
-       # Try getting the class from the command line.\r
+       # Try getting the class/jar from the command line.\r
        cmd = proc.CommandLine\r
        if not cmd:\r
                raise LookupError\r
-       m = RE_CMDLINE_CLASS.match(cmd)\r
-       if not m:\r
+       try:\r
+               return "javaw_" + _getEntryPoint(cmd).replace(".", "_")\r
+       except:\r
                raise LookupError\r
-       return "javaw_" + m.group(1).replace(".", "_")\r