• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: str):
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() -> int:
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_cpu() -> int:
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        host_cpu_list = ['M1', 'M2', 'M3']
45        for host_cpu_num in host_cpu_list:
46            if host_cpu_num in host_cpu:
47                print("host cpu is", host_cpu_num)
48                break
49
50    return 0
51
52
53def main():
54    if sys.argv[1] == "cpu":
55        check_cpu()
56    elif sys.argv[1] == "system":
57        check_darwin_system()
58    else:
59        return 0
60
61if __name__ == '__main__':
62    sys.exit(main())
63