1 | import sys |
---|
2 | import os |
---|
3 | import ctypes |
---|
4 | import _winreg |
---|
5 | |
---|
6 | def get_registry_value(key, subkey, value): |
---|
7 | if sys.platform != 'win32': |
---|
8 | raise OSError("get_registry_value is only supported on Windows") |
---|
9 | |
---|
10 | import _winreg |
---|
11 | key = getattr(_winreg, key) |
---|
12 | handle = _winreg.OpenKey(key, subkey) |
---|
13 | (value, type) = _winreg.QueryValueEx(handle, value) |
---|
14 | return value |
---|
15 | |
---|
16 | class SystemInformation: |
---|
17 | def __init__(self): |
---|
18 | self.os = self._os_version().strip() |
---|
19 | self.cpu = self._cpu().strip() |
---|
20 | self.browsers = self._browsers() |
---|
21 | self.totalRam, self.availableRam = self._ram() |
---|
22 | self.totalRam = self.totalRam / (1024*1024) |
---|
23 | self.availableRam = self.availableRam / (1024*1024) |
---|
24 | self.hdFree = self._disk_c() / (1024*1024*1024) |
---|
25 | |
---|
26 | def _os_version(self): |
---|
27 | def get(key): |
---|
28 | return get_registry_value( |
---|
29 | "HKEY_LOCAL_MACHINE", |
---|
30 | "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", |
---|
31 | key) |
---|
32 | os = get("ProductName") |
---|
33 | sp = get("CSDVersion") |
---|
34 | build = get("CurrentBuildNumber") |
---|
35 | return "%s %s (build %s)" % (os, sp, build) |
---|
36 | |
---|
37 | def _cpu(self): |
---|
38 | return get_registry_value( |
---|
39 | "HKEY_LOCAL_MACHINE", |
---|
40 | "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", |
---|
41 | "ProcessorNameString") |
---|
42 | |
---|
43 | def _firefox_version(self): |
---|
44 | try: |
---|
45 | version = get_registry_value( |
---|
46 | "HKEY_LOCAL_MACHINE", |
---|
47 | "SOFTWARE\\Mozilla\\Mozilla Firefox", |
---|
48 | "CurrentVersion") |
---|
49 | version = (u"Mozilla Firefox", version) |
---|
50 | except WindowsError: |
---|
51 | version = None |
---|
52 | return version |
---|
53 | |
---|
54 | def _iexplore_version(self): |
---|
55 | try: |
---|
56 | version = get_registry_value( |
---|
57 | "HKEY_LOCAL_MACHINE", |
---|
58 | "SOFTWARE\\Microsoft\\Internet Explorer", |
---|
59 | "Version") |
---|
60 | version = (u"Internet Explorer", version) |
---|
61 | except WindowsError: |
---|
62 | version = None |
---|
63 | return version |
---|
64 | |
---|
65 | def _browsers(self): |
---|
66 | browsers = [] |
---|
67 | firefox = self._firefox_version() |
---|
68 | if firefox: |
---|
69 | browsers.append(firefox) |
---|
70 | iexplore = self._iexplore_version() |
---|
71 | if iexplore: |
---|
72 | browsers.append(iexplore) |
---|
73 | |
---|
74 | return browsers |
---|
75 | |
---|
76 | def _ram(self): |
---|
77 | kernel32 = ctypes.windll.kernel32 |
---|
78 | c_ulong = ctypes.c_ulong |
---|
79 | class MEMORYSTATUS(ctypes.Structure): |
---|
80 | _fields_ = [ |
---|
81 | ('dwLength', c_ulong), |
---|
82 | ('dwMemoryLoad', c_ulong), |
---|
83 | ('dwTotalPhys', c_ulong), |
---|
84 | ('dwAvailPhys', c_ulong), |
---|
85 | ('dwTotalPageFile', c_ulong), |
---|
86 | ('dwAvailPageFile', c_ulong), |
---|
87 | ('dwTotalVirtual', c_ulong), |
---|
88 | ('dwAvailVirtual', c_ulong) |
---|
89 | ] |
---|
90 | |
---|
91 | memoryStatus = MEMORYSTATUS() |
---|
92 | memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS) |
---|
93 | kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus)) |
---|
94 | return (memoryStatus.dwTotalPhys, memoryStatus.dwAvailPhys) |
---|
95 | |
---|
96 | def _disk_c(self): |
---|
97 | drive = unicode(os.getenv("SystemDrive")) |
---|
98 | freeuser = ctypes.c_int64() |
---|
99 | total = ctypes.c_int64() |
---|
100 | free = ctypes.c_int64() |
---|
101 | ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive, |
---|
102 | ctypes.byref(freeuser), |
---|
103 | ctypes.byref(total), |
---|
104 | ctypes.byref(free)) |
---|
105 | return freeuser.value |
---|
106 | |
---|
107 | if __name__ == "__main__": |
---|
108 | s = SystemInformation() |
---|
109 | print s.os |
---|
110 | print s.cpu |
---|
111 | print "Browsers: " |
---|
112 | print "\n".join([" %s %s" % b for b in s.browsers]) |
---|
113 | print "RAM : %dMb total" % s.totalRam |
---|
114 | print "RAM : %dMb free" % s.availableRam |
---|
115 | print "System HD : %dGb free" % s.hdFree |
---|
116 | |
---|