summaryrefslogtreecommitdiffstats
path: root/rteval/dmi.py
blob: d317c3c8267bd3ab598d018f7fb73c0c4b676bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#
#   dmi.py - class to wrap DMI Table information
#
#   Copyright 2009,2010   Clark Williams <williams@redhat.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#   For the avoidance of doubt the "preferred form" of this code is one which
#   is in an open unpatent encumbered format. Where cryptographic key signing
#   forms part of the process of creating an executable the information
#   including keys needed to generate an equivalently functional executable
#   are deemed to be part of the source code.
#

import sys
import os
import subprocess
sys.pathconf = "."
import xmlout
import libxml2
import libxslt

try:
    import dmidecode
except:
    class dmidecode(object):
        fake = 1
        def __init__(self):
            pass
        

def ProcessWarnings():
    
    if hasattr(dmidecode, "fake") or not hasattr(dmidecode, 'get_warnings'):
        return

    warnings = dmidecode.get_warnings()
    if warnings == None:
        return

    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.':
            continue

        # All other warnings will be printed
        if len(warnline) > 0:
            print "** DMI WARNING ** %s" % warnline

    dmidecode.clear_warnings()


class DMIinfo(object):
    '''class used to obtain DMI info via python-dmidecode'''

    def __init__(self, config):
        self.version = '0.3'
        self.smbios = None
        self.sharedir = config.installdir

        if hasattr(dmidecode, "fake"):
            return

        self.dmixml = dmidecode.dmidecodeXML()
        self.smbios = dmidecode.dmi.replace('SMBIOS ', '').replace(' present', '')

        xsltdoc = self.__load_xslt('rteval_dmi.xsl')
        self.xsltparser = libxslt.parseStylesheetDoc(xsltdoc)


    def __load_xslt(self, fname):
        if os.path.exists(fname):
            return libxml2.parseFile(fname)
        elif os.path.exists(self.sharedir + '/' + fname):
            return libxml2.parseFile(self.sharedir + '/' + fname)
        else:
            raise RuntimeError, 'Could not locate XSLT template for DMI data (%s)' % fname

    def genxml(self, xml):
        if hasattr(dmidecode, "fake"):
            return
        self.dmixml.SetResultType(dmidecode.DMIXML_DOC)
        resdoc = self.xsltparser.applyStylesheet(self.dmixml.QuerySection('all'), None)
        node = resdoc.getRootElement().copyNode(1)
        node.newProp("DMIinfo_version", self.version)
        xml.AppendXMLnodes(node)


def unit_test(rootdir):
    from pprint import pprint

    class unittest_ConfigDummy(object):
        def __init__(self, rootdir):
            self.config = {'installdir': '%s/rteval'}
            self.__update_vars()

        def __update_vars(self):
            for k in self.config.keys():
                self.__dict__[k] = self.config[k]

    try:
        ProcessWarnings()
        if os.getuid() != 0:
            print "** ERROR **  Must be root to run this unit_test()"
            return 1

        cfg = unittest_ConfigDummy(rootdir)
        d = DMIinfo(cfg)
        x = xmlout.XMLOut('dmi_test', "0.0")
        x.NewReport()
        d.genxml(x)
        x.close()
        x.Write('-')
        return 0
    except Exception, e:
        print "** EXCEPTION: %s" % str(e)
        return 1

if __name__ == '__main__':
    sys.exit(unit_test('.'))