~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to checkbox/parsers/cpuinfo.py

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-17 13:54:25 UTC
  • mto: This revision was merged to the branch mainline in revision 2130.
  • Revision ID: zygmunt.krynicki@canonical.com-20130517135425-cxcenxx5t0qrtbxd
checkbox-ng: add CheckBoxNG sub-project

CheckBoxNG (or lowercase as checkbox-ng, pypi:checkbox-ng) is a clean
implementation of CheckBox on top of PlainBox. It provides a new
executable, 'checkbox' that has some of the same commands that were
previously implemented in the plainbox package.

In particular CheckBoxNG comes with the 'checkbox sru' command
(the same one as in plainbox). Later on this sub-command will be removed
from plainbox.

CheckBoxNG depends on plainbox >= 0.3

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of Checkbox.
 
3
#
 
4
# Copyright 2011 Canonical Ltd.
 
5
#
 
6
# Checkbox is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation, either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
#
 
19
import re
 
20
 
 
21
from os import uname
 
22
 
 
23
from checkbox.lib.conversion import string_to_type
 
24
 
 
25
 
 
26
class CpuinfoParser:
 
27
    """Parser for the /proc/cpuinfo file."""
 
28
 
 
29
    def __init__(self, stream, machine=None):
 
30
        self.stream = stream
 
31
        self.machine = machine or uname()[4].lower()
 
32
 
 
33
    def getAttributes(self):
 
34
        count = 0
 
35
        attributes = {}
 
36
        cpuinfo = self.stream.read()
 
37
        for block in re.split(r"\n{2,}", cpuinfo):
 
38
            block = block.strip()
 
39
            if not block:
 
40
                continue
 
41
 
 
42
            count += 1
 
43
            if count > 1:
 
44
                continue
 
45
 
 
46
            for line in block.split("\n"):
 
47
                if not line:
 
48
                    continue
 
49
                key, value = line.split(":", 1)
 
50
                key, value = key.strip(), value.strip()
 
51
 
 
52
                # Handle bogomips on sparc
 
53
                if key.endswith("Bogo"):
 
54
                    key = "bogomips"
 
55
 
 
56
                attributes[key.lower()] = value
 
57
 
 
58
        if attributes:
 
59
            attributes["count"] = count
 
60
 
 
61
        return attributes
 
62
 
 
63
    def run(self, result):
 
64
        attributes = self.getAttributes()
 
65
        if not attributes:
 
66
            return
 
67
 
 
68
        # Default values
 
69
        machine = self.machine
 
70
        processor = {
 
71
            "platform": machine,
 
72
            "count": 1,
 
73
            "type": machine,
 
74
            "model": machine,
 
75
            "model_number": "",
 
76
            "model_version": "",
 
77
            "model_revision": "",
 
78
            "cache": 0,
 
79
            "bogomips": 0,
 
80
            "speed": -1,
 
81
            "other": ""}
 
82
 
 
83
        # Conversion table
 
84
        platform_to_conversion = {
 
85
            ("i386", "i486", "i586", "i686", "x86_64",): {
 
86
                "type": "vendor_id",
 
87
                "model": "model name",
 
88
                "model_number": "cpu family",
 
89
                "model_version": "model",
 
90
                "model_revision": "stepping",
 
91
                "cache": "cache size",
 
92
                "other": "flags",
 
93
                "speed": "cpu mhz"},
 
94
            ("alpha", "alphaev6",): {
 
95
                "count": "cpus detected",
 
96
                "type": "cpu",
 
97
                "model": "cpu model",
 
98
                "model_number": "cpu variation",
 
99
                "model_version": ("system type", "system variation",),
 
100
                "model_revision": "cpu revision",
 
101
                "other": "platform string",
 
102
                "speed": "cycle frequency [Hz]"},
 
103
            ("armv7l",): {
 
104
                "type": "hardware",
 
105
                "model": "processor",
 
106
                "model_number": "cpu variant",
 
107
                "model_version": "cpu architecture",
 
108
                "model_revision": "cpu revision",
 
109
                "other": "features"},
 
110
            ("ia64",): {
 
111
                "type": "vendor",
 
112
                "model": "family",
 
113
                "model_version": "archrev",
 
114
                "model_revision": "revision",
 
115
                "other": "features",
 
116
                "speed": "cpu mhz"},
 
117
            ("ppc64", "ppc",): {
 
118
                "type": "platform",
 
119
                "model": "cpu",
 
120
                "model_version": "revision",
 
121
                "speed": "clock"},
 
122
            ("sparc64", "sparc",): {
 
123
                "count": "ncpus probed",
 
124
                "type": "type",
 
125
                "model": "cpu",
 
126
                "model_version": "type",
 
127
                "speed": "bogomips"}}
 
128
 
 
129
        processor["count"] = attributes.get("count", 1)
 
130
        bogompips_string = attributes.get("bogomips", "0.0")
 
131
        processor["bogomips"] = int(round(float(bogompips_string)))
 
132
        for platform, conversion in platform_to_conversion.items():
 
133
            if machine in platform:
 
134
                for pkey, ckey in conversion.items():
 
135
                    if isinstance(ckey, (list, tuple)):
 
136
                        processor[pkey] = "/".join([attributes[k]
 
137
                                                    for k in ckey])
 
138
                    elif ckey in attributes:
 
139
                        processor[pkey] = attributes[ckey]
 
140
 
 
141
        # Adjust platform
 
142
        if machine[0] == "i" and machine[-2:] == "86":
 
143
            processor["platform"] = "i386"
 
144
        elif machine[:5] == "alpha":
 
145
            processor["platform"] = "alpha"
 
146
 
 
147
        # Adjust cache
 
148
        if processor["cache"]:
 
149
            processor["cache"] = string_to_type(processor["cache"])
 
150
 
 
151
        # Adjust speed
 
152
        try:
 
153
            if machine[:5] == "alpha":
 
154
                speed = processor["speed"].split()[0]
 
155
                processor["speed"] = int(round(float(speed))) / 1000000
 
156
            elif machine[:5] == "sparc":
 
157
                speed = processor["speed"]
 
158
                processor["speed"] = int(round(float(speed))) / 2
 
159
            else:
 
160
                if machine[:3] == "ppc":
 
161
                    # String is appended with "mhz"
 
162
                    speed = processor["speed"][:-3]
 
163
                else:
 
164
                    speed = processor["speed"]
 
165
                processor["speed"] = int(round(float(speed)) - 1)
 
166
        except ValueError:
 
167
            processor["speed"] = -1
 
168
 
 
169
        # Adjust count
 
170
        try:
 
171
            processor["count"] = int(processor["count"])
 
172
        except ValueError:
 
173
            processor["count"] = 1
 
174
        else:
 
175
            # There is at least one processor
 
176
            if processor["count"] == 0:
 
177
                processor["count"] = 1
 
178
 
 
179
        result.setProcessor(processor)