return flags and warn if HUP/ERR instead of raising exception
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 1 Oct 2015 16:11:46 +0000 (18:11 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 1 Oct 2015 16:11:46 +0000 (18:11 +0200)
follow.py

index 5cdd6ad..4b3cf49 100644 (file)
--- a/follow.py
+++ b/follow.py
@@ -94,15 +94,16 @@ NOT possible::
 
 from __future__ import print_function
 from warnings import warn
-import select
+from select import poll, POLLIN, POLLOUT, POLLPRI, POLLHUP, POLLERR, POLLNVAL
 from subprocess import Popen, PIPE
+from socket import socket as Socket
+
 from type_helpers import isstr
 
 # #############################################################################
 # CONSTANTS
 # #############################################################################
-DEFAULT_POLL_FLAGS = select.POLLIN | select.POLLPRI | select.POLLHUP
-#                     | select.POLLERR
+DEFAULT_POLL_FLAGS = POLLIN | POLLPRI | POLLHUP | POLLERR
 
 DEFAULT_POLL_TIMEOUT = 10
 
@@ -114,17 +115,17 @@ DEFAULT_POLL_TIMEOUT = 10
 def flags_to_str(flags):
     """ return short string representation for poll flags """
     text_parts = []
-    if flags & select.POLLIN:
+    if flags & POLLIN:
         text_parts.append('IN')
-    if flags & select.POLLOUT:
+    if flags & POLLOUT:
         text_parts.append('OUT')
-    if flags & select.POLLPRI:
+    if flags & POLLPRI:
         text_parts.append('PRI')
-    if flags & select.POLLERR:
+    if flags & POLLERR:
         text_parts.append('ERR')
-    if flags & select.POLLHUP:
+    if flags & POLLHUP:
         text_parts.append('HUP')
-    if flags & select.POLLNVAL:
+    if flags & POLLNVAL:
         text_parts.append('INVALID')
     return ','.join(text_parts)
 
@@ -214,7 +215,7 @@ class Follower(object):
             if key not in ('flags', 'timeout'):
                 raise ValueError('argument not recognized: {0}'.format(key))
 
-        self._poller = select.poll()
+        self._poller = poll()
 
         sources = []
         file_nos = []
@@ -251,7 +252,7 @@ class Follower(object):
     def next(self):
         """ main function to poll next line from sources
 
-        returns (source, desc, line_stripped)
+        returns (source, desc, line_stripped, flags)
         """
 
         while True:
@@ -273,9 +274,19 @@ class Follower(object):
                 if not flags & self._flags:
                     raise PollFlagsException(flags, desc)
 
-                if flags & select.POLLHUP:
-                    raise PollHupException(desc)
+                if flags & POLLHUP:
+                    #raise PollHupException(desc)
+                    warn('received a hangup during polling for {0}'
+                         .format(desc))
+
+                if flags & POLLERR:
+                    warn('received an err during polling for {0}'
+                         .format(desc))
 
+                if flags & POLLNVAL:
+                    warn('poll replied "invalid request" err during polling '
+                         'for {0}'
+                         .format(desc))
                 # read line from source
                 line = source.readline()
 
@@ -283,7 +294,7 @@ class Follower(object):
                     continue
                 # if reach here, we have a new line of text
 
-                return source, desc, line.strip()
+                return source, desc, line.strip(), flags
             # for poll results
         # end: inf loop
     # end: Follower._next_line
@@ -561,11 +572,9 @@ def test_follower_syslog():
     with open(syslog_file, 'r') as file_handle:
         flwr = Follower(file_handle, 'syslog file', proc.stdout, 'tail syslog',
                         proc.stderr, 'tail stderr')
-        for n_lines, (source, desc, line) in enumerate(flwr):
-            test_syslog_line(n_lines, source, desc, line, start_time)
-        # until user aborts
-    # end: with open syslog
-# end: function test_follower_syslog
+        for n_lines, (source, desc, line, flags) in enumerate(flwr):
+            test_syslog_line(n_lines, source, desc, line,
+                             today_str, start_time)
 
 
 def test_follower_context():
@@ -574,8 +583,9 @@ def test_follower_context():
     start_time = dt.now() - td(seconds=time_diff_seconds)
 
     with follow(syslog_file) as flwr:
-        for n_lines, (source, desc, line) in enumerate(flwr):
-            test_syslog_line(n_lines, source, desc, line, start_time)
+        for n_lines, (source, desc, line, flags) in enumerate(flwr):
+            test_syslog_line(n_lines, source, desc, line,
+                             today_str, start_time)
 
 
 def test():