mirror of https://gitlab.com/baco/winsysinfo.git
148 lines
5.3 KiB
Python
148 lines
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# SystemInformation.py
|
|
#
|
|
# "THE BEER-WARE LICENSE" (Revision 42):
|
|
# Dionisio E Alonso <dealonso@gmail.com> wrote this file. As long as you
|
|
# retain this notice you can do whatever you want with this stuff. If we
|
|
# meet some day, and you think this stuff is worth it, you can buy me a
|
|
# beer in return
|
|
#
|
|
# with by Baco
|
|
#
|
|
|
|
import winreg
|
|
import ctypes
|
|
import string
|
|
|
|
|
|
def get_registry_value(key, subkey, value):
|
|
key = getattr(winreg, key)
|
|
handle = winreg.OpenKey(key, subkey)
|
|
(value, type) = winreg.QueryValueEx(handle, value)
|
|
return value
|
|
|
|
|
|
class SystemInformation:
|
|
def __init__(self):
|
|
self.cpu = 'CPU:\t{}'.format(self._cpu().strip())
|
|
self.graphics = 'Video:\t{}'.format(self._graphics())
|
|
self.totalRam, self.availableRam = self._ram()
|
|
self.ram = 'RAM:\t{:.2f}GB total'.format(self.totalRam/(1024**3))
|
|
hdd = []
|
|
for part in self._disk_c():
|
|
hdd.append('{} {:.2f}GB free'.format(part[0], part[1]/(1024**3)))
|
|
self.hdFree = 'HDD:\t{}'.format(', '.join(hdd))
|
|
self.os = 'OS:\t{}'.format(self._os_version())
|
|
|
|
def _cpu(self):
|
|
return get_registry_value(
|
|
'HKEY_LOCAL_MACHINE',
|
|
'HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0',
|
|
'ProcessorNameString')
|
|
|
|
def _graphics(self):
|
|
reg_keys = set()
|
|
for device in range(16):
|
|
try:
|
|
video_card_string = get_registry_value(
|
|
'HKEY_LOCAL_MACHINE',
|
|
'HARDWARE\\DEVICEMAP\\VIDEO',
|
|
'\Device\Video{}'.format(device))
|
|
reg_key = video_card_string.split('\\')[-2]
|
|
# Probe keys to avoid software devices
|
|
service = get_registry_value(
|
|
'HKEY_LOCAL_MACHINE',
|
|
'SYSTEM\\CurrentControlSet\\Control\\Video\\'
|
|
'{}\\Video'.format(reg_key),
|
|
'Service')
|
|
if service == 'vga':
|
|
reg_keys.add(reg_key)
|
|
except:
|
|
pass
|
|
|
|
video_cards = []
|
|
for video_card in reg_keys:
|
|
try:
|
|
device_descrption = get_registry_value(
|
|
'HKEY_LOCAL_MACHINE',
|
|
'SYSTEM\\CurrentControlSet\\Control\\Video\\'
|
|
'{}\\0000'.format(video_card),
|
|
'Device Description')
|
|
except:
|
|
try:
|
|
device_descrption = get_registry_value(
|
|
'HKEY_LOCAL_MACHINE',
|
|
'SYSTEM\\CurrentControlSet\\Control\\Video\\'
|
|
'{}\\0000'.format(video_card),
|
|
'DriverDesc')
|
|
except:
|
|
device_descrption = 'Failed to obtain video adapter.'
|
|
video_cards.append(device_descrption)
|
|
return '\n\t'.join(video_cards)
|
|
|
|
def _ram(self):
|
|
kernel32 = ctypes.windll.kernel32
|
|
c_ulong = ctypes.c_ulong
|
|
|
|
class MemoryStatus(ctypes.Structure):
|
|
_fields_ = [('dwLength', c_ulong),
|
|
('dwMemoryLoad', c_ulong),
|
|
('dwTotalPhys', c_ulong),
|
|
('dwAvailPhys', c_ulong),
|
|
('dwTotalPageFile', c_ulong),
|
|
('dwAvailPageFile', c_ulong),
|
|
('dwTotalVirtual', c_ulong),
|
|
('dwAvailVirtual', c_ulong)]
|
|
|
|
mem_status = MemoryStatus()
|
|
mem_status.dwLength = ctypes.sizeof(MemoryStatus)
|
|
kernel32.GlobalMemoryStatus(ctypes.byref(mem_status))
|
|
return (mem_status.dwTotalPhys, mem_status.dwAvailPhys)
|
|
|
|
def _disk_c(self):
|
|
drives = [c + ':' for c in list(string.ascii_uppercase)]
|
|
freeuser = ctypes.c_int64()
|
|
total = ctypes.c_int64()
|
|
free = ctypes.c_int64()
|
|
free_values = []
|
|
for drive in drives:
|
|
ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive,
|
|
ctypes.byref(freeuser),
|
|
ctypes.byref(total),
|
|
ctypes.byref(free))
|
|
if freeuser.value != 0:
|
|
free_values.append((drive, freeuser.value))
|
|
freeuser.value = 0
|
|
return free_values
|
|
|
|
def _os_version(self):
|
|
def get(key):
|
|
return get_registry_value(
|
|
'HKEY_LOCAL_MACHINE',
|
|
'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion', key)
|
|
os = get('ProductName')
|
|
try:
|
|
sp = get('CSDVersion')
|
|
except:
|
|
try:
|
|
sp = 'n' + get('CSDBuildNumber')
|
|
except:
|
|
sp = '(no service pack found)'
|
|
build = get('CurrentBuildNumber')
|
|
return '{} {} (build {})'.format(os, sp, build)
|
|
|
|
def save(self):
|
|
with open('system_information.txt', 'w') as f:
|
|
f.write('System Information\n'
|
|
'==================\n\n')
|
|
f.write(self.cpu + '\n')
|
|
f.write(self.graphics + '\n')
|
|
f.write(self.ram + '\n')
|
|
f.write(self.hdFree + '\n')
|
|
f.write(self.os + '\n')
|
|
|
|
if __name__ == '__main__':
|
|
sys = SystemInformation()
|
|
sys.save()
|