windowsSystemInfo.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import sys
  2. import os
  3. import ctypes
  4. import _winreg
  5. """Class to extract system information from a MS-Windows Box:
  6. Instructions for Using:
  7. Instance the class WindowsSystemInformation
  8. Then in the instance, access the following variables:
  9. os
  10. cpu
  11. totalRAM
  12. totalVM
  13. availableVM
  14. availableRAM
  15. Example:
  16. s = SystemInformation()
  17. print s.os
  18. s.refresh() # if you need to refresh the dynamic data (i.e. Memory stats, etc)
  19. """
  20. def get_registry_value(key, subkey, value):
  21. if sys.platform != 'win32':
  22. raise OSError("get_registry_value is only supported on Windows")
  23. key = getattr(_winreg, key)
  24. handle = _winreg.OpenKey(key, subkey)
  25. (value, type) = _winreg.QueryValueEx(handle, value)
  26. return value
  27. c_ulong = ctypes.c_ulong
  28. class MEMORYSTATUS(ctypes.Structure):
  29. _fields_ = [
  30. ('dwLength', c_ulong),
  31. ('dwMemoryLoad', c_ulong),
  32. ('dwTotalPhys', c_ulong),
  33. ('dwAvailPhys', c_ulong),
  34. ('dwTotalPageFile', c_ulong),
  35. ('dwAvailPageFile', c_ulong),
  36. ('dwTotalVirtual', c_ulong),
  37. ('dwAvailVirtual', c_ulong)
  38. ]
  39. class SystemInformation:
  40. def __init__(self):
  41. # Just in in case somebody called this class by accident, we should
  42. # check to make sure the OS is MS-Windows before continuing.
  43. assert sys.platform == 'win32', "Not an MS-Windows Computer. This class should not be called"
  44. # os contains the Operating System Name with Service Pack and Build
  45. # Example: Microsoft Windows XP Service Pack 2 (build 2600)
  46. self.os = self._os_version().strip()
  47. # cpu contains the CPU model and speed
  48. # Example: Intel Core(TM)2 CPU 6700 @ 2.66GHz
  49. self.cpu = self._cpu().strip()
  50. self.totalRAM, self.availableRAM, self.totalPF, self.availablePF, self.memoryLoad, self.totalVM, self.availableVM = self._ram()
  51. # totalRam contains the total amount of RAM in the system
  52. self.totalRAM = self.totalRAM / 1024
  53. # totalVM contains the total amount of VM available to the system
  54. self.totalVM = self.totalVM / 1024
  55. # availableVM contains the amount of VM that is free
  56. self.availableVM = self.availableVM / 1024
  57. # availableRam: Ammount of available RAM in the system
  58. self.availableRAM = self.availableRAM / 1024
  59. def refresh(self):
  60. self.totalRAM, self.availableRAM, self.totalPF, self.availablePF, self.memoryLoad, self.totalVM, self.availableVM = self._ram()
  61. self.totalRAM = self.totalRAM / 1024
  62. self.totalVM = self.totalVM / 1024
  63. self.availableVM = self.availableVM / 1024
  64. self.availableRAM = self.availableRAM / 1024
  65. def _os_version(self):
  66. def get(key):
  67. return get_registry_value(
  68. "HKEY_LOCAL_MACHINE",
  69. "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
  70. key)
  71. os = get("ProductName")
  72. sp = get("CSDVersion")
  73. build = get("CurrentBuildNumber")
  74. return "%s %s (build %s)" % (os, sp, build)
  75. def _cpu(self):
  76. return get_registry_value(
  77. "HKEY_LOCAL_MACHINE",
  78. "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  79. "ProcessorNameString")
  80. def _ram(self):
  81. kernel32 = ctypes.windll.kernel32
  82. memoryStatus = MEMORYSTATUS()
  83. memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
  84. kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))
  85. return (memoryStatus.dwTotalPhys, memoryStatus.dwAvailPhys, memoryStatus.dwTotalPageFile, memoryStatus.dwAvailPageFile, memoryStatus.dwMemoryLoad, memoryStatus.dwTotalVirtual, memoryStatus.dwAvailVirtual)
  86. # To test, execute the script standalone.
  87. if __name__ == "__main__":
  88. s = SystemInformation()
  89. print s.os
  90. print s.cpu
  91. print "RAM : %dKb total" % s.totalRAM
  92. print "RAM : %dKb free" % s.availableRAM
  93. print "Total VM: %dKb" % s.totalVM
  94. print "Available VM: %dKb" % s.availableVM