import os
import sys
import subprocess
import re
def run_cmd(cmd: str):
res = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
sout, serr = res.communicate()
return res.pid, res.returncode, sout, serr
def check_darwin_system() -> int:
check_system_cmd = "uname -s"
res = run_cmd(check_system_cmd)
if res[1] == 0 and res[2] != "":
if "Darwin" in res[2].strip().decode():
print("system is darwin")
return 0
def check_cpu() -> int:
check_host_cpu_cmd = "sysctl -n machdep.cpu.brand_string"
res = run_cmd(check_host_cpu_cmd)[2].strip().decode()
pattern = r'(M\d+\b)(?:\s+[A-Za-z]+)?'
matches = re.findall(pattern, res)
if matches:
print("host cpu is", matches[0])
return 0
def main():
if sys.argv[1] == "cpu":
return check_cpu()
elif sys.argv[1] == "system":
return check_darwin_system()
else:
return 0
if __name__ == '__main__':
sys.exit(main())