15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2021 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 sys
175f9996aaSopenharmony_ciimport argparse
185f9996aaSopenharmony_ciimport os
195f9996aaSopenharmony_ciimport shutil
205f9996aaSopenharmony_ciimport re
215f9996aaSopenharmony_cisys.path.append(
225f9996aaSopenharmony_ci    os.path.dirname(os.path.dirname(os.path.dirname(
235f9996aaSopenharmony_ci        os.path.abspath(__file__)))))
245f9996aaSopenharmony_cifrom scripts.util.file_utils import read_json_file, write_json_file  # noqa: E402
255f9996aaSopenharmony_cifrom scripts.util import build_utils  # noqa: E402
265f9996aaSopenharmony_ci
275f9996aaSopenharmony_ci
285f9996aaSopenharmony_cidef _get_label_name(label):
295f9996aaSopenharmony_ci    return re.split('[:|()]', label)[1]
305f9996aaSopenharmony_ci
315f9996aaSopenharmony_ci
325f9996aaSopenharmony_cidef _read_notice_info_file(subsystem_notice_info_dir: str, platform_base_dir: str):
335f9996aaSopenharmony_ci    subsystem_notice_info_list = []
345f9996aaSopenharmony_ci    subsystem_notice_files = []
355f9996aaSopenharmony_ci    _file_list = os.listdir(subsystem_notice_info_dir)
365f9996aaSopenharmony_ci    for _info_file in _file_list:
375f9996aaSopenharmony_ci        _info_file_path = os.path.join(subsystem_notice_info_dir, _info_file)
385f9996aaSopenharmony_ci        if not os.path.isfile(_info_file_path):
395f9996aaSopenharmony_ci            continue
405f9996aaSopenharmony_ci        if not _info_file.endswith('_notice_file'):
415f9996aaSopenharmony_ci            continue
425f9996aaSopenharmony_ci        subsystem_notice_info_list.append(_info_file_path)
435f9996aaSopenharmony_ci        _nf_src_list = read_json_file(_info_file_path)
445f9996aaSopenharmony_ci        for _nf_src in _nf_src_list:
455f9996aaSopenharmony_ci            _dest = os.path.join(platform_base_dir, os.path.dirname(_nf_src))
465f9996aaSopenharmony_ci            if not os.path.exists(_dest):
475f9996aaSopenharmony_ci                os.makedirs(_dest, exist_ok=True)
485f9996aaSopenharmony_ci            shutil.copy2(_nf_src, _dest)
495f9996aaSopenharmony_ci            _dest_file = os.path.relpath(
505f9996aaSopenharmony_ci                os.path.join(_dest, os.path.basename(_nf_src)))
515f9996aaSopenharmony_ci            subsystem_notice_files.append(_dest_file)
525f9996aaSopenharmony_ci    return subsystem_notice_info_list, subsystem_notice_files
535f9996aaSopenharmony_ci
545f9996aaSopenharmony_ci
555f9996aaSopenharmony_cidef get_notice_info(system_install_info_file: str, notice_info_dir: str,
565f9996aaSopenharmony_ci                    platform_base_dir: str):
575f9996aaSopenharmony_ci    system_install_info = read_json_file(system_install_info_file)
585f9996aaSopenharmony_ci    if system_install_info is None:
595f9996aaSopenharmony_ci        raise Exception(
605f9996aaSopenharmony_ci            "read file '{}' failed.".format(system_install_info_file))
615f9996aaSopenharmony_ci
625f9996aaSopenharmony_ci    src_subsystem_list = []
635f9996aaSopenharmony_ci    for subsystem_info in system_install_info:
645f9996aaSopenharmony_ci        if subsystem_info.get('is_source') is False:
655f9996aaSopenharmony_ci            continue
665f9996aaSopenharmony_ci        part_label = subsystem_info.get('part_label')
675f9996aaSopenharmony_ci        part_name = _get_label_name(part_label)
685f9996aaSopenharmony_ci        src_subsystem_list.append(part_name)
695f9996aaSopenharmony_ci
705f9996aaSopenharmony_ci    notice_info_file_list = []
715f9996aaSopenharmony_ci    system_notice_files = []
725f9996aaSopenharmony_ci    for subsystem_name in src_subsystem_list:
735f9996aaSopenharmony_ci        subsystem_notice_info_dir = os.path.join(notice_info_dir,
745f9996aaSopenharmony_ci                                                 subsystem_name)
755f9996aaSopenharmony_ci        if not os.path.exists(subsystem_notice_info_dir):
765f9996aaSopenharmony_ci            continue
775f9996aaSopenharmony_ci        subsystem_notice_info_list, subsystem_notice_files = _read_notice_info_file(
785f9996aaSopenharmony_ci            subsystem_notice_info_dir, platform_base_dir)
795f9996aaSopenharmony_ci        notice_info_file_list.extend(subsystem_notice_info_list)
805f9996aaSopenharmony_ci        system_notice_files.extend(subsystem_notice_files)
815f9996aaSopenharmony_ci    return notice_info_file_list, system_notice_files
825f9996aaSopenharmony_ci
835f9996aaSopenharmony_ci
845f9996aaSopenharmony_cidef main():
855f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
865f9996aaSopenharmony_ci    parser.add_argument('--system-install-info-file', required=True)
875f9996aaSopenharmony_ci    parser.add_argument('--notice-file-info-dir', required=True)
885f9996aaSopenharmony_ci    parser.add_argument('--platform-base-dir', required=True)
895f9996aaSopenharmony_ci    parser.add_argument('--output-file', required=True)
905f9996aaSopenharmony_ci    parser.add_argument('--depfile', required=False)
915f9996aaSopenharmony_ci    args = parser.parse_args()
925f9996aaSopenharmony_ci
935f9996aaSopenharmony_ci    _dep_files = [args.system_install_info_file]
945f9996aaSopenharmony_ci    notice_info_file_list, system_notice_files = get_notice_info(
955f9996aaSopenharmony_ci        args.system_install_info_file, args.notice_file_info_dir,
965f9996aaSopenharmony_ci        args.platform_base_dir)
975f9996aaSopenharmony_ci
985f9996aaSopenharmony_ci    _dep_files.extend(notice_info_file_list)
995f9996aaSopenharmony_ci    write_json_file(args.output_file, system_notice_files)
1005f9996aaSopenharmony_ci
1015f9996aaSopenharmony_ci    if args.depfile:
1025f9996aaSopenharmony_ci        list.sort(_dep_files)
1035f9996aaSopenharmony_ci        build_utils.write_depfile(args.depfile,
1045f9996aaSopenharmony_ci                                 args.output_file,
1055f9996aaSopenharmony_ci                                 _dep_files,
1065f9996aaSopenharmony_ci                                 add_pydeps=False)
1075f9996aaSopenharmony_ci    return 0
1085f9996aaSopenharmony_ci
1095f9996aaSopenharmony_ci
1105f9996aaSopenharmony_ciif __name__ == '__main__':
1115f9996aaSopenharmony_ci    sys.exit(main())
112