15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_ciimport json
175f9996aaSopenharmony_ciimport os
185f9996aaSopenharmony_ciimport stat
195f9996aaSopenharmony_ci
205f9996aaSopenharmony_cifrom scripts.util.file_utils import read_json_file
215f9996aaSopenharmony_ci
225f9996aaSopenharmony_ci
235f9996aaSopenharmony_cidef _read_lite_component_configs(file):
245f9996aaSopenharmony_ci    subsystem_name = os.path.basename(file)[:-5]
255f9996aaSopenharmony_ci    configs = {}
265f9996aaSopenharmony_ci    configs['subsystem'] = subsystem_name
275f9996aaSopenharmony_ci    with open(file, 'rb') as fin:
285f9996aaSopenharmony_ci        data = json.load(fin)
295f9996aaSopenharmony_ci        components = data.get('components')
305f9996aaSopenharmony_ci        parts = {}
315f9996aaSopenharmony_ci        for com in components:
325f9996aaSopenharmony_ci            part = {}
335f9996aaSopenharmony_ci            targets = com.get('targets')
345f9996aaSopenharmony_ci            test_targets = []
355f9996aaSopenharmony_ci            non_test_targets = []
365f9996aaSopenharmony_ci            for item in targets:
375f9996aaSopenharmony_ci                target_names = item.strip('"').split(':')
385f9996aaSopenharmony_ci                if len(target_names) > 1 and 'test' in target_names[1]:
395f9996aaSopenharmony_ci                    test_targets.append(item)
405f9996aaSopenharmony_ci                else:
415f9996aaSopenharmony_ci                    non_test_targets.append(item)
425f9996aaSopenharmony_ci            part['module_list'] = non_test_targets
435f9996aaSopenharmony_ci            if test_targets != []:
445f9996aaSopenharmony_ci                part['test_list'] = test_targets
455f9996aaSopenharmony_ci            part_name = com.get('component')
465f9996aaSopenharmony_ci            parts[part_name] = part
475f9996aaSopenharmony_ci        configs['parts'] = parts
485f9996aaSopenharmony_ci    return configs
495f9996aaSopenharmony_ci
505f9996aaSopenharmony_ci
515f9996aaSopenharmony_cidef _save_as_ohos_build(config, ohos_build):
525f9996aaSopenharmony_ci    new_config = json.dumps(config, indent=2, sort_keys=True)
535f9996aaSopenharmony_ci    with os.fdopen(os.open(ohos_build,
545f9996aaSopenharmony_ci                           os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as fout:
555f9996aaSopenharmony_ci        fout.write(new_config)
565f9996aaSopenharmony_ci
575f9996aaSopenharmony_ci
585f9996aaSopenharmony_cidef parse_lite_subsystem_config(lite_components_dir, output_dir,
595f9996aaSopenharmony_ci                                source_root_dir, subsystem_config_file):
605f9996aaSopenharmony_ci    subsystem_infos = read_json_file(subsystem_config_file)
615f9996aaSopenharmony_ci    for root, _, files in os.walk(lite_components_dir):
625f9996aaSopenharmony_ci        for file in files:
635f9996aaSopenharmony_ci            if file[-5:] == '.json':
645f9996aaSopenharmony_ci                configs = _read_lite_component_configs(os.path.join(
655f9996aaSopenharmony_ci                    root, file))
665f9996aaSopenharmony_ci                subsystem_name = configs.get('subsystem')
675f9996aaSopenharmony_ci                ohos_build = os.path.join(
685f9996aaSopenharmony_ci                    output_dir, '{}/ohos.build'.format(subsystem_name))
695f9996aaSopenharmony_ci                os.makedirs(os.path.dirname(ohos_build), exist_ok=True)
705f9996aaSopenharmony_ci                _save_as_ohos_build(configs, ohos_build)
715f9996aaSopenharmony_ci                subsystem_infos[subsystem_name] = {
725f9996aaSopenharmony_ci                    'name':
735f9996aaSopenharmony_ci                    subsystem_name,
745f9996aaSopenharmony_ci                    "path":
755f9996aaSopenharmony_ci                    os.path.relpath(os.path.dirname(ohos_build),
765f9996aaSopenharmony_ci                                    source_root_dir),
775f9996aaSopenharmony_ci                }
785f9996aaSopenharmony_ci    return subsystem_infos
79