• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2023 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 exceptions.ohos_exception import OHOSException
22from containers.status import throw_exception
23
24
25class DeviceUtil():
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: str):
33        return os.path.isdir(kernel_path) and\
34            'config.gni' in os.listdir(kernel_path)
35
36    @staticmethod
37    @throw_exception
38    def get_device_path(board_path: str, kernel_type: str, kernel_version: str):
39        for kernel_config, kernel_path in DeviceUtil.get_kernel_config(board_path):
40            if DeviceUtil.match_kernel(kernel_config, kernel_type, kernel_version):
41                return kernel_path
42
43        raise OHOSException(f'cannot find {kernel_type}_{kernel_version} '
44                            f'in {board_path}', "0004")
45
46    @staticmethod
47    def get_kernel_config(board_path: str):
48        DeviceUtil.check_path(board_path)
49        for kernel in os.listdir(board_path):
50            kernel_path = os.path.join(board_path, kernel)
51
52            if os.path.isdir(kernel_path):
53                kernel_config = os.path.join(kernel_path, 'config.gni')
54                if not os.path.isfile(kernel_config):
55                    continue
56                yield kernel_config, kernel_path
57
58    @staticmethod
59    def match_kernel(config: str, kernel: str, version: str):
60        kernel_pattern = r'kernel_type ?= ?"{}"'.format(kernel)
61        version_pattern = r'kernel_version ?= ?"{}"'.format(version)
62
63        with open(config, 'rt', encoding='utf-8') as config_file:
64            data = config_file.read()
65            return re.search(kernel_pattern, data) and\
66                re.search(version_pattern, data)
67
68    @staticmethod
69    @throw_exception
70    def get_kernel_info(config: str):
71        kernel_pattern = r'kernel_type ?= ?"(\w+)"'
72        version_pattern = r'kernel_version ?= ?"([a-zA-Z0-9._]*)"'
73
74        with open(config, 'rt', encoding='utf-8') as config_file:
75            data = config_file.read()
76            kernel_list = re.findall(kernel_pattern, data)
77            version_list = re.findall(version_pattern, data)
78            if not len(kernel_list) or not len(version_list):
79                raise OHOSException(f'kernel_type or kernel_version '
80                                    f'not found in {config}', '0005')
81
82            return kernel_list[0], version_list[0]
83
84    @staticmethod
85    @throw_exception
86    def check_path(path: str):
87        if os.path.isdir(path) or os.path.isfile(path):
88            return
89        raise OHOSException(f'invalid path: {path}', '0006')
90
91    @staticmethod
92    @throw_exception
93    def get_compiler(config_path: str):
94        config = os.path.join(config_path, 'config.gni')
95        if not os.path.isfile(config):
96            return ''
97        compiler_pattern = r'board_toolchain_type ?= ?"(\w+)"'
98        with open(config, 'rt', encoding='utf-8') as config_file:
99            data = config_file.read()
100        compiler_list = re.findall(compiler_pattern, data)
101        if not len(compiler_list):
102            raise OHOSException(
103                f'board_toolchain_type is None in {config}', '0007')
104
105        return compiler_list[0]
106