diff mbox series

rteval: Fix "DMI WARNING" when not running as root

Message ID 20220823190329.61326-1-jkacur@redhat.com
State New
Headers show
Series rteval: Fix "DMI WARNING" when not running as root | expand

Commit Message

John Kacur Aug. 23, 2022, 7:03 p.m. UTC
In some cases it is not necessary to run as root, for example when
running -Z (--summarize) to summarize an existing report.

In such cases we do not want to see the message:
** DMI WARNING ** Failed to open memory buffer (/dev/mem): Permission denied

The fix here surpresses that message.

In addition:
- the unused "config" option to DMIinfo.__init__ is removed
- A few strings are converted to f-strings
- "with" is used to open the xsltfile

Signed-off-by: John Kacur <jkacur@redhat.com>
---
 rteval/sysinfo/__init__.py |  6 ++---
 rteval/sysinfo/dmi.py      | 45 +++++++++++++++++++-------------------
 2 files changed, 26 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/rteval/sysinfo/__init__.py b/rteval/sysinfo/__init__.py
index 0436ebb350d9..a4359382f006 100644
--- a/rteval/sysinfo/__init__.py
+++ b/rteval/sysinfo/__init__.py
@@ -42,7 +42,7 @@  class SystemInfo(KernelInfo, SystemServices, dmi.DMIinfo, CPUtopology,
         self.__logger = logger
         KernelInfo.__init__(self, logger=logger)
         SystemServices.__init__(self, logger=logger)
-        dmi.DMIinfo.__init__(self, config, logger=logger)
+        dmi.DMIinfo.__init__(self, logger=logger)
         CPUtopology.__init__(self)
         OSInfo.__init__(self, logger=logger)
         cmdlineInfo.__init__(self, logger=logger)
@@ -80,8 +80,8 @@  if __name__ == "__main__":
     cfg.installdir = "."
     si = SystemInfo(cfg, logger=l)
 
-    print("\tRunning on %s" % si.get_base_os())
-    print("\tNUMA nodes: %d" % si.mem_get_numa_nodes())
+    print(f"\tRunning on {si.get_base_os()}")
+    print(f"\tNUMA nodes: {si.mem_get_numa_nodes()}")
     print("\tMemory available: %03.2f %s\n" % si.mem_get_size())
 
     print("\tServices: ")
diff --git a/rteval/sysinfo/dmi.py b/rteval/sysinfo/dmi.py
index 80cf3c723b36..5965c128c093 100644
--- a/rteval/sysinfo/dmi.py
+++ b/rteval/sysinfo/dmi.py
@@ -1,6 +1,4 @@ 
 #
-#   dmi.py - class to wrap DMI Table information
-#
 #   Copyright 2009 - 2013   Clark Williams <williams@redhat.com>
 #   Copyright 2009 - 2013   David Sommerseth <davids@redhat.com>
 #
@@ -24,6 +22,7 @@ 
 #   including keys needed to generate an equivalently functional executable
 #   are deemed to be part of the source code.
 #
+""" dmi.py class to wrap DMI Table Information """
 
 import sys
 import os
@@ -52,16 +51,18 @@  def ProcessWarnings():
     if warnings is None:
         return
 
+    ignore1  = '/dev/mem: Permission denied'
+    ignore2 = 'No SMBIOS nor DMI entry point found, sorry.'
+    ignore3 = 'Failed to open memory buffer (/dev/mem): Permission denied'
+    ignore = (ignore1, ignore2, ignore3)
     for warnline in warnings.split('\n'):
         # Ignore these warnings, as they are "valid" if not running as root
-        if warnline == '/dev/mem: Permission denied':
-            continue
-        if warnline == 'No SMBIOS nor DMI entry point found, sorry.':
+        if warnline in ignore:
             continue
 
         # All other warnings will be printed
         if len(warnline) > 0:
-            print("** DMI WARNING ** %s" % warnline)
+            print(f"** DMI WARNING ** {warnline}")
 
     dmidecode.clear_warnings()
 
@@ -69,8 +70,7 @@  def ProcessWarnings():
 class DMIinfo:
     '''class used to obtain DMI info via python-dmidecode'''
 
-    # TODO: Remove unnecessary config
-    def __init__(self, config, logger):
+    def __init__(self, logger):
         self.__version = '0.5'
 
         if not dmidecode_loaded:
@@ -83,22 +83,24 @@  class DMIinfo:
 
         self.__xsltparser = self.__load_xslt('rteval_dmi.xsl')
 
-    def __load_xslt(self, fname):
-        xsltfile = None
+    @staticmethod
+    def __load_xslt(fname):
+        xsltf = None
         if os.path.exists(fname):
-            xsltfile = open(fname, "r")
-        elif rtevalConfig.default_config_search([fname], os.path.isfile):
-            xsltfile = open(rtevalConfig.default_config_search([fname], os.path.isfile), "r")
-
-        if xsltfile:
-            xsltdoc = lxml.etree.parse(xsltfile)
-            ret = lxml.etree.XSLT(xsltdoc)
-            xsltfile.close()
+            xsltf = fname
+        else:
+            xsltf = rtevalConfig.default_config_search([fname], os.path.isfile)
+
+        if xsltf:
+            with open(xsltf, "r") as xsltfile:
+                xsltdoc = lxml.etree.parse(xsltfile)
+                ret = lxml.etree.XSLT(xsltdoc)
             return ret
 
         raise RuntimeError(f'Could not locate XSLT template for DMI data ({fname})')
 
     def MakeReport(self):
+        """ Add DMI information to final report """
         rep_n = libxml2.newNode("DMIinfo")
         rep_n.newProp("version", self.__version)
         if self.__fake:
@@ -113,7 +115,7 @@  class DMIinfo:
         return rep_n
 
 def unit_test(rootdir):
-    """ unit_test for dmi.py, looks a little crufty! """
+    """ unit_test for dmi.py """
 
     class UnittestConfigDummy:
         def __init__(self, rootdir):
@@ -132,15 +134,14 @@  def unit_test(rootdir):
 
         log = Log()
         log.SetLogVerbosity(Log.DEBUG|Log.INFO)
-        cfg = UnittestConfigDummy(rootdir)
-        d = DMIinfo(cfg, log)
+        d = DMIinfo(log)
         dx = d.MakeReport()
         x = libxml2.newDoc("1.0")
         x.setRootElement(dx)
         x.saveFormatFileEnc("-", "UTF-8", 1)
         return 0
     except Exception as e:
-        print("** EXCEPTION: %s" % str(e))
+        print(f"** EXCEPTION: {str(e)}")
         return 1
 
 if __name__ == '__main__':