[GRASS-SVN] r30183 - grass/trunk/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Feb 16 10:02:55 EST 2008


Author: martinl
Date: 2008-02-16 10:02:55 -0500 (Sat, 16 Feb 2008)
New Revision: 30183

Modified:
   grass/trunk/gui/wxpython/gui_modules/gcmd.py
Log:
wxGUI: stream (stderr, stdout) redirection fixed for commands. In the future could be replaced with wx.Execute or something portable (MS Windows), etc.

Modified: grass/trunk/gui/wxpython/gui_modules/gcmd.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gcmd.py	2008-02-16 12:51:19 UTC (rev 30182)
+++ grass/trunk/gui/wxpython/gui_modules/gcmd.py	2008-02-16 15:02:55 UTC (rev 30183)
@@ -433,12 +433,11 @@
 
     def run(self):
         """Run command"""
+        # TODO: wx.Exectute/wx.Process (?)
         self.module = Popen(self.cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
-            
-        # close_fds=False) ### Unix only
 
         if self.stdin: # read stdin if requested ...
             self.module.stdin.write(self.stdin)
@@ -448,43 +447,59 @@
             return
 
         # redirect standard outputs...
+        if self.stdout or self.stderr:
+            self.__redirect_stream()
+
+    def __read_all(self, fd):
+        out = ""
+        while True:
+            try:
+                bytes = fd.read(4096)
+            except IOError, e:
+                if e[0] != errno.EAGAIN:
+                    raise
+                break
+            if not bytes:
+                break
+            out += bytes
+
+        return out
+
+    def __redirect_stream(self):
+        """Redirect stream"""
         if self.stdout:
-            self.__redirect_stream(self.module.stdout, self.stdout)
-        
+            # make module stdout/stderr non-blocking
+            out_fileno = self.module.stdout.fileno()
+            # FIXME (MS Windows)
+            flags = fcntl.fcntl(out_fileno, fcntl.F_GETFL)
+            fcntl.fcntl(out_fileno, fcntl.F_SETFL, flags| os.O_NONBLOCK)
+            
         if self.stderr:
-            self.__redirect_stream(self.module.stderr, self.stderr)
+            # make module stdout/stderr non-blocking
+            out_fileno = self.module.stderr.fileno()
+            # FIXME (MS Windows)
+            flags = fcntl.fcntl(out_fileno, fcntl.F_GETFL)
+            fcntl.fcntl(out_fileno, fcntl.F_SETFL, flags| os.O_NONBLOCK)
 
-    def __redirect_stream(self, streamFrom, streamTo):
-        """Redirect stream"""
-        # make stdout/stderr non-blocking
-        out_fileno = streamFrom.fileno()
-            
-        # FIXME (MS Windows)
-        flags = fcntl.fcntl(out_fileno, fcntl.F_GETFL)
-        fcntl.fcntl(out_fileno, fcntl.F_SETFL, flags| os.O_NONBLOCK)
-            
         # wait for the process to end, sucking in stuff until it does end
         while self.module.poll() is None:
-            # evt = wxgui_utils.UpdateGMConsoleEvent()
-            # wx.PostEvent(self.stdout, evt)
-            # wx.PostEvent(self.stderr, evt)
-            try:
-                line = streamFrom.read()
-                # self.rerr = self.__parseString(line)
-                streamTo.write(line)
-            except IOError:
-                pass
-                
-            time.sleep(0.1)
-            
+            time.sleep(.1)
+            if self.stdout:
+                line = self.__read_all(self.module.stdout)
+                self.stdout.write(line)
+            if self.stderr:
+                line = self.__read_all(self.module.stderr)
+                self.stderr.write(line)
+
         # get the last output
-        try:
-            line = streamFrom.read()
+        if self.stdout:
+            line = self.__read_all(self.module.stdout)
+            self.stdout.write(line)
+        if self.stderr:
+            line = self.__read_all(self.module.stderr)
+            self.stderr.write(line)
             self.rerr = self.__parseString(line)
-            streamTo.write(line)
-        except IOError:
-            pass
-
+        
     def __parseString(self, string):
         """Parse line
 



More information about the grass-commit mailing list