# MIT License - Copyright (c) 2023 - Captain FLAM # # https://github.com/Captain-FLAM/KaraFan import os, sys, subprocess, json, regex from distutils import spawn def Get(font_size): import platform, psutil system = platform.system() html = '
' html += "**** System Informations ****" return html
" # Get the total virtual memory size (in bytes) total_virtual_memory = psutil.virtual_memory().total unit_index = 0 units = ['B', 'KB', 'MB', 'GB', 'TB'] # Convert size into larger units until size is less than 1024 while total_virtual_memory >= 1024: total_virtual_memory /= 1024 unit_index += 1 html += "Python : "+ regex.sub(r'\(.*?\)\s*', '', sys.version) +"
" html += f"OS : {system} { platform.release() }
" html += f"RAM : {total_virtual_memory:.2f} {units[unit_index]}
" html += f"Current directory : { os.getcwd() }
" html += "**** CPU Informations ****
" match system: case 'Windows': # use 'wmic' try: cpu_info = subprocess.check_output(['wmic', 'cpu', 'get', 'Caption,MaxClockSpeed,NumberOfCores,NumberOfLogicalProcessors', '/FORMAT:CSV'], shell=True, stderr=subprocess.STDOUT).decode('utf-8') cpu_info = cpu_info.split('\n')[-2].strip() # catch the last line # Split values cpu_info = cpu_info.split(',') html += f"CPU : {cpu_info[1]}
" html += f"Cores : {cpu_info[3]}
" html += f"Threads : {cpu_info[4]}
" html += f"MaxClock Speed : {cpu_info[2]} MHz" except FileNotFoundError: html += "--> Can't get CPU infos : 'wmic' tool is not available on this platform." case 'Linux': # use 'lscpu' try: cpu_info = subprocess.check_output(['lscpu', '-J'], shell=True, stderr=subprocess.STDOUT).decode('utf-8') try: cpu_info = json.loads(cpu_info) if 'lscpu' in cpu_info: sockets = cores = threads = 1 for item in cpu_info["lscpu"]: if 'field' in item and 'data' in item: data = item['data'] match item['field']: case "Architecture:": html += f"Arch : {data}
" case "Model name:": html += f"CPU : {data}
" case "CPU max MHz:": html += f"MaxClock Speed : {int(data)} MHz
" case "Socket(s):": sockets = int(data) case "Core(s) per socket:": cores = int(data) case "Thread(s) per core:": threads = int(data) html += f"Cores : {cores * sockets}
" html += f"Threads : {threads * cores * sockets}" except json.decoder.JSONDecodeError: html += "--> Can't get CPU infos : 'lscpu' doesn't work correctly on this platform ??" except FileNotFoundError: html += "--> Can't get CPU infos : 'lscpu' tool is not available on this platform." case 'Darwin': # For macOS, use 'sysctl' try: ## TODO : decode CPU infos on macOS html += "CPU : " + subprocess.check_output(['sysctl', 'machdep.cpu'], shell=True, stderr=subprocess.STDOUT).decode('utf-8') except FileNotFoundError: html += "--> Can't get CPU infos : 'sysctl' tool is not available on this platform." case _: # For other platforms, display a generic message html += "--> CPU informations are not available for this platform." html += "
**** GPU Informations ****
" try: if platform.system() == "Windows": # If the platform is Windows and nvidia-smi # could not be found from the environment path, # try to find it from system drive with default installation path nvidia_smi = spawn.find_executable('nvidia-smi') if nvidia_smi is None: nvidia_smi = "%s\\Program Files\\NVIDIA Corporation\\NVSMI\\nvidia-smi.exe" % os.environ['systemdrive'] else: nvidia_smi = "nvidia-smi" # Nvidia details information gpu_info = subprocess.check_output(nvidia_smi, shell=True, stderr=subprocess.STDOUT).decode('utf-8') html += ''+ gpu_info +'
' if gpu_info.find('failed') >= 0: html += "GPU runtime is disabled. You can only use your CPU with available RAM." elif gpu_info.find('Tesla T4') >= 0: html += "You got a Tesla T4 GPU. (speeds are around 10-25 iterations/sec)" elif gpu_info.find('Tesla P4') >= 0: html += "You got a Tesla P4 GPU. (speeds are around 8-22 iterations/sec)" elif gpu_info.find('Tesla K80') >= 0: html += "You got a Tesla K80 GPU. (This is the most common and slowest gpu, speeds are around 2-10 iterations/sec)" elif gpu_info.find('Tesla P100') >= 0: html += "You got a Tesla P100 GPU. (This is the FASTEST gpu, speeds are around 15-42 iterations/sec)" except subprocess.CalledProcessError as e: print(f"Erreur lors de l'exécution de nvidia-smi : {e.returncode}\n{e.output.decode('utf-8')}") except FileNotFoundError: html += "--> Can't get GPU infos : 'nvidia-smi' tool is not available on this platform." html += "