This class implements a class for reading dmesg, including a dummy class for running on non-posix systems. It works as a class, and is cleaner than the current implementation.
Signed-off-by: Dylan Baker <[email protected]> --- framework/dmesg.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 framework/dmesg.py diff --git a/framework/dmesg.py b/framework/dmesg.py new file mode 100644 index 0000000..9a23c14 --- /dev/null +++ b/framework/dmesg.py @@ -0,0 +1,108 @@ +# Copyright (c) 2013 Intel Corporation +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice (including the next +# paragraph) shall be included in all copies or substantial portions of the +# Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. + +""" Module implementing classes for reading posix dmesg """ + +import os +import subprocess +from threads import synchronized_self + +__all__ = ['Dmesg'] + +# plain text list of statuses to be considered either a warn or a fail, any +# statuses not on this list will simply be ignored. +WARN_STATUSES = [] +FAIL_STATUSES = [] + + +class PosixDmesg(object): + """ Read dmesg on posix systems + + This reads the dmesg ring buffer, stores the contents of the buffer, and + then compares it whenever called, returning the difference between the + calls. + + This class is threadsafe, but with the caveat that the test reported dmesg + problems might not be the test that generated them. dmesg reporting can + only be considered authoratative when running non-concurrently. + + """ + def __init__(self): + """ Create a dmesg instance """ + self.dmesg = [] + self._last_message = "" + self._new_messages = [] + + # Populate self.dmesg initially, otherwise the first test will always + # be full of dmesg crud. + self._call_dmesg() + + @synchronized_self + def update_dmesg(self): + """ Call dmesg and look for changes. """ + self._call_dmesg() + + return self._new_messages + + def _call_dmesg(self): + """ Call dmesg using subproces.check_output + + Get the contents of dmesg, then calculate new messages, finally set + self.dmesg equal to the just read contents of dmesg. + + """ + # Call strip to remove the trailing newline before splitting on the \n. + # posix systems *should* use \n for newlines + dmesg = subprocess.check_output( ['dmesg']).strip().split('\n') + + # Calculate new entires in dmesg by returning any strings that are in + # the new dmesg list but not in the old one. This circumvents the + # ringbuffer issue, since if there are not entries in common all of the + # new messages will be returned. + self._new_messages = [x for x in dmesg if x not in self.dmesg] + self.dmesg = dmesg + + +class DummyDmesg(object): + """ An dummy class for dmesg on non unix-like systems + + This implments only a very small subset of the UnixDmesg class, which + allows it to return None anywhere Dmesg is called. + + The usage of the dmesg class is universally to call ``if dmesg:'', so by + returning None the DummyDmesg class will always skip any dmesg handling + code. + + """ + + def __init__(self): + pass + + def update_dmesg(self): + return None + + +# Export the Dmesg class as either PosixDmesg (if on a posix system) or +# DummyDmesg if not on a posix system +if os.name == "posix": + Dmesg = PosixDmesg +else: + Dmesg = DummyDmesg -- 1.8.1.5 _______________________________________________ Piglit mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/piglit
