-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_os_info.py
70 lines (58 loc) · 2.02 KB
/
get_os_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import platform
import subprocess
from utils import ind, print_success
def get_os_info():
os_name = platform.system()
if os_name == 'Darwin':
os_name = 'Mac'
darwin_version = platform.uname().release
os_version_name = darwin_to_mac_version(darwin_version)
return os_name, os_version_name, darwin_version
else:
os_version = platform.release()
return os_name, os_version, None
def get_architecture():
architecture = platform.machine()
if architecture == 'x86_64':
return 'Intel', None
elif 'arm' in architecture:
chip_version = get_apple_chip_version()
return 'Apple Silicon', chip_version
else:
return 'Unknown', None
def is_apple_silicon():
architecture, _ = get_architecture()
return architecture == 'Apple Silicon'
def get_apple_chip_version():
try:
chip_info = subprocess.check_output(
['sysctl', '-n', 'machdep.cpu.brand_string'], text=True)
if 'Apple M1' in chip_info:
return 'M1'
elif 'Apple M2' in chip_info:
return 'M2'
elif 'Apple M3' in chip_info:
return 'M3'
else:
return 'Unknown'
except Exception as e:
print(f"An error occurred: {e}")
return 'Unknown'
def darwin_to_mac_version(darwin_version):
major_version = darwin_version.split('.')[0]
darwin_mac_mapping = {
'20': 'Big Sur',
'21': 'Monterey',
'22': 'Ventura',
'23': 'Sonoma'
}
return darwin_mac_mapping.get(major_version, 'OS not found')
def print_os_info():
os_name, os_version_name, darwin_version = get_os_info()
architecture, chip_version = get_architecture()
if os_name == 'Mac':
print_success(ind(
f"Your OS is {os_name} {os_version_name} (Darwin Kernel Version {darwin_version}) on {architecture} with a {chip_version} chip."))
else:
print_success(ind(
f"You OS is {os_name} version {os_version_name} on {architecture} architecture."))