Skip to content

Commit 0eb4531

Browse files
committed
Add caching for system information
1 parent 8f0b2d9 commit 0eb4531

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

clibot/system.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,56 @@
22
import platform
33
import datetime
44
import os
5+
from functools import lru_cache
56

67
package_dir = os.path.dirname(os.path.abspath(__file__))
78

8-
def get_system_clibot():
9+
@lru_cache(maxsize=1)
10+
def get_system_info():
911
"""Get system information."""
1012
system_name = platform.system()
1113

1214
if system_name == "Windows":
13-
os_info = subprocess.check_output('systeminfo', shell=True).decode('utf-8').splitlines()
14-
os_name = next((line.split(':', 1)[1].strip() for line in os_info if line.startswith('OS Name:')), "Unknown OS")
15+
return get_windows_info()
16+
else:
17+
return get_unix_info()
1518

16-
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
17-
kernel_info = subprocess.check_output('ver', shell=True).decode('utf-8').strip()
18-
cpu_info = subprocess.check_output('wmic cpu get caption', shell=True).decode('utf-8').strip().split('\n')[1].strip()
19-
gpu_info = subprocess.check_output('wmic path win32_videocontroller get caption', shell=True).decode('utf-8').strip().split('\n')[1].strip()
20-
mem_info = subprocess.check_output('wmic memorychip get capacity', shell=True).decode('utf-8').strip().split('\n')[1].strip()
21-
disk_info = subprocess.check_output('wmic logicaldisk get size,freespace,caption', shell=True).decode('utf-8').strip().split('\n')[1:]
22-
disk_info = ', '.join(disk_info)
19+
def get_windows_info():
20+
os_info = subprocess.check_output('systeminfo', shell=True).decode('utf-8').splitlines()
21+
os_name = next((line.split(':', 1)[1].strip() for line in os_info if line.startswith('OS Name:')), "Unknown OS")
2322

24-
else:
25-
try:
26-
os_info = subprocess.check_output('cat /etc/os-release', shell=True).decode('utf-8').strip()
27-
os_name = next((line.split('=')[1].strip('"') for line in os_info.split('\n') if line.startswith('PRETTY_NAME=')), "Unknown OS")
28-
except subprocess.CalledProcessError:
29-
os_name = "Unknown OS"
30-
31-
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
32-
kernel_info = subprocess.check_output('uname -a', shell=True).decode('utf-8').strip()
33-
cpu_info_raw = subprocess.check_output('lscpu | grep "Model name"', shell=True).decode('utf-8').strip()
34-
cpu_info = cpu_info_raw.split(':', 1)[1].strip() if ':' in cpu_info_raw else cpu_info_raw
35-
gpu_info = subprocess.check_output(r"lspci | grep -i 'vga\|3d\|2d'", shell=True).decode('utf-8').strip()
36-
mem_info_raw = subprocess.check_output('free -h | grep "Mem"', shell=True).decode('utf-8').strip().split()
37-
mem_info = f"Total: {mem_info_raw[1]}, Used: {mem_info_raw[2]}, Free: {mem_info_raw[3]}, Shared: {mem_info_raw[4]}, Buffers: {mem_info_raw[5]}, Available: {mem_info_raw[6]}"
38-
disk_info_raw = subprocess.check_output('df -h --total | grep "total"', shell=True).decode('utf-8').strip().split()
39-
disk_info = f"Total: {disk_info_raw[1]}, Used: {disk_info_raw[2]}, Available: {disk_info_raw[3]}, Use%: {disk_info_raw[4]}, Mounted on: {disk_info_raw[5]}"
23+
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
24+
kernel_info = subprocess.check_output('ver', shell=True).decode('utf-8').strip()
25+
cpu_info = subprocess.check_output('wmic cpu get caption', shell=True).decode('utf-8').strip().split('\n')[1].strip()
26+
gpu_info = subprocess.check_output('wmic path win32_videocontroller get caption', shell=True).decode('utf-8').strip().split('\n')[1].strip()
27+
mem_info = subprocess.check_output('wmic memorychip get capacity', shell=True).decode('utf-8').strip().split('\n')[1].strip()
28+
disk_info = subprocess.check_output('wmic logicaldisk get size,freespace,caption', shell=True).decode('utf-8').strip().split('\n')[1:]
29+
disk_info = ', '.join(disk_info)
30+
31+
return os_name, now, kernel_info, cpu_info, gpu_info, mem_info, disk_info
32+
33+
def get_unix_info():
34+
try:
35+
os_info = subprocess.check_output('cat /etc/os-release', shell=True).decode('utf-8').strip()
36+
os_name = next((line.split('=')[1].strip('"') for line in os_info.split('\n') if line.startswith('PRETTY_NAME=')), "Unknown OS")
37+
except subprocess.CalledProcessError:
38+
os_name = "Unknown OS"
39+
40+
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
41+
kernel_info = subprocess.check_output('uname -a', shell=True).decode('utf-8').strip()
42+
cpu_info_raw = subprocess.check_output('lscpu | grep "Model name"', shell=True).decode('utf-8').strip()
43+
cpu_info = cpu_info_raw.split(':', 1)[1].strip() if ':' in cpu_info_raw else cpu_info_raw
44+
gpu_info = subprocess.check_output(r"lspci | grep -i 'vga\|3d\|2d'", shell=True).decode('utf-8').strip()
45+
mem_info_raw = subprocess.check_output('free -h | grep "Mem"', shell=True).decode('utf-8').strip().split()
46+
mem_info = f"Total: {mem_info_raw[1]}, Used: {mem_info_raw[2]}, Free: {mem_info_raw[3]}, Shared: {mem_info_raw[4]}, Buffers: {mem_info_raw[5]}, Available: {mem_info_raw[6]}"
47+
disk_info_raw = subprocess.check_output('df -h --total | grep "total"', shell=True).decode('utf-8').strip().split()
48+
disk_info = f"Total: {disk_info_raw[1]}, Used: {disk_info_raw[2]}, Available: {disk_info_raw[3]}, Use%: {disk_info_raw[4]}, Mounted on: {disk_info_raw[5]}"
49+
50+
return os_name, now, kernel_info, cpu_info, gpu_info, mem_info, disk_info
51+
52+
def get_system_clibot():
53+
"""Get system information for Clibot."""
54+
os_name, now, kernel_info, cpu_info, gpu_info, mem_info, disk_info = get_system_info()
4055

4156
system_prompt = prompt.format(
4257
os_name=os_name,

0 commit comments

Comments
 (0)