1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2020 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18import os 19import re 20 21from hb_internal.cts.menuconfig import Menuconfig 22from hb_internal.common.utils import OHOSException 23 24 25class Device(): 26 @staticmethod 27 def is_in_device(): 28 cwd_pardir = os.path.dirname(os.path.dirname(os.getcwd())) 29 return os.path.basename(cwd_pardir) == 'device' 30 31 @staticmethod 32 def is_kernel(kernel_path): 33 return os.path.isdir(kernel_path) and\ 34 'config.gni' in os.listdir(kernel_path) 35 36 @staticmethod 37 def device_menuconfig(): 38 kernel_path_dict = {} 39 cwd = os.getcwd() 40 Device.check_path(cwd) 41 42 for kernel_config, kernel_path in Device.get_kernel_config(cwd): 43 kernel_type, kernel_version = Device.get_kernel_info(kernel_config) 44 kernel_path_dict['{}@{}'.format(kernel_type, kernel_version)] =\ 45 kernel_path 46 47 if not len(kernel_path_dict): 48 raise OHOSException('no valid kernel found') 49 50 choices = [{'name': kernel} for kernel in kernel_path_dict.keys()] 51 52 menu = Menuconfig() 53 kernel = menu.list_promt('kernel', 'Which kernel do you need?', 54 choices).get('kernel')[0] 55 return kernel_path_dict.get(kernel), kernel.split('@')[0],\ 56 os.path.basename(cwd) 57 58 @staticmethod 59 def get_device_path(board_path, kernel_type, kernel_version): 60 for kernel_config, kernel_path in Device.get_kernel_config(board_path): 61 if Device.match_kernel(kernel_config, kernel_type, kernel_version): 62 return kernel_path 63 64 raise OHOSException(f'cannot find {kernel_type}_{kernel_version} ' 65 f'in {board_path}') 66 67 @staticmethod 68 def get_kernel_config(board_path): 69 Device.check_path(board_path) 70 for kernel in os.listdir(board_path): 71 kernel_path = os.path.join(board_path, kernel) 72 73 if os.path.isdir(kernel_path): 74 kernel_config = os.path.join(kernel_path, 'config.gni') 75 if not os.path.isfile(kernel_config): 76 continue 77 yield kernel_config, kernel_path 78 79 @staticmethod 80 def match_kernel(config, kernel, version): 81 kernel_pattern = r'kernel_type ?= ?"{}"'.format(kernel) 82 version_pattern = r'kernel_version ?= ?"{}"'.format(version) 83 84 with open(config, 'rt', encoding='utf-8') as config_file: 85 data = config_file.read() 86 return re.search(kernel_pattern, data) and\ 87 re.search(version_pattern, data) 88 89 @staticmethod 90 def get_kernel_info(config): 91 kernel_pattern = r'kernel_type ?= ?"(\w+)"' 92 version_pattern = r'kernel_version ?= ?"([a-zA-Z0-9._]*)"' 93 94 with open(config, 'rt', encoding='utf-8') as config_file: 95 data = config_file.read() 96 kernel_list = re.findall(kernel_pattern, data) 97 version_list = re.findall(version_pattern, data) 98 if not len(kernel_list) or not len(version_list): 99 raise OHOSException(f'kernel_type or kernel_version ' 100 f'not found in {config}') 101 102 return kernel_list[0], version_list[0] 103 104 @staticmethod 105 def check_path(path): 106 if os.path.isdir(path) or os.path.isfile(path): 107 return 108 raise OHOSException(f'invalid path: {path}') 109 110 @staticmethod 111 def get_compiler(config_path): 112 config = os.path.join(config_path, 'config.gni') 113 if not os.path.isfile(config): 114 return '' 115 compiler_pattern = r'board_toolchain_type ?= ?"(\w+)"' 116 with open(config, 'rt', encoding='utf-8') as config_file: 117 data = config_file.read() 118 compiler_list = re.findall(compiler_pattern, data) 119 if not len(compiler_list): 120 raise OHOSException(f'board_toolchain_type is None in {config}') 121 122 return compiler_list[0] 123