1
# Copyright (c) 2010 Linaro
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
from subprocess import Popen, PIPE
19
from utils import read_file, get_machine_type
22
'vendor_id': 'cpu_vendor_name',
23
'cpu family': 'cpu_family',
25
'model name': 'cpu_model_name',
26
'stepping': 'cpu_stepping',
28
'flags': 'cpu_features',
39
'Processor': 'cpu_model_name',
40
'Features': 'cpu_features',
41
'CPU implementer': 'cpu_implementer',
42
'CPU architecture': 'cpu_architecture',
43
'CPU variant': 'cpu_variant',
44
'CPU part': 'cpu_part',
45
'CPU revision': 'cpu_revision',
49
'CPU implementer': lambda value: int(value, 16),
50
'CPU architecture': int,
51
'CPU variant': lambda value: int(value, 16),
52
'CPU part': lambda value: int(value, 16),
57
def _translate_cpuinfo(keymap, valmap, key, value):
59
Translate a key and value using keymap and valmap passed in
61
newkey = keymap.get(key, key)
62
newval = valmap.get(key, lambda x: x)(value)
67
Return a list of CPU devices
69
pattern = re.compile('^(?P<key>.+?)\s*:\s*(?P<value>.*)$')
74
machine = get_machine_type()
75
if machine in ('i686', 'x86_64'):
76
keymap, valmap = INTEL_KEYMAP, INTEL_VALMAP
77
elif machine.startswith('arm'):
78
keymap, valmap = ARM_KEYMAP, ARM_VALMAP
81
cpuinfo = read_file("/proc/cpuinfo")
82
for line in cpuinfo.splitlines():
83
match = pattern.match(line)
85
key, value = match.groups()
86
key, value = _translate_cpuinfo(keymap, valmap,
88
if cpudevs[cpunum].get(key):
91
cpudevs[cpunum][key] = value
92
for c in range(len(cpudevs)):
94
device['device_type'] = 'device.cpu'
95
device['description'] = 'Processor #{0}'.format(c)
96
device['attributes'] = cpudevs[c]
97
devices.append(device)
99
print >> sys.stderr, "WARNING: Could not read cpu information"
103
def get_board_devs():
105
Return a list of board devices
110
machine = get_machine_type()
111
if machine in ('i686', 'x86_64'):
113
description = read_file('/sys/class/dmi/id/board_name') or None
114
vendor = read_file('/sys/class/dmi/id/board_vendor') or None
115
version = read_file('/sys/class/dmi/id/board_version') or None
117
device['description'] = description.strip()
119
attributes['vendor'] = vendor.strip()
121
attributes['version'] = version.strip()
123
print >> sys.stderr, "WARNING: Could not read board information"
125
elif machine.startswith('arm'):
127
cpuinfo = read_file("/proc/cpuinfo")
130
pattern = re.compile("^Hardware\s*:\s*(?P<description>.+)$", re.M)
131
match = pattern.search(cpuinfo)
134
device['description'] = match.group('description')
136
print >> sys.stderr, "WARNING: Could not read board information"
141
device['attributes'] = attributes
142
device['device_type'] = 'device.board'
143
devices.append(device)
147
""" Return a list of memory devices
149
This returns up to two items, one for physical RAM and another for swap
151
pattern = re.compile('^(?P<key>.+?)\s*:\s*(?P<value>.+) kB$', re.M)
155
meminfo = read_file("/proc/meminfo")
156
for match in pattern.finditer(meminfo):
157
key, value = match.groups()
158
if key not in ('MemTotal', 'SwapTotal'):
160
capacity = int(value) << 10 #Kernel reports in 2^10 units
163
if key == 'MemTotal':
167
description = "{capacity}MiB of {kind}".format(
168
capacity=capacity >> 20, kind=kind)
170
device['description'] = description
171
device['attributes'] = {'capacity': str(capacity), 'kind': kind}
172
device['device_type'] = "device.mem"
173
devices.append(device)
175
print >> sys.stderr, "WARNING: Could not read memory information"
180
Return a list of usb devices
182
pattern = re.compile(
183
"^Bus \d{3} Device \d{3}: ID (?P<vendor_id>[0-9a-f]{4}):"
184
"(?P<product_id>[0-9a-f]{4}) (?P<description>.*)$")
187
for line in Popen('lsusb', stdout=PIPE).communicate()[0].splitlines():
188
match = pattern.match(line)
190
vendor_id, product_id, description = match.groups()
193
attributes['vendor_id'] = int(vendor_id, 16)
194
attributes['product_id'] = int(product_id, 16)
195
device['attributes'] = attributes
196
device['description'] = description
197
device['device_type'] = 'device.usb'
198
devices.append(device)
200
print >> sys.stderr, "WARNING: Could not read usb device information, \
201
unable to run lsusb, please install usbutils package"
204
def get_hardware_context():
206
Return a dict with all of the hardware profile information gathered
208
hardware_context = {}
210
devices.extend(get_cpu_devs())
211
devices.extend(get_board_devs())
212
devices.extend(get_mem_devs())
213
devices.extend(get_usb_devs())
214
hardware_context['devices'] = devices
215
return hardware_context