From 98e162889cd8b180ae2e42cebd2723c26d9f16a1 Mon Sep 17 00:00:00 2001 From: jchzhou Date: Sat, 14 Sep 2024 18:23:06 +0800 Subject: [PATCH] add fallback default values when detecting hardware info with dmidecode and cpuinfo Signed-off-by: jchzhou --- hwcompatible/device.py | 5 ++++- hwcompatible/document.py | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/hwcompatible/device.py b/hwcompatible/device.py index d854d36..0322640 100755 --- a/hwcompatible/device.py +++ b/hwcompatible/device.py @@ -433,7 +433,10 @@ class Device: def get_cpu_vendor(self): cmd_result = self.command.run_cmd( "cat /proc/cpuinfo | grep vendor_id | head -1", log_print=False) - cpu_vendor_id = cmd_result[0].split(':')[1].strip() + if cmd_result[0] == "": + cpu_vendor_id = "Unknown Vendor" + else: + cpu_vendor_id = cmd_result[0].split(':')[1].strip() return cpu_vendor_id def _is_null(self): diff --git a/hwcompatible/document.py b/hwcompatible/document.py index 49d9874..85549d7 100755 --- a/hwcompatible/document.py +++ b/hwcompatible/document.py @@ -75,14 +75,25 @@ class CertDocument(Document): """ try: cmd_result = getoutput("/usr/sbin/dmidecode -t 1") + defaults = { + "Manufacturer": "Unknown Vendor", + "Product Name": "Unknown Product", + "Version": "Unknown Version" + } for line in cmd_result.split("\n"): property_right = line.split(":", 1) if len(property_right) != 2: continue key = property_right[0].strip() value = property_right[1].strip() - if key in ("Manufacturer", "Product Name", "Version"): - self.document[key] = value + if key in defaults: + self.document[key] = value or defaults[key] + else: + self.logger.warning(f"Unknown key '{key}' encountered with \ + value '{value}' when parsing hardware info with dmidecode") + for key, default_value in defaults.items(): + if key not in self.document: + self.document[key] = default_value except Exception as concrete_error: self.logger.error( "Get hardware information failed. %s" % concrete_error) -- Gitee