[GRASS-SVN] r61135 - grass/trunk/lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 3 07:39:53 PDT 2014


Author: martinl
Date: 2014-07-03 07:39:53 -0700 (Thu, 03 Jul 2014)
New Revision: 61135

Modified:
   grass/trunk/lib/python/script/core.py
Log:
wingrass: fix Popen class (contribution of Glynn and Anna)

Modified: grass/trunk/lib/python/script/core.py
===================================================================
--- grass/trunk/lib/python/script/core.py	2014-07-03 04:03:02 UTC (rev 61134)
+++ grass/trunk/lib/python/script/core.py	2014-07-03 14:39:53 UTC (rev 61135)
@@ -41,8 +41,28 @@
 
 
 class Popen(subprocess.Popen):
-    pass
+    _builtin_exts = set(['.com', '.exe', '.bat', '.cmd'])
 
+    @staticmethod
+    def _escape_for_shell(arg):
+        # TODO: what are cmd.exe's parsing rules?
+        return arg
+
+    def __init__(self, args, **kwargs):
+        if ( sys.platform == 'win32'
+             and isinstance(args, list)
+             and not kwargs.get('shell', False)
+             and kwargs.get('executable') is None ):
+            cmd = shutil_which(args[0])
+            if cmd is None:
+                raise OSError
+            args = [cmd] + args[1:]
+            name, ext = os.path.splitext(cmd)
+            if ext.lower() not in self._builtin_exts:
+                kwargs['shell'] = True
+                args = [self._escape_for_shell(arg) for arg in args]
+        subprocess.Popen.__init__(self, args, **kwargs)
+
 PIPE = subprocess.PIPE
 STDOUT = subprocess.STDOUT
 



More information about the grass-commit mailing list