1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 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 18 19sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 20from scripts.util import file_utils # noqa: E402 21 22 23class BundlePartObj(object): 24 def __init__(self, bundle_config_file): 25 self._build_config_file = bundle_config_file 26 self._loading_config() 27 28 def _loading_config(self): 29 if not os.path.exists(self._build_config_file): 30 raise Exception("file '{}' doesn't exist.".format( 31 self._build_config_file)) 32 self.bundle_info = file_utils.read_json_file(self._build_config_file) 33 if self.bundle_info is None: 34 raise Exception("read file '{}' failed.".format( 35 self._build_config_file)) 36 self._check_format() 37 38 def _check_format(self): 39 _tip_info = "bundle.json info is incorrect in '{}'".format( 40 self._build_config_file) 41 if 'component' not in self.bundle_info: 42 raise Exception("{}, 'component' is required.".format(_tip_info)) 43 _component_info = self.bundle_info.get('component') 44 if 'name' not in _component_info: 45 raise Exception( 46 "{}, 'component.name' is required.".format(_tip_info)) 47 if 'subsystem' not in _component_info: 48 raise Exception( 49 "{}, 'component.subsystem' is required.".format(_tip_info)) 50 if 'build' not in _component_info: 51 raise Exception( 52 "{}, 'component.build' is required.".format(_tip_info)) 53 _bundle_build = _component_info.get('build') 54 if 'sub_component' not in _bundle_build: 55 raise Exception( 56 "{}, 'component.build.sub_component' is required.".format( 57 _tip_info)) 58 59 def to_ohos_build(self): 60 _component_info = self.bundle_info.get('component') 61 _subsystem_name = _component_info.get('subsystem') 62 _part_name = _component_info.get('name') 63 _bundle_build = _component_info.get('build') 64 _ohos_build_info = {} 65 _ohos_build_info['subsystem'] = _subsystem_name 66 _part_info = {} 67 _part_info['module_list'] = _component_info.get('build').get( 68 'sub_component') 69 if 'inner_kits' in _bundle_build: 70 _part_info['inner_kits'] = _bundle_build.get('inner_kits') 71 if 'test' in _bundle_build: 72 _part_info['test_list'] = _bundle_build.get('test') 73 if 'features' in _component_info: 74 _part_info['feature_list'] = _component_info.get('features') 75 if 'syscap' in _component_info: 76 _part_info['system_capabilities'] = _component_info.get('syscap') 77 if 'hisysevent_config' in _component_info: 78 _part_info['hisysevent_config'] = _component_info.get('hisysevent_config') 79 _ohos_build_info['parts'] = {_part_name: _part_info} 80 return _ohos_build_info 81