15f9996aaSopenharmony_ci#!/usr/bin/env python 25f9996aaSopenharmony_ci# -*- coding: utf-8 -*- 35f9996aaSopenharmony_ci 45f9996aaSopenharmony_ci# 55f9996aaSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd. 65f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 75f9996aaSopenharmony_ci# you may not use this file except in compliance with the License. 85f9996aaSopenharmony_ci# You may obtain a copy of the License at 95f9996aaSopenharmony_ci# 105f9996aaSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 115f9996aaSopenharmony_ci# 125f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software 135f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 145f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 155f9996aaSopenharmony_ci# See the License for the specific language governing permissions and 165f9996aaSopenharmony_ci# limitations under the License. 175f9996aaSopenharmony_ci 185f9996aaSopenharmony_ciimport os 195f9996aaSopenharmony_ciimport re 205f9996aaSopenharmony_ci 215f9996aaSopenharmony_cifrom exceptions.ohos_exception import OHOSException 225f9996aaSopenharmony_cifrom containers.status import throw_exception 235f9996aaSopenharmony_ci 245f9996aaSopenharmony_ci 255f9996aaSopenharmony_ciclass DeviceUtil(): 265f9996aaSopenharmony_ci @staticmethod 275f9996aaSopenharmony_ci def is_in_device(): 285f9996aaSopenharmony_ci cwd_pardir = os.path.dirname(os.path.dirname(os.getcwd())) 295f9996aaSopenharmony_ci return os.path.basename(cwd_pardir) == 'device' 305f9996aaSopenharmony_ci 315f9996aaSopenharmony_ci @staticmethod 325f9996aaSopenharmony_ci def is_kernel(kernel_path: str): 335f9996aaSopenharmony_ci return os.path.isdir(kernel_path) and\ 345f9996aaSopenharmony_ci 'config.gni' in os.listdir(kernel_path) 355f9996aaSopenharmony_ci 365f9996aaSopenharmony_ci @staticmethod 375f9996aaSopenharmony_ci @throw_exception 385f9996aaSopenharmony_ci def get_device_path(board_path: str, kernel_type: str, kernel_version: str): 395f9996aaSopenharmony_ci for kernel_config, kernel_path in DeviceUtil.get_kernel_config(board_path): 405f9996aaSopenharmony_ci if DeviceUtil.match_kernel(kernel_config, kernel_type, kernel_version): 415f9996aaSopenharmony_ci return kernel_path 425f9996aaSopenharmony_ci 435f9996aaSopenharmony_ci raise OHOSException(f'cannot find {kernel_type}_{kernel_version} ' 445f9996aaSopenharmony_ci f'in {board_path}', "0004") 455f9996aaSopenharmony_ci 465f9996aaSopenharmony_ci @staticmethod 475f9996aaSopenharmony_ci def get_kernel_config(board_path: str): 485f9996aaSopenharmony_ci DeviceUtil.check_path(board_path) 495f9996aaSopenharmony_ci for kernel in os.listdir(board_path): 505f9996aaSopenharmony_ci kernel_path = os.path.join(board_path, kernel) 515f9996aaSopenharmony_ci 525f9996aaSopenharmony_ci if os.path.isdir(kernel_path): 535f9996aaSopenharmony_ci kernel_config = os.path.join(kernel_path, 'config.gni') 545f9996aaSopenharmony_ci if not os.path.isfile(kernel_config): 555f9996aaSopenharmony_ci continue 565f9996aaSopenharmony_ci yield kernel_config, kernel_path 575f9996aaSopenharmony_ci 585f9996aaSopenharmony_ci @staticmethod 595f9996aaSopenharmony_ci def match_kernel(config: str, kernel: str, version: str): 605f9996aaSopenharmony_ci kernel_pattern = r'kernel_type ?= ?"{}"'.format(kernel) 615f9996aaSopenharmony_ci version_pattern = r'kernel_version ?= ?"{}"'.format(version) 625f9996aaSopenharmony_ci 635f9996aaSopenharmony_ci with open(config, 'rt', encoding='utf-8') as config_file: 645f9996aaSopenharmony_ci data = config_file.read() 655f9996aaSopenharmony_ci return re.search(kernel_pattern, data) and\ 665f9996aaSopenharmony_ci re.search(version_pattern, data) 675f9996aaSopenharmony_ci 685f9996aaSopenharmony_ci @staticmethod 695f9996aaSopenharmony_ci @throw_exception 705f9996aaSopenharmony_ci def get_kernel_info(config: str): 715f9996aaSopenharmony_ci kernel_pattern = r'kernel_type ?= ?"(\w+)"' 725f9996aaSopenharmony_ci version_pattern = r'kernel_version ?= ?"([a-zA-Z0-9._]*)"' 735f9996aaSopenharmony_ci 745f9996aaSopenharmony_ci with open(config, 'rt', encoding='utf-8') as config_file: 755f9996aaSopenharmony_ci data = config_file.read() 765f9996aaSopenharmony_ci kernel_list = re.findall(kernel_pattern, data) 775f9996aaSopenharmony_ci version_list = re.findall(version_pattern, data) 785f9996aaSopenharmony_ci if not len(kernel_list) or not len(version_list): 795f9996aaSopenharmony_ci raise OHOSException(f'kernel_type or kernel_version ' 805f9996aaSopenharmony_ci f'not found in {config}', '0005') 815f9996aaSopenharmony_ci 825f9996aaSopenharmony_ci return kernel_list[0], version_list[0] 835f9996aaSopenharmony_ci 845f9996aaSopenharmony_ci @staticmethod 855f9996aaSopenharmony_ci @throw_exception 865f9996aaSopenharmony_ci def check_path(path: str): 875f9996aaSopenharmony_ci if os.path.isdir(path) or os.path.isfile(path): 885f9996aaSopenharmony_ci return 895f9996aaSopenharmony_ci raise OHOSException(f'invalid path: {path}', '0006') 905f9996aaSopenharmony_ci 915f9996aaSopenharmony_ci @staticmethod 925f9996aaSopenharmony_ci @throw_exception 935f9996aaSopenharmony_ci def get_compiler(config_path: str): 945f9996aaSopenharmony_ci config = os.path.join(config_path, 'config.gni') 955f9996aaSopenharmony_ci if not os.path.isfile(config): 965f9996aaSopenharmony_ci return '' 975f9996aaSopenharmony_ci compiler_pattern = r'board_toolchain_type ?= ?"(\w+)"' 985f9996aaSopenharmony_ci with open(config, 'rt', encoding='utf-8') as config_file: 995f9996aaSopenharmony_ci data = config_file.read() 1005f9996aaSopenharmony_ci compiler_list = re.findall(compiler_pattern, data) 1015f9996aaSopenharmony_ci if not len(compiler_list): 1025f9996aaSopenharmony_ci raise OHOSException( 1035f9996aaSopenharmony_ci f'board_toolchain_type is None in {config}', '0007') 1045f9996aaSopenharmony_ci 1055f9996aaSopenharmony_ci return compiler_list[0] 106