• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2022 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 re
18
19
20class BundleCheckTools:
21    '''用于检查 bundle.json 的工具箱。'''
22
23    @staticmethod
24    def is_all_lower(string:str):
25        ''' 判断字符串是否有且仅有小写字母。'''
26        if not string:
27            return False
28        for i in string:
29            if not (97 <= ord(i) <= 122):
30                return False
31        return True
32
33    @staticmethod
34    def is_number(string:str) -> bool:
35        ''' 判断字符串 s 是否能表示为数值。'''
36        try:
37            float(string)
38            return True
39        except (OverflowError, ValueError):
40            return False
41
42    @staticmethod
43    def split_by_unit(string:str):
44        '''分离 s 字符串的数值和单位'''
45        match = re.match(r'^[~]?(\d+)([a-zA-Z]*)$', string)
46        if not match:
47            return (0, '')
48        return (float(match.group(1)), match.group(2))
49
50    @staticmethod
51    def get_root_path() -> str:
52        cur_path = os.path.dirname(os.path.abspath(__file__))
53        root_path = os.path.normpath(os.path.join(cur_path, '../../../../../'))
54        try:
55            if BundleCheckTools.is_project(root_path):
56                return root_path
57            else:
58                raise ValueError(
59                    'get root path failed, please check script "bundle_check_common.py" under '
60                    'the path(build/tools/component_tools/static_check/bundle_check).')
61        except ValueError as get_path_error:
62            print("Error: ", repr(get_path_error))
63        return ""
64
65    @staticmethod
66    def is_project(path:str) -> bool:
67        '''
68        @func: 判断路径是否源码工程。
69        @note: 通过是否存在 .repo/manifests 目录判断。
70        '''
71        if not path:
72            return False
73        abs_path = os.path.abspath(os.path.normpath(path))
74        return os.path.exists(abs_path + '/' + '.repo/manifests')
75
76    @staticmethod
77    def get_ohos_version(root:str) -> str:
78        if not BundleCheckTools.is_project(root):
79            return ""
80
81        version_path = os.path.join(root, 'build/version.gni')
82        lines = []
83        with open(version_path, 'r', encoding='utf-8') as version_file:
84            lines = version_file.readlines()
85        for line in lines:
86            line = line.strip()
87            if line and line[0] == '#':
88                continue
89            if 'sdk_version =' in line:
90                match_result = re.match(r'\s*sdk_version\s*=\s*"(\d+\.\d+).*"', line)
91                if match_result:
92                    return match_result.group(1)
93                else:
94                    return ""
95        return ""
96
97    @staticmethod
98    def match_unix_like_name(name:str):
99        '''
100        @func: 检查是否符合 unix_like 命名规范(规则1.1)
101        '''
102        return re.match(r'^[a-z][a-z0-9_]{1,62}$', name)
103
104    @staticmethod
105    def match_bundle_full_name(name:str):
106        '''
107        @func: 检查 bundle.json 第一个 name 字段命名规则。
108        '''
109        return re.match(r'^@[a-z]+/([a-z][a-z0-9_]{1,62})$', name)