Extracting system information using python _winreg --- not supported by Jython

Asked by balakrishnan

I need to extract
(Processor details, memory details, Drive details , Graphics card details , Network driver details ) .

Below is the code i have tried but i dont know how to extract graphics card details ...Please let me know.

import sys
import os
import ctypes
import _winreg

def get_registry_value(key, subkey, value):
    if sys.platform != 'win32':
        raise OSError("get_registry_value is only supported on Windows")

    import _winreg
    key = getattr(_winreg, key)
    handle = _winreg.OpenKey(key, subkey)
    (value, type) = _winreg.QueryValueEx(handle, value)
    return value

class SystemInformation:
    def __init__(self):
        self.os = self._os_version().strip()
        self.cpu = self._cpu().strip()
        self.browsers = self._browsers()
        self.totalRam, self.availableRam = self._ram()
        self.totalRam = self.totalRam / (1024*1024)
        self.availableRam = self.availableRam / (1024*1024)
        self.hdFree = self._disk_c() / (1024*1024*1024)

    def _os_version(self):
        def get(key):
            return get_registry_value(
                "HKEY_LOCAL_MACHINE",
                "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
                key)
        os = get("ProductName")
        sp = get("CSDVersion")
        build = get("CurrentBuildNumber")
        return "%s %s (build %s)" % (os, sp, build)

    def _cpu(self):
        return get_registry_value(
            "HKEY_LOCAL_MACHINE",
            "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
            "ProcessorNameString")

    def _firefox_version(self):
        try:
            version = get_registry_value(
                "HKEY_LOCAL_MACHINE",
                "SOFTWARE\\Mozilla\\Mozilla Firefox",
                "CurrentVersion")
            version = (u"Mozilla Firefox", version)
        except WindowsError:
            version = None
        return version

    def _iexplore_version(self):
        try:
            version = get_registry_value(
                "HKEY_LOCAL_MACHINE",
                "SOFTWARE\\Microsoft\\Internet Explorer",
                "Version")
            version = (u"Internet Explorer", version)
        except WindowsError:
            version = None
        return version

    def _browsers(self):
        browsers = []
        firefox = self._firefox_version()
        if firefox:
            browsers.append(firefox)
        iexplore = self._iexplore_version()
        if iexplore:
            browsers.append(iexplore)

        return browsers

    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)
            ]

        memoryStatus = MEMORYSTATUS()
        memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
        kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))
        return (memoryStatus.dwTotalPhys, memoryStatus.dwAvailPhys)

    def _disk_c(self):

        drive = unicode(os.getenv("SystemDrive"))
        freeuser = ctypes.c_int64()
        total = ctypes.c_int64()
        free = ctypes.c_int64()
        ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive,
                                        ctypes.byref(freeuser),
                                        ctypes.byref(total),
                                        ctypes.byref(free))
        return freeuser.value

if __name__ == "__main__":
    s = SystemInformation()
    print s.os
    print s.cpu
    print "Browsers: "
    print "\n".join([" %s %s" % b for b in s.browsers])
    print "RAM : %dMb total" % s.totalRam
    print "RAM : %dMb free" % s.availableRam
    print "System HD : %dGb free" % s.hdFree

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Not really a question for this board.

This information seems not to be available through a registry key, otherwise you should have found this already ;-)

You might have a look for a "hardware information tool", that can be run from command line and produces some structured output to stdout. You can use os.popen().readlines() to run the tool and get the output back for evaluation.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

I just realized, that this is Python C-type stuff you are using.

_winreg and ctypes are not available in Jython environments, so this will not work with Sikuli scripts.

As a workaround you might use the Windows command reg together with os.popen().
see https://answers.launchpad.net/sikuli/+question/166375

Revision history for this message
maroz (v-dorogiy) said :
#3

Sorry for my kinda stupid question: how can I get information about RAM (e.g. its total amount, available amount, used amount)? Or even better: how can I get information about used memory but for specific process?

Revision history for this message
maroz (v-dorogiy) said :
#4

oh, I forgot to say that I use Windows OS.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

@maroz
on a command line:
tasklist /v /fo /csv

to see how the output looks like

from a script (version 1.0.1+):

out = run("tasklist /v /fo /csv")
for line in out.split("\n"):
    line = line.strip()
    print line
    items = line.split(";")
    for item in items:
        print item.strip()

Revision history for this message
maroz (v-dorogiy) said :
#6

Thanks, @RaiMan! That is what I'm looking for.

Can you help with this problem?

Provide an answer of your own, or ask balakrishnan for more information if necessary.

To post a message you must log in.