|
2 | 2 | import platform
|
3 | 3 | import datetime
|
4 | 4 | import os
|
| 5 | +from functools import lru_cache |
5 | 6 |
|
6 | 7 | package_dir = os.path.dirname(os.path.abspath(__file__))
|
7 | 8 |
|
8 |
| -def get_system_clibot(): |
| 9 | +@lru_cache(maxsize=1) |
| 10 | +def get_system_info(): |
9 | 11 | """Get system information."""
|
10 | 12 | system_name = platform.system()
|
11 | 13 |
|
12 | 14 | 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() |
15 | 18 |
|
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") |
23 | 22 |
|
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() |
40 | 55 |
|
41 | 56 | system_prompt = prompt.format(
|
42 | 57 | os_name=os_name,
|
|
0 commit comments