• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 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 sys
17import os
18from containers.status import throw_exception
19from exceptions.ohos_exception import OHOSException
20sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
21from scripts.util import file_utils  # noqa: E402
22
23
24class BundlePartObj(object):
25    def __init__(self, bundle_config_file, exclusion_modules_config_file, load_test_config):
26        self._build_config_file = bundle_config_file
27        self._exclusion_modules_config_file = exclusion_modules_config_file
28        self._load_test_config = load_test_config
29        self._loading_config()
30
31    @throw_exception
32    def _loading_config(self):
33        if not os.path.exists(self._build_config_file):
34            raise OHOSException("file '{}' doesn't exist.".format(
35                self._build_config_file), "2011")
36        self.bundle_info = file_utils.read_json_file(self._build_config_file)
37        if self.bundle_info is None:
38            raise OHOSException("read file '{}' failed.".format(
39                self._build_config_file), "2011")
40        self._check_format()
41        self.exclusion_modules_info = file_utils.read_json_file(
42            self._exclusion_modules_config_file)
43
44    @throw_exception
45    def _check_format(self):
46        _tip_info = "bundle.json info is incorrect in '{}'".format(
47            self._build_config_file)
48        if 'component' not in self.bundle_info:
49            raise OHOSException(
50                "{}, 'component' is required.".format(_tip_info), "2011")
51        _component_info = self.bundle_info.get('component')
52        if 'name' not in _component_info:
53            raise OHOSException(
54                "{}, 'component.name' is required.".format(_tip_info), "2011")
55        if 'subsystem' not in _component_info:
56            raise OHOSException(
57                "{}, 'component.subsystem' is required.".format(_tip_info), "2011")
58        if 'build' not in _component_info:
59            raise OHOSException(
60                "{}, 'component.build' is required.".format(_tip_info), "2011")
61        _bundle_build = _component_info.get('build')
62        if 'sub_component' not in _bundle_build and 'group_type' not in _bundle_build \
63                and 'modules' not in _bundle_build:
64            raise OHOSException(
65                "{}, 'component.build.sub_component','component.build.group_type' or \
66                'component.build.modules' is required.".format(_tip_info), "2011")
67        if 'group_type' in _bundle_build:
68            group_list = ['base_group', 'fwk_group', 'service_group']
69            _module_groups = _bundle_build.get('group_type')
70            for _group_type, _module_list in _module_groups.items():
71                if _group_type not in group_list:
72                    raise OHOSException(
73                        "{}, incorrect group type".format(_tip_info), "2011")
74
75    def to_ohos_build(self):
76        _component_info = self.bundle_info.get('component')
77        _subsystem_name = _component_info.get('subsystem')
78        _part_name = _component_info.get('name')
79        _bundle_build = _component_info.get('build')
80        _exclusion_modules_info = self.exclusion_modules_info
81        _ohos_build_info = {}
82        _ohos_build_info['subsystem'] = _subsystem_name
83        _part_info = {}
84        module_list = []
85        if _component_info.get('build').__contains__('sub_component'):
86            _part_info['module_list'] = _component_info.get('build').get(
87                'sub_component')
88        elif _component_info.get('build').__contains__('modules'):
89            _part_info['module_list'] = _component_info.get(
90                'build').get('modules')
91        elif _component_info.get('build').__contains__('group_type'):
92            _module_groups = _component_info.get('build').get('group_type')
93            for _group_type, _module_list in _module_groups.items():
94                _key = '{}:{}'.format(_subsystem_name, _part_name)
95                if not _exclusion_modules_info.get(_key):
96                    module_list.extend(_module_list)
97                elif _group_type not in _exclusion_modules_info.get(_key):
98                    module_list.extend(_module_list)
99            _part_info['module_list'] = module_list
100        if 'inner_kits' in _bundle_build:
101            _part_info['inner_kits'] = _bundle_build.get('inner_kits')
102        elif 'inner_api' in _bundle_build:
103            _part_info['inner_kits'] = _bundle_build.get('inner_api')
104        if 'test' in _bundle_build and self._load_test_config:
105            _part_info['test_list'] = _bundle_build.get('test')
106        if 'features' in _component_info:
107            _part_info['feature_list'] = _component_info.get('features')
108        if 'syscap' in _component_info:
109            _part_info['system_capabilities'] = _component_info.get('syscap')
110        if 'hisysevent_config' in _component_info:
111            _part_info['hisysevent_config'] = _component_info.get(
112                'hisysevent_config')
113        _part_info['part_deps'] = _component_info.get('deps', {})
114        _part_info['part_deps']['build_config_file'] = self._build_config_file
115        _ohos_build_info['parts'] = {_part_name: _part_info}
116        return _ohos_build_info
117