~launchpad-results/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpresults/xunit/parsers/dmidecode.py

  • Committer: Marc Tardif
  • Date: 2011-09-28 15:18:09 UTC
  • Revision ID: marc.tardif@canonical.com-20110928151809-rwn04clcebacbnfm
Added processor states for system units.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
    "DmidecodeParser",
8
8
    ]
9
9
 
10
 
from re import compile
 
10
import re
 
11
 
11
12
from string import (
12
13
    hexdigits,
13
14
    uppercase,
19
20
    )
20
21
 
21
22
 
22
 
HANDLE_RE = compile(
 
23
HANDLE_RE = re.compile(
23
24
    r"^Handle (?P<handle>0x[%s]{4}), "
24
 
    "DMI type (?P<type>\d+), "
25
 
    "(?P<size>\d+) bytes$"
 
25
    r"DMI type (?P<type>\d+), "
 
26
    r"(?P<size>\d+) bytes$"
26
27
    % hexdigits)
27
 
KEY_VALUE_RE = compile("^\t(?P<key>[%s].+):( (?P<value>.+))?$"
 
28
KEY_VALUE_RE = re.compile(
 
29
    r"^\t(?P<key>[%s].+):( (?P<value>.+))?$"
28
30
    % uppercase)
29
31
 
30
32
 
31
33
class DmidecodeParser:
 
34
    """Parser for the dmidecode command."""
32
35
 
33
 
    _attribute_map = {
 
36
    _key_whitelist = {
 
37
        "ID": "serial",
34
38
        "Manufacturer": "vendor",
35
39
        "Product Name": "name",
36
40
        "Serial Number": "serial",
38
42
        "Vendor": "vendor",
39
43
        "Version": "version",
40
44
        }
 
45
    _value_blacklist = (
 
46
        "n/a",
 
47
        "oem",
 
48
        "product name",
 
49
        "system manufacturer",
 
50
        "system name",
 
51
        "system product name",
 
52
        "system version",
 
53
        "to be filled by o.e.m.",
 
54
        "to be filled by o.e.m. by more string",
 
55
        "unknow",  # XXX This is correct mispelling
 
56
        )
41
57
 
42
58
    def __init__(self, stream):
43
59
        self.stream = stream
44
60
 
 
61
    def _parseKey(self, key):
 
62
        return self._key_whitelist.get(key)
 
63
 
 
64
    def _parseValue(self, value):
 
65
        if value is not None:
 
66
            value = value.strip()
 
67
            if not value or value.lower() in self._value_blacklist:
 
68
                value = None
 
69
 
 
70
        return value
 
71
 
45
72
    def run(self, result):
46
73
        output = self.stream.read()
47
 
        for record in output.split("\n\n"):
 
74
        for record in re.split(r"\n{2,}", output):
 
75
            record = record.strip()
48
76
            # Skip empty records
49
77
            if not record:
50
78
                continue
71
99
 
72
100
            category = Dmi.type_names[type_index]
73
101
            category = category.upper().split(" ")[-1]
74
 
            if category not in ("BOARD", "BIOS", "CHASSIS", "SYSTEM"):
 
102
            if category not in (
 
103
                "BOARD", "BIOS", "CHASSIS", "PROCESSOR", "SYSTEM"):
75
104
                continue
76
105
 
77
106
            # Parse attributes
78
107
            attributes = {}
79
108
            for line in lines:
 
109
                # Skip lines with an unsupported key/value pair
80
110
                match = KEY_VALUE_RE.match(line)
81
 
                if match:
82
 
                    key = match.group("key")
83
 
                    value = match.group("value")
84
 
                    if key in self._attribute_map:
85
 
                        key = "%s_%s" % (
86
 
                            category.lower(), self._attribute_map[key])
87
 
                        attributes[key] = value
 
111
                if not match:
 
112
                    continue
 
113
 
 
114
                # Skip lines with an unsupported key
 
115
                key = self._parseKey(match.group("key"))
 
116
                if not key:
 
117
                    continue
 
118
 
 
119
                key = "%s_%s" % (category.lower(), key)
 
120
                value = self._parseValue(match.group("value"))
 
121
                attributes[key] = value
88
122
 
89
123
            device = DmiDevice(attributes, category)
90
124
            result.addDmiDevice(device)