• 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 resources.global_var import CURRENT_OHOS_ROOT
22from exceptions.ohos_exception import OHOSException
23from util.io_util import IoUtil
24from containers.status import throw_exception
25
26
27class ComponentUtil():
28
29    @staticmethod
30    def is_in_component_dir(path: str) -> bool:
31        return _recurrent_search_bundle_file(path)[0]
32
33    @staticmethod
34    def is_component_in_product(component_name: str, product_name: str) -> bool:
35        build_configs_path = os.path.join(
36            CURRENT_OHOS_ROOT, 'out', product_name, 'build_configs')
37        if os.path.exists(build_configs_path):
38            for root, dirs, files in os.walk(build_configs_path, topdown=True, followlinks=False):
39                if component_name in dirs:
40                    return True
41        return False
42
43    @staticmethod
44    def get_component_name(path: str) -> str:
45        found_bundle_file, bundle_path = _recurrent_search_bundle_file(path)
46        if found_bundle_file:
47            data = IoUtil.read_json_file(bundle_path)
48            return data['component']['name']
49
50        return ''
51
52    @staticmethod
53    @throw_exception
54    def get_component_module_full_name(out_path: str, component_name: str, module_name: str) -> str:
55        root_path = os.path.join(out_path, "build_configs")
56        target_info = ""
57        module_list = []
58        for file in os.listdir(root_path):
59            if len(target_info):
60                break
61            file_path = os.path.join(root_path, file)
62            if not os.path.isdir(file_path):
63                continue
64            for component in os.listdir(file_path):
65                if os.path.isdir(os.path.join(file_path, component)) and component == component_name:
66                    target_info = IoUtil.read_file(
67                        os.path.join(file_path, component, "BUILD.gn"))
68                    break
69        pattern = re.compile(r'(?<=module_list = )\[([^\[\]]*)\]')
70        results = pattern.findall(target_info)
71        for each_tuple in results:
72            module_list = each_tuple.replace('\n', '').replace(
73                ' ', '').replace('\"', '').split(',')
74        for target_path in module_list:
75            if target_path != '':
76                path, target = target_path.split(":")
77                if target == module_name:
78                    return target_path
79
80        raise OHOSException('You are trying to compile a module {} which do not exists in {} while compiling {}'.format(
81            module_name, component_name, out_path), "4001")
82
83
84def _recurrent_search_bundle_file(path: str):
85    cur_dir = path
86    while cur_dir != CURRENT_OHOS_ROOT:
87        bundle_json = os.path.join(
88            cur_dir, 'bundle.json')
89        if os.path.exists(bundle_json):
90            return True, bundle_json
91        cur_dir = os.path.dirname(cur_dir)
92    return False, ''
93