1#!/usr/bin/env python 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 json 17import os 18import stat 19 20from scripts.util.file_utils import read_json_file 21 22 23def _read_lite_component_configs(file): 24 subsystem_name = os.path.basename(file)[:-5] 25 configs = {} 26 configs['subsystem'] = subsystem_name 27 with open(file, 'rb') as fin: 28 data = json.load(fin) 29 components = data.get('components') 30 parts = {} 31 for com in components: 32 part = {} 33 targets = com.get('targets') 34 test_targets = [] 35 non_test_targets = [] 36 for item in targets: 37 target_names = item.strip('"').split(':') 38 if len(target_names) > 1 and 'test' in target_names[1]: 39 test_targets.append(item) 40 else: 41 non_test_targets.append(item) 42 part['module_list'] = non_test_targets 43 if test_targets != []: 44 part['test_list'] = test_targets 45 part_name = com.get('component') 46 parts[part_name] = part 47 configs['parts'] = parts 48 return configs 49 50 51def _save_as_ohos_build(config, ohos_build): 52 new_config = json.dumps(config, indent=2, sort_keys=True) 53 with os.fdopen(os.open(ohos_build, 54 os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as fout: 55 fout.write(new_config) 56 57 58def parse_lite_subsystem_config(lite_components_dir, output_dir, 59 source_root_dir, subsystem_config_file): 60 subsystem_infos = read_json_file(subsystem_config_file) 61 for root, _, files in os.walk(lite_components_dir): 62 for file in files: 63 if file[-5:] == '.json': 64 configs = _read_lite_component_configs(os.path.join( 65 root, file)) 66 subsystem_name = configs.get('subsystem') 67 ohos_build = os.path.join( 68 output_dir, '{}/ohos.build'.format(subsystem_name)) 69 os.makedirs(os.path.dirname(ohos_build), exist_ok=True) 70 _save_as_ohos_build(configs, ohos_build) 71 subsystem_infos[subsystem_name] = { 72 'name': 73 subsystem_name, 74 "path": 75 os.path.relpath(os.path.dirname(ohos_build), 76 source_root_dir), 77 } 78 return subsystem_infos 79