1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import sys 18import subprocess 19 20 21def run_cmd(cmd): 22 res = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, 23 stderr=subprocess.PIPE) 24 sout, serr = res.communicate() 25 26 return res.pid, res.returncode, sout, serr 27 28 29def check_darwin_system(): 30 check_system_cmd = "uname -s" 31 res = run_cmd(check_system_cmd) 32 if res[1] == 0 and res[2] != "": 33 if "Darwin" in res[2].strip().decode(): 34 print("system is darwin") 35 36 return 0 37 38 39def check_m1_cpu(): 40 check_host_cpu_cmd = "sysctl machdep.cpu.brand_string" 41 res = run_cmd(check_host_cpu_cmd) 42 if res[1] == 0 and res[2] != "": 43 host_cpu = res[2].strip().decode().split("brand_string:")[-1] 44 if "M1" in host_cpu: 45 print("host cpu is m1") 46 elif "M2" in host_cpu: 47 print("host cpu is m2") 48 49 return 0 50 51 52def main(): 53 if sys.argv[1] == "cpu": 54 check_m1_cpu() 55 elif sys.argv[1] == "system": 56 check_darwin_system() 57 else: 58 return 0 59 60if __name__ == '__main__': 61 sys.exit(main()) 62