13af6ab5fSopenharmony_ci#!/usr/bin/env python3
23af6ab5fSopenharmony_ci# coding=utf-8
33af6ab5fSopenharmony_ci#
43af6ab5fSopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd.
53af6ab5fSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
63af6ab5fSopenharmony_ci# you may not use this file except in compliance with the License.
73af6ab5fSopenharmony_ci# You may obtain a copy of the License at
83af6ab5fSopenharmony_ci#
93af6ab5fSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
103af6ab5fSopenharmony_ci#
113af6ab5fSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
123af6ab5fSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
133af6ab5fSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
143af6ab5fSopenharmony_ci# See the License for the specific language governing permissions and
153af6ab5fSopenharmony_ci# limitations under the License.
163af6ab5fSopenharmony_ci
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci"""This module provides structures, to save some info while parsing headers."""
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_ciimport os
213af6ab5fSopenharmony_ciimport sys
223af6ab5fSopenharmony_cifrom typing import Any, Dict
233af6ab5fSopenharmony_cifrom file_tools import print_to_yaml
243af6ab5fSopenharmony_cifrom log_tools import info_log
253af6ab5fSopenharmony_ci
263af6ab5fSopenharmony_cistatistics: Dict[str, Dict[str, Any]] = {}
273af6ab5fSopenharmony_cicustom_yamls: Dict[str, Dict[str, Any]] = {}
283af6ab5fSopenharmony_ciLIB_GEN_FOLDER = ""
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_cidef init_collections(lib_gen_folder: str) -> None:  # pylint: disable=C
323af6ab5fSopenharmony_ci    global statistics, custom_yamls, LIB_GEN_FOLDER  # pylint: disable=W
333af6ab5fSopenharmony_ci    LIB_GEN_FOLDER = lib_gen_folder
343af6ab5fSopenharmony_ci
353af6ab5fSopenharmony_ci    statistics = {
363af6ab5fSopenharmony_ci        "unreachable": {
373af6ab5fSopenharmony_ci            "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/unreachable.txt"),
383af6ab5fSopenharmony_ci            "collection": set(),
393af6ab5fSopenharmony_ci        },
403af6ab5fSopenharmony_ci        "skip": {
413af6ab5fSopenharmony_ci            "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/skip.txt"),
423af6ab5fSopenharmony_ci            "collection": set()
433af6ab5fSopenharmony_ci        },
443af6ab5fSopenharmony_ci        "generated_yamls": {
453af6ab5fSopenharmony_ci            "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/generated_yamls.txt"),
463af6ab5fSopenharmony_ci            "collection": set(),
473af6ab5fSopenharmony_ci        },
483af6ab5fSopenharmony_ci    }
493af6ab5fSopenharmony_ci
503af6ab5fSopenharmony_ci    custom_yamls = {
513af6ab5fSopenharmony_ci        "allEnums": {
523af6ab5fSopenharmony_ci            "yaml_file": os.path.join(LIB_GEN_FOLDER, "./gen/headers/allEnums.yaml"),
533af6ab5fSopenharmony_ci            "collection": {"enums": []},
543af6ab5fSopenharmony_ci        },
553af6ab5fSopenharmony_ci        "pathsToHeaders": {
563af6ab5fSopenharmony_ci            "yaml_file": os.path.join(LIB_GEN_FOLDER, "./gen/headers/pathsToHeaders.yaml"),
573af6ab5fSopenharmony_ci            "collection": {"paths": []},
583af6ab5fSopenharmony_ci        },
593af6ab5fSopenharmony_ci    }
603af6ab5fSopenharmony_ci
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_cidef add_to_statistics(key: str, data: Any) -> None:
633af6ab5fSopenharmony_ci    if isinstance(statistics[key]["collection"], set):
643af6ab5fSopenharmony_ci        statistics[key]["collection"].add(data)
653af6ab5fSopenharmony_ci    elif isinstance(statistics[key]["collection"], list):
663af6ab5fSopenharmony_ci        statistics[key]["collection"].append(data)
673af6ab5fSopenharmony_ci    else:
683af6ab5fSopenharmony_ci        raise RuntimeError("Unreachable")
693af6ab5fSopenharmony_ci
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_cidef add_to_custom_yamls(yaml_name: str, key: str, data: Any) -> None:
723af6ab5fSopenharmony_ci    custom_yamls[yaml_name]["collection"][key].append(data)
733af6ab5fSopenharmony_ci
743af6ab5fSopenharmony_ci
753af6ab5fSopenharmony_cidef save_custom_yamls() -> None:
763af6ab5fSopenharmony_ci    headers_path = os.path.join(LIB_GEN_FOLDER, "./gen/headers")
773af6ab5fSopenharmony_ci    if not os.path.exists(headers_path):
783af6ab5fSopenharmony_ci        os.makedirs(headers_path)
793af6ab5fSopenharmony_ci
803af6ab5fSopenharmony_ci    for _, value in custom_yamls.items():
813af6ab5fSopenharmony_ci        yaml_file = value["yaml_file"]
823af6ab5fSopenharmony_ci        print_to_yaml(yaml_file, value["collection"])
833af6ab5fSopenharmony_ci        info_log(f"Saved custom yaml: '{yaml_file}'")
843af6ab5fSopenharmony_ci
853af6ab5fSopenharmony_ci        statistics["generated_yamls"]["collection"].add(
863af6ab5fSopenharmony_ci            os.path.basename(yaml_file)
873af6ab5fSopenharmony_ci        )
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ci
903af6ab5fSopenharmony_cidef save_statistics() -> None:
913af6ab5fSopenharmony_ci    logs_path = os.path.join(LIB_GEN_FOLDER, "./gen/logs")
923af6ab5fSopenharmony_ci    if not os.path.exists(logs_path):
933af6ab5fSopenharmony_ci        os.makedirs(logs_path)
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_ci    info_log(f"Parsed {len(custom_yamls['pathsToHeaders']['collection']['paths'])} / {len(sys.argv[3:])} headers.")
963af6ab5fSopenharmony_ci
973af6ab5fSopenharmony_ci    for _, value in statistics.items():
983af6ab5fSopenharmony_ci        with os.fdopen(os.open(value["log_file"], os.O_WRONLY | os.O_CREAT, mode=511), "w", encoding="utf-8") as f:
993af6ab5fSopenharmony_ci            for item in value["collection"]:
1003af6ab5fSopenharmony_ci                f.write(f"{item}\n")
101