2
# This file is part of Checkbox.
4
# Copyright 2011 Canonical Ltd.
6
# Checkbox is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License version 3,
8
# as published by the Free Software Foundation.
11
# Checkbox is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
26
from checkbox.lib.dmi import (
32
HANDLE_RE = re.compile(
33
r"^Handle (?P<handle>0x[%s]{4}), "
34
r"DMI type (?P<type>\d+), "
35
r"(?P<size>\d+) bytes$"
37
KEY_VALUE_RE = re.compile(
38
r"^\t(?P<key>[%s].+):( (?P<value>.+))?$"
42
class DmidecodeParser:
43
"""Parser for the dmidecode command."""
47
"Manufacturer": "vendor",
48
"Product Name": "name",
49
"Serial Number": "serial",
54
"Form Factor": "form",
57
def __init__(self, stream):
60
def _parseKey(self, key):
61
return self._key_map.get(key)
63
def _parseValue(self, value):
71
def run(self, result):
72
output = self.stream.read()
73
for record in re.split(r"\n{2,}", output):
74
record = record.strip()
80
lines = record.split("\n")
82
if line.startswith("#"):
85
# Skip records with an unsupported handle
86
match = HANDLE_RE.match(line)
90
# Skip records that are empty or inactive
91
if not lines or lines.pop(0) == "Inactive":
94
# Skip disabled entries and end-of-table marker
95
type_index = int(match.group("type"))
96
if type_index >= len(Dmi.type_names):
99
category = Dmi.type_names[type_index]
100
category = category.upper().split(" ")[-1]
102
"BOARD", "BIOS", "CHASSIS", "DEVICE", "PROCESSOR", "SYSTEM"):
109
# Skip lines with an unsupported key/value pair
110
match = KEY_VALUE_RE.match(line)
114
# Skip lines with an unsupported key
115
key = self._parseKey(match.group("key"))
119
key = "%s_%s" % (category.lower(), key)
120
value = self._parseValue(match.group("value"))
121
attributes[key] = value
123
device = DmiDevice(attributes, category)
124
result.addDmiDevice(device)