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 19import re 20 21 22def run_cmd(cmd: str): 23 res = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, 24 stderr=subprocess.PIPE) 25 sout, serr = res.communicate() 26 27 return res.pid, res.returncode, sout, serr 28 29 30def check_darwin_system() -> int: 31 check_system_cmd = "uname -s" 32 res = run_cmd(check_system_cmd) 33 if res[1] == 0 and res[2] != "": 34 if "Darwin" in res[2].strip().decode(): 35 print("system is darwin") 36 37 return 0 38 39 40def check_cpu() -> int: 41 check_host_cpu_cmd = "sysctl -n machdep.cpu.brand_string" 42 res = run_cmd(check_host_cpu_cmd)[2].strip().decode() 43 pattern = r'(M\d+\b)(?:\s+[A-Za-z]+)?' 44 matches = re.findall(pattern, res) 45 if matches: 46 print("host cpu is", matches[0]) 47 48 return 0 49 50 51def main(): 52 if sys.argv[1] == "cpu": 53 return check_cpu() 54 elif sys.argv[1] == "system": 55 return check_darwin_system() 56 else: 57 return 0 58 59if __name__ == '__main__': 60 sys.exit(main()) 61