[Piglit] [PATCH 20/20] framework: guard POSIX specific code in monitoring module

Dylan Baker dylan at pnwbakers.com
Wed Jun 1 23:50:27 UTC 2016


Fixes running on windows.

cc: Brian Paul <brianp at vmware.com>
cc: Olivier Berthier <olivierx.berthier at linux.intel.com>
Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---

This is not the "right" solution, but it should get everyone up and
running again. Then we can hopefully sort out what the right solution is
and get that implemented.

 framework/monitoring.py       | 172 +++++++++++++++++++++---------------------
 unittests/monitoring_tests.py |   5 ++
 2 files changed, 93 insertions(+), 84 deletions(-)

diff --git a/framework/monitoring.py b/framework/monitoring.py
index f178bd6..1060cc5 100644
--- a/framework/monitoring.py
+++ b/framework/monitoring.py
@@ -36,7 +36,6 @@ from __future__ import (
 )
 import abc
 import errno
-import fcntl
 import os
 import re
 
@@ -219,93 +218,98 @@ class BaseMonitoring(object):
                     return line
 
 
-class MonitoringFile(BaseMonitoring):
-    """Monitoring from a file
+if os.name == 'posix':
+    import fcntl
 
-    This class is for monitoring the system from a file that
-    can be a standard file or a locked file. The locked file
-    algorithm uses fnctl, so it doesn't work on non Unix systems
 
-    Arguments:
-    is_locked -- True if the target is a locked file
+    class MonitoringFile(BaseMonitoring):
+        """Monitoring from a file
 
-    """
-    _is_locked = False
-
-    def __init__(self, monitoring_source, regex, is_locked=False):
-        """Create a MonitoringFile instance"""
-        self._last_message = None
-        self._is_locked = is_locked
-        super(MonitoringFile, self).__init__(monitoring_source, regex)
-
-    def update_monitoring(self):
-        """Open the file and get the differences
+        This class is for monitoring the system from a file that
+        can be a standard file or a locked file. The locked file
+        algorithm uses fnctl, so it doesn't work on non Unix systems
 
-        Get the contents of the file, then calculate new messages.
-        This implements also a specific method for reading locked files.
+        Arguments:
+        is_locked -- True if the target is a locked file
 
         """
+        _is_locked = False
+
+        def __init__(self, monitoring_source, regex, is_locked=False):
+            """Create a MonitoringFile instance"""
+            self._last_message = None
+            self._is_locked = is_locked
+            super(MonitoringFile, self).__init__(monitoring_source, regex)
+
+        def update_monitoring(self):
+            """Open the file and get the differences
+
+            Get the contents of the file, then calculate new messages.
+            This implements also a specific method for reading locked files.
+
+            """
+
+            try:
+                with open(self._monitoring_source, 'r') as f:
+                    lines = []
+                    if self._is_locked:
+                        # Create a duplicated file descriptor, this avoid lock
+                        fd = os.dup(f.fileno())
+                        # use I/O control for reading the lines
+                        fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK)
+
+                        while True:
+                            try:
+                                line = os.read(fd, 1024)
+                                if not line:
+                                    break;
+                            except OSError as e:
+                                if e.errno == errno.EAGAIN:
+                                    break
+                                else:
+                                    raise e
+                            lines.append(line.decode("utf-8", "strict"))
+                        os.close(fd)
+
+                    else:
+                        lines = f.read().splitlines()
+
+                    f.close()
+
+                    # Find all new entries, do this by slicing the list of the
+                    # lines to only returns elements after the last element
+                    # stored. If there are not matches a value error is raised,
+                    # that means all of the lines are new
+                    l = 0
+                    for index, item in enumerate(reversed(lines)):
+                        if item == self._last_message:
+                            l = len(lines) - index  # don't include the matched element
+                            break
+                    self._new_messages = lines[l:]
+                    # Attempt to store the last element of lines,
+                    # unless there was no line
+                    self._last_message = lines[-1] if lines else None
+            except Exception:
+                # if an error occured, we consider there are no new messages
+                self._new_messages = []
+                pass
+
+
+    class MonitoringLinuxDmesg(BaseMonitoring, LinuxDmesg):
+        """Monitoring on dmesg
+
+        This class is for monitoring on the system dmesg. It's inherited
+        from LinuxDmesg for the dmesg processing methods.
+
+        Work only on Linux operating system.
 
-        try:
-            with open(self._monitoring_source, 'r') as f:
-                lines = []
-                if self._is_locked:
-                    # Create a duplicated file descriptor, this avoid lock
-                    fd = os.dup(f.fileno())
-                    # use I/O control for reading the lines
-                    fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK)
-
-                    while True:
-                        try:
-                            line = os.read(fd, 1024)
-                            if not line:
-                                break;
-                        except OSError as e:
-                            if e.errno == errno.EAGAIN:
-                                break
-                            else:
-                                raise e
-                        lines.append(line.decode("utf-8", "strict"))
-                    os.close(fd)
-
-                else:
-                    lines = f.read().splitlines()
-
-                f.close()
-
-                # Find all new entries, do this by slicing the list of the lines to only
-                # returns elements after the last element stored. If there are not
-                # matches a value error is raised, that means all of the lines are new
-                l = 0
-                for index, item in enumerate(reversed(lines)):
-                    if item == self._last_message:
-                        l = len(lines) - index  # don't include the matched element
-                        break
-                self._new_messages = lines[l:]
-                # Attempt to store the last element of lines,
-                # unless there was no line
-                self._last_message = lines[-1] if lines else None
-        except Exception:
-            # if an error occured, we consider there are no new messages
-            self._new_messages = []
-            pass
-
-
-class MonitoringLinuxDmesg(BaseMonitoring, LinuxDmesg):
-    """Monitoring on dmesg
-
-    This class is for monitoring on the system dmesg. It's inherited
-    from LinuxDmesg for the dmesg processing methods.
-
-    Work only on Linux operating system.
-
-    """
-    def __init__(self, monitoring_source, regex):
-        """Create a MonitoringLinuxDmesg instance"""
-        self.DMESG_COMMAND = ['dmesg']+monitoring_source.split()
-        BaseMonitoring.__init__(self, monitoring_source, regex)
-        LinuxDmesg.__init__(self)
-
-    def update_monitoring(self):
-        """Call update_dmesg"""
-        self.update_dmesg()
+        """
+        def __init__(self, monitoring_source, regex):
+            """Create a MonitoringLinuxDmesg instance"""
+            self.DMESG_COMMAND = ['dmesg']+monitoring_source.split()
+            BaseMonitoring.__init__(self, monitoring_source, regex)
+            LinuxDmesg.__init__(self)
+
+        def update_monitoring(self):
+            """Call update_dmesg"""
+            self.update_dmesg()
diff --git a/unittests/monitoring_tests.py b/unittests/monitoring_tests.py
index b2364bc..3981df0 100644
--- a/unittests/monitoring_tests.py
+++ b/unittests/monitoring_tests.py
@@ -55,6 +55,7 @@ class TestMonitoring(object):
         self.error_contents = r'BUG:bar\n'
         self.monitoring = monitoring.Monitoring(False)
 
+    @utils.nose.Skip.platform('linux')
     def test_Monitoring_delete_rule(self):
         """monitoring.Monitoring: add and delete rule."""
 
@@ -82,6 +83,7 @@ class TestMonitoring(object):
                                      tfile,
                                      self.regex)
 
+    @utils.nose.Skip.platform('linux')
     def test_Monitoring_file_error(self):
         """monitoring.Monitoring: error found on a file."""
 
@@ -98,6 +100,7 @@ class TestMonitoring(object):
 
         nt.assert_equal(self.monitoring.abort_needed, True)
 
+    @utils.nose.Skip.platform('linux')
     def test_Monitoring_file_no_error(self):
         """monitoring.Monitoring: no error found on a file."""
 
@@ -114,6 +117,7 @@ class TestMonitoring(object):
 
         nt.assert_equal(self.monitoring.abort_needed, False)
 
+    @utils.nose.Skip.platform('linux')
     def test_Monitoring_locked_file_error(self):
         """monitoring.Monitoring: error found on a locked file."""
 
@@ -130,6 +134,7 @@ class TestMonitoring(object):
 
         nt.assert_equal(self.monitoring.abort_needed, True)
 
+    @utils.nose.Skip.platform('linux')
     def test_Monitoring_locked_file_no_error(self):
         """monitoring.Monitoring: no error found on a locked file."""
 
-- 
2.8.3



More information about the Piglit mailing list