No functional changes.
# #
###############################################################################
-import os
+import os.path
import _fireinfo
SYS_CLASS_CPUID = "/sys/class/cpuid/cpu%d"
class CPU(object):
+ """
+ A class that represents the first CPU in a system.
+
+ We get all information form the first CPU (or core) and assume that
+ all other ones are equal.
+ """
+
__cpuinfo = {}
def __init__(self):
+ """
+ Initialize this class by reading all data from /proc/cpuinfo.
+ """
self.read_cpuinfo()
def read_cpuinfo(self):
@property
def bogomips(self):
+ """
+ Return the bogomips of this CPU.
+ """
return float(self.__cpuinfo["bogomips"])
@property
def model(self):
+ """
+ Return the model id of this CPU.
+ """
return int(self.__cpuinfo["model"])
@property
def model_string(self):
+ """
+ Return the model string of this CPU.
+ """
return self.__cpuinfo["model_name"]
@property
def vendor(self):
+ """
+ Return the vendor string of this CPU.
+ """
return self.__cpuinfo["vendor_id"]
@property
def stepping(self):
+ """
+ Return the stepping id of this CPU.
+ """
return int(self.__cpuinfo["stepping"])
@property
def flags(self):
+ """
+ Return all flags of this CPU.
+ """
return self.__cpuinfo["flags"].split()
@property
def speed(self):
+ """
+ Return the speed (in MHz) of this CPU.
+ """
return float(self.__cpuinfo["cpu_MHz"])
@property
def family(self):
+ """
+ Return the family id of this CPU.
+ """
return int(self.__cpuinfo["cpu_family"])
@property
# #
###############################################################################
-import os
-import string
-
+import os.path
class Device(object):
+ """
+ This is an abstract class that represents all devices in the system.
+ Every single device has its own instance of this class.
+ """
+
def __init__(self, path):
- self.path=path
- self._uevent={}
+ """
+ Collect all information about the device by reading the
+ "uevent" file and parsing it.
+ """
+
+ # Save the path in /sys to the device.
+ self.path = path
+
+ # Datastructure to store information we read.
+ self._uevent = {}
+
+ # Open the uevent file and parse all lines.
with open(os.path.join(path, "uevent")) as f:
for line in f.readlines():
key, val = line.split("=")
- self._uevent[key]=val.rstrip().lower()
-
+ self._uevent[key] = val.rstrip().lower()
+
@property
def driver(self):
+ """
+ Get the driver/kernel module that device is driven by or return None.
+ """
return self._uevent.get("DRIVER", None)
class PCIDevice(Device):
+ """
+ A class that represents all PCI (and PCIe) devices in a system.
+ """
+
subsystem = "pci"
-
+
@property
def model(self):
+ """
+ Return the PCI model id of this device.
+ """
return self._uevent['PCI_ID'].split(":")[1]
@property
def vendor(self):
+ """
+ Return the PCI vendor id of this device.
+ """
return self._uevent['PCI_ID'].split(":")[0]
@property
def deviceclass(self):
+ """
+ Return the PCI device class of this device.
+ """
return self._uevent['PCI_CLASS']
@property
def sub_vendor(self):
+ """
+ Return the PCI vendor sub id of this device.
+ """
return self._uevent["PCI_SUBSYS_ID"].split(":")[0]
@property
def sub_model(self):
+ """
+ Return the PCI model sub id of this device.
+ """
return self._uevent["PCI_SUBSYS_ID"].split(":")[1]
class USBDevice(Device):
+ """
+ A class that represents all USB devices in a system.
+ """
+
subsystem = "usb"
def pad(self, s):
+ """
+ A function to pad ids that have no leading zeroes.
+ """
while len(s) < 4:
s = "0"+s
return s
-
-
+
@property
def vendor(self):
+ """
+ Return the USB vendor id of this device.
+ """
return self.pad(self._uevent['PRODUCT'].split("/")[0])
@property
def model(self):
+ """
+ Return the USB model id of this device.
+ """
return self.pad(self._uevent['PRODUCT'].split("/")[1])
@property
def deviceclass(self):
+ """
+ Return the USB device class of this device.
+ """
return self._uevent.get("INTERFACE", None)
-
-
-
@property
def language(self):
+ """
+ Return the language code of IPFire or "unknown" if we cannot get it.
+ """
# Return "unknown" if settings file does not exist.
filename = "/var/ipfire/main/settings"
if not os.path.exists(filename):
with open(filename, "r") as f:
for line in f.readlines():
key, val = line.split("=", 1)
- if key=="LANGUAGE":
+ if key == "LANGUAGE":
return val.strip()
@property
def release(self):
+ """
+ Return the system release string.
+ """
return read_from_file("/etc/system-release")
@property
def bios_vendor(self):
+ """
+ Return the bios vendor name.
+ """
return read_from_file("/sys/class/dmi/id/bios_vendor")
@property
def vendor(self):
+ """
+ Return the vendor string of this system (if any).
+ """
ret = None
for file in ("sys_vendor", "board_vendor", "chassis_vendor",):
ret = read_from_file(os.path.join(SYS_CLASS_DMI, file))
@property
def model(self):
+ """
+ Return the model string of this system (if any).
+ """
ret = None
for file in ("product_name", "board_model", "chassis_model",):
ret = read_from_file(os.path.join(SYS_CLASS_DMI, file))
@property
def memory(self):
+ """
+ Return the amount of memory in kilobytes.
+ """
with open("/proc/meminfo", "r") as f:
firstline = f.readline().strip()
return int(firstline.split()[1])
@property
def kernel_release(self):
+ """
+ Return the kernel release string.
+ """
return os.uname()[2]
@property
def root_disk(self):
+ """
+ Return the dev node of the root disk.
+ """
with open("/etc/mtab", "r") as f:
- dev, mountpoint, rest = f.readline().split(" ",2)
+ dev, mountpoint, rest = f.readline().split(" ", 2)
if mountpoint == "/":
+ # Cut off /dev
dev = dev[5:]
- # cut off all digits at end of string
+ # Cut off all digits at end of string
while dev[-1] in string.digits:
- dev = dev[:-1]
- return dev
+ dev = dev[:-1]
+
+ return dev
@property
def root_size(self):
- path="/sys/block/%s/size" %self.root_disk
+ """
+ Return the size of the root disk in kilobytes.
+ """
+ path = "/sys/block/%s/size" % self.root_disk
if not os.path.exists(path):
return
+
with open(path, "r") as f:
- return int(f.readline())*512/1024
+ return int(f.readline()) * 512 / 1024
@property
def root_disk_serial(self):
+ """
+ Return the serial number of the root disk (if any).
+ """
serial = _fireinfo.get_harddisk_serial("/dev/%s" % self.root_disk)
if serial:
return serial.strip()
def scan(self):
- toscan = (("/sys/bus/pci/devices", device.PCIDevice),
- ("/sys/bus/usb/devices", device.USBDevice))
+ """
+ Scan for all devices (PCI/USB) in the system and append them
+ to our list.
+ """
+ self.devices = []
+
+ toscan = (
+ ("/sys/bus/pci/devices", device.PCIDevice),
+ ("/sys/bus/usb/devices", device.USBDevice)
+ )
for path, cls in toscan:
if not os.path.exists(path):
continue