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 20def run_cmd(cmd): 21 res = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, 22 stderr=subprocess.PIPE) 23 sout, serr = res.communicate() 24 25 return res.pid, res.returncode, sout, serr 26 27def check_darwin_system(): 28 check_system_cmd = "uname -s" 29 res = run_cmd(check_system_cmd) 30 if res[1] == 0 and res[2] != "": 31 if "Darwin" in res[2].strip().decode(): 32 print("system is darwin") 33 34 return 0 35 36def check_m1_cpu(): 37 check_host_cpu_cmd = "sysctl machdep.cpu.brand_string" 38 res = run_cmd(check_host_cpu_cmd) 39 if res[1] == 0 and res[2] != "": 40 host_cpu = res[2].strip().decode().split("brand_string:")[-1] 41 if "M1" in host_cpu: 42 print("host cpu is m1") 43 44 return 0 45 46def main(): 47 if sys.argv[1] == "cpu": 48 check_m1_cpu() 49 elif sys.argv[1] == "system": 50 check_darwin_system() 51 else: 52 return 0 53 54if __name__ == '__main__': 55 sys.exit(main()) 56