util: add a simple poll utility
authorPierre-Yves David <pierre-yves.david@fb.com>
Wed, 20 May 2015 18:00:05 -0500
changeset 25420 c2ec81891502
parent 25419 6e62a5b3442d
child 25421 3dd3ccf7b807
util: add a simple poll utility We'll use it to detect when a sshpeer have server output to be displayed. The implementation is super basic because all case support is not the focus of this series.
mercurial/posix.py
mercurial/util.py
mercurial/windows.py
--- a/mercurial/posix.py	Wed Jun 03 14:22:21 2015 -0700
+++ b/mercurial/posix.py	Wed May 20 18:00:05 2015 -0500
@@ -8,6 +8,7 @@
 from i18n import _
 import encoding
 import os, sys, errno, stat, getpass, pwd, grp, socket, tempfile, unicodedata
+import select
 import fcntl, re
 
 posixfile = open
@@ -594,6 +595,19 @@
     '''check whether a stat result is an executable file'''
     return st and (st.st_mode & 0100 != 0)
 
+def poll(fds):
+    """block until something happens on any file descriptor
+
+    This is a generic helper that will check for any activity
+    (read, write.  exception) and return the list of touched files.
+
+    In unsupported cases, it will raise a NotImplementedError"""
+    try:
+        res = select.select(fds, fds, fds)
+    except ValueError: # out of range file descriptor
+        raise NotImplementedError()
+    return sorted(list(set(sum(res, []))))
+
 def readpipe(pipe):
     """Read all available data from a pipe."""
     # We can't fstat() a pipe because Linux will always report 0.
--- a/mercurial/util.py	Wed Jun 03 14:22:21 2015 -0700
+++ b/mercurial/util.py	Wed May 20 18:00:05 2015 -0500
@@ -54,6 +54,7 @@
 oslink = platform.oslink
 parsepatchoutput = platform.parsepatchoutput
 pconvert = platform.pconvert
+poll = platform.poll
 popen = platform.popen
 posixfile = platform.posixfile
 quotecommand = platform.quotecommand
--- a/mercurial/windows.py	Wed Jun 03 14:22:21 2015 -0700
+++ b/mercurial/windows.py	Wed May 20 18:00:05 2015 -0500
@@ -371,6 +371,10 @@
     '''check whether a stat result is an executable file'''
     return False
 
+def poll(fds):
+    # see posix.py for description
+    raise NotImplementedError()
+
 def readpipe(pipe):
     """Read all available data from a pipe."""
     chunks = []