• 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 resources.global_var import COMPONENTS_PATH_DIR
23from exceptions.ohos_exception import OHOSException
24from util.io_util import IoUtil
25from containers.status import throw_exception
26
27
28class ComponentUtil():
29
30    @staticmethod
31    def is_in_component_dir(path: str) -> bool:
32        return _recurrent_search_bundle_file(path)[0]
33
34    @staticmethod
35    def is_component_in_product(component_name: str, product_name: str) -> bool:
36        build_configs_path = os.path.join(
37            CURRENT_OHOS_ROOT, 'out', product_name, 'build_configs')
38        if os.path.exists(build_configs_path):
39            for root, dirs, files in os.walk(build_configs_path, topdown=True, followlinks=False):
40                if component_name in dirs:
41                    return True
42        return False
43
44    @staticmethod
45    def get_component_name(path: str) -> str:
46        found_bundle_file, bundle_path = _recurrent_search_bundle_file(path)
47        if found_bundle_file:
48            data = IoUtil.read_json_file(bundle_path)
49            return data['component']['name']
50
51        return ''
52
53    @staticmethod
54    def get_component(path: str) -> str:
55        found_bundle_file, bundle_path = _recurrent_search_bundle_file(path)
56        if found_bundle_file:
57            data = IoUtil.read_json_file(bundle_path)
58            return data['component']['name'] , os.path.dirname(bundle_path)
59
60        return '' , ''
61
62    @staticmethod
63    def get_default_deps(variant: str) -> str:
64        default_deps_path = os.path.join(
65            CURRENT_OHOS_ROOT, 'build', 'indep_configs', 'variants', variant, 'default_deps.json')
66
67        return default_deps_path
68
69    @staticmethod
70    @throw_exception
71    def get_component_module_full_name(out_path: str, component_name: str, module_name: str) -> str:
72        root_path = os.path.join(out_path, "build_configs")
73        target_info = ""
74        module_list = []
75        for file in os.listdir(root_path):
76            if len(target_info):
77                break
78            file_path = os.path.join(root_path, file)
79            if not os.path.isdir(file_path):
80                continue
81            for component in os.listdir(file_path):
82                if os.path.isdir(os.path.join(file_path, component)) and component == component_name:
83                    target_info = IoUtil.read_file(
84                        os.path.join(file_path, component, "BUILD.gn"))
85                    break
86        pattern = re.compile(r'(?<=module_list = )\[([^\[\]]*)\]')
87        results = pattern.findall(target_info)
88        for each_tuple in results:
89            module_list = each_tuple.replace('\n', '').replace(
90                ' ', '').replace('\"', '').split(',')
91        for target_path in module_list:
92            if target_path != '':
93                path, target = target_path.split(":")
94                if target == module_name:
95                    return target_path
96
97        raise OHOSException('You are trying to compile a module {} which do not exists in {} while compiling {}'.format(
98            module_name, component_name, out_path), "4001")
99
100    @staticmethod
101    def search_bundle_file(component_name: str):
102        all_bundle_path = get_all_bundle_path(CURRENT_OHOS_ROOT)
103        return all_bundle_path.get(component_name)
104
105def _recurrent_search_bundle_file(path: str):
106    cur_dir = path
107    while cur_dir != CURRENT_OHOS_ROOT:
108        bundle_json = os.path.join(
109            cur_dir, 'bundle.json')
110        if os.path.exists(bundle_json):
111            return True, bundle_json
112        cur_dir = os.path.dirname(cur_dir)
113    return False, ''
114
115def get_all_bundle_path(path):
116    if os.path.exists(COMPONENTS_PATH_DIR):
117        return IoUtil.read_json_file(COMPONENTS_PATH_DIR)
118    bundles_path = {}
119    for root, dirnames, filenames in os.walk(path):
120        if root == os.path.join(path, "out") or root == os.path.join(path, ".repo"):
121            continue
122        for filename in filenames:
123            if filename == "bundle.json":
124                bundle_json = os.path.join(root, filename)
125                data = IoUtil.read_json_file(bundle_json)
126                bundles_path = process_bundle_path(bundle_json, bundles_path, data)
127    IoUtil.dump_json_file(COMPONENTS_PATH_DIR, bundles_path)
128    return bundles_path
129
130
131def process_bundle_path(bundle_json, bundles_path, data):
132    if data.get("component") and data.get("component").get("name"):
133        bundles_path[data["component"]["name"]] = os.path.dirname(bundle_json)
134    return bundles_path