platform: implement readpipe()
authorGregory Szorc <gregory.szorc@gmail.com>
Fri, 15 Aug 2014 20:02:18 -0700
changeset 22245 234e4c24b980
parent 22244 172036d60b22
child 22246 331cbf088c4c
platform: implement readpipe() Reading all available data from a pipe has a platform-dependent implementation. This patch establishes platform.readpipe() by copying the inline implementation in sshpeer.readerr(). The implementations for POSIX and Windows are currently identical. The POSIX implementation will be changed in a subsequent patch.
mercurial/posix.py
mercurial/sshpeer.py
mercurial/util.py
mercurial/windows.py
--- a/mercurial/posix.py	Fri Aug 15 19:18:21 2014 -0700
+++ b/mercurial/posix.py	Fri Aug 15 20:02:18 2014 -0700
@@ -567,3 +567,16 @@
 def statisexec(st):
     '''check whether a stat result is an executable file'''
     return st and (st.st_mode & 0100 != 0)
+
+def readpipe(pipe):
+    """Read all available data from a pipe."""
+    chunks = []
+    while True:
+        size = os.fstat(pipe.fileno()).st_size
+        if not size:
+            break
+
+        s = pipe.read(size)
+        if not s:
+            break
+        chunks.append(s)
--- a/mercurial/sshpeer.py	Fri Aug 15 19:18:21 2014 -0700
+++ b/mercurial/sshpeer.py	Fri Aug 15 20:02:18 2014 -0700
@@ -103,13 +103,8 @@
         return self._caps
 
     def readerr(self):
-        while True:
-            size = util.fstat(self.pipee).st_size
-            if size == 0:
-                break
-            s = self.pipee.read(size)
-            if not s:
-                break
+        s = util.readpipe(self.pipee)
+        if s:
             for l in s.splitlines():
                 self.ui.status(_("remote: "), l, '\n')
 
--- a/mercurial/util.py	Fri Aug 15 19:18:21 2014 -0700
+++ b/mercurial/util.py	Fri Aug 15 20:02:18 2014 -0700
@@ -53,6 +53,7 @@
 popen = platform.popen
 posixfile = platform.posixfile
 quotecommand = platform.quotecommand
+readpipe = platform.readpipe
 rename = platform.rename
 samedevice = platform.samedevice
 samefile = platform.samefile
--- a/mercurial/windows.py	Fri Aug 15 19:18:21 2014 -0700
+++ b/mercurial/windows.py	Fri Aug 15 20:02:18 2014 -0700
@@ -336,3 +336,18 @@
 def statisexec(st):
     '''check whether a stat result is an executable file'''
     return False
+
+def readpipe(pipe):
+    """Read all available data from a pipe."""
+    chunks = []
+    while True:
+        size = os.fstat(pipe.fileno()).st_size
+        if not size:
+            break
+
+        s = pipe.read(size)
+        if not s:
+            break
+        chunks.append(s)
+
+    return ''.join(chunks)