]> git.ipfire.org Git - oddments/fireinfo.git/blobdiff - fireinfo/cpu.py
Support ARM in fireinfo.
[oddments/fireinfo.git] / fireinfo / cpu.py
index 6b92b9396859d6548cd07165bfe99dc2afbe7e47..ed6317783d01de0046aa5e0a39df4a860e9e3b82 100644 (file)
@@ -21,7 +21,7 @@
 
 import os.path
 
-import _fireinfo
+import system
 
 PROC_CPUINFO = "/proc/cpuinfo"
 SYS_CLASS_CPUID = "/sys/class/cpuid/cpu%d"
@@ -40,13 +40,20 @@ class CPU(object):
                """
                        Initialize this class by reading all data from /proc/cpuinfo.
                """
-               self.read_cpuinfo()
+               self.__cpuinfo = self.read_cpuinfo()
 
-       def read_cpuinfo(self):
+       @property
+       def system(self):
+               return system.System()
+
+       @staticmethod
+       def read_cpuinfo():
                """
                        Read information from PROC_CPUINFO and store
-                       it into a dictionary self.__cpuinfo.
+                       it into a dictionary cpuinfo.
                """
+               cpuinfo = {}
+
                f = open(PROC_CPUINFO)
                while True:
                        line = f.readline()
@@ -63,65 +70,105 @@ class CPU(object):
                        key = key.strip().replace(" ", "_")
                        val = val.strip()
 
-                       self.__cpuinfo[key] = val
+                       cpuinfo[key] = val
 
                f.close()
 
+               return cpuinfo
+
        @property
        def bogomips(self):
                """
                        Return the bogomips of this CPU.
                """
-               return float(self.__cpuinfo["bogomips"])
+               try:
+                       bogomips = self.__cpuinfo["bogomips"]
+               except KeyError:
+                       bogomips = self.__cpuinfo["BogoMIPS"]
+
+               return float(bogomips)
 
        @property
        def model(self):
                """
                        Return the model id of this CPU.
                """
-               return int(self.__cpuinfo["model"])
+               try:
+                       model = int(self.__cpuinfo["model"])
+               except KeyError:
+                       model = None
+
+               return model
 
        @property
        def model_string(self):
                """
                        Return the model string of this CPU.
                """
-               return self.__cpuinfo["model_name"]
+               return self.__cpuinfo["Processor"]
 
        @property
        def vendor(self):
                """
                        Return the vendor string of this CPU.
                """
-               return self.__cpuinfo["vendor_id"]
+               try:
+                       vendor = self.__cpuinfo["vendor_id"]
+               except KeyError:
+                       if self.system.arch.startswith("arm"):
+                               vendor = "ARM"
+                       else:
+                               vendor = ""
+
+               return vendor
 
        @property
        def stepping(self):
                """
                        Return the stepping id of this CPU.
                """
-               return int(self.__cpuinfo["stepping"])
+               try:
+                       stepping = int(self.__cpuinfo["stepping"])
+               except KeyError:
+                       stepping = None
+
+               return stepping
 
        @property
        def flags(self):
                """
                        Return all flags of this CPU.
                """
-               return self.__cpuinfo["flags"].split()
+               try:
+                       flags = self.__cpuinfo["flags"]
+               except KeyError:
+                       flags = self.__cpuinfo["Features"]
+
+               return flags.split()
 
        @property
        def speed(self):
                """
                        Return the speed (in MHz) of this CPU.
                """
-               return float(self.__cpuinfo["cpu_MHz"])
+               try:
+                       speed = float(self.__cpuinfo["cpu_MHz"])
+               except KeyError:
+                       speed = 0
+
+               return speed
 
        @property
        def family(self):
                """
                        Return the family id of this CPU.
                """
-               return int(self.__cpuinfo["cpu_family"])
+               try:
+                       family = int(self.__cpuinfo["cpu_family"])
+               except KeyError:
+                       family = None
+
+               return family
        
        @property
        def count(self):
@@ -131,7 +178,8 @@ class CPU(object):
                i = 0
                while (os.path.exists(SYS_CLASS_CPUID % i)):
                        i += 1
-               return i
+
+               return i or 1
 
 
 if __name__ == "__main__":