1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18"""This module provides structures, to save some info while parsing headers.""" 19 20import os 21import sys 22from typing import Any, Dict 23from file_tools import print_to_yaml 24from log_tools import info_log 25 26statistics: Dict[str, Dict[str, Any]] = {} 27custom_yamls: Dict[str, Dict[str, Any]] = {} 28LIB_GEN_FOLDER = "" 29 30 31def init_collections(lib_gen_folder: str) -> None: # pylint: disable=C 32 global statistics, custom_yamls, LIB_GEN_FOLDER # pylint: disable=W 33 LIB_GEN_FOLDER = lib_gen_folder 34 35 statistics = { 36 "unreachable": { 37 "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/unreachable.txt"), 38 "collection": set(), 39 }, 40 "skip": { 41 "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/skip.txt"), 42 "collection": set() 43 }, 44 "generated_yamls": { 45 "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/generated_yamls.txt"), 46 "collection": set(), 47 }, 48 } 49 50 custom_yamls = { 51 "allEnums": { 52 "yaml_file": os.path.join(LIB_GEN_FOLDER, "./gen/headers/allEnums.yaml"), 53 "collection": {"enums": []}, 54 }, 55 "pathsToHeaders": { 56 "yaml_file": os.path.join(LIB_GEN_FOLDER, "./gen/headers/pathsToHeaders.yaml"), 57 "collection": {"paths": []}, 58 }, 59 } 60 61 62def add_to_statistics(key: str, data: Any) -> None: 63 if isinstance(statistics[key]["collection"], set): 64 statistics[key]["collection"].add(data) 65 elif isinstance(statistics[key]["collection"], list): 66 statistics[key]["collection"].append(data) 67 else: 68 raise RuntimeError("Unreachable") 69 70 71def add_to_custom_yamls(yaml_name: str, key: str, data: Any) -> None: 72 custom_yamls[yaml_name]["collection"][key].append(data) 73 74 75def save_custom_yamls() -> None: 76 headers_path = os.path.join(LIB_GEN_FOLDER, "./gen/headers") 77 if not os.path.exists(headers_path): 78 os.makedirs(headers_path) 79 80 for _, value in custom_yamls.items(): 81 yaml_file = value["yaml_file"] 82 print_to_yaml(yaml_file, value["collection"]) 83 info_log(f"Saved custom yaml: '{yaml_file}'") 84 85 statistics["generated_yamls"]["collection"].add( 86 os.path.basename(yaml_file) 87 ) 88 89 90def save_statistics() -> None: 91 logs_path = os.path.join(LIB_GEN_FOLDER, "./gen/logs") 92 if not os.path.exists(logs_path): 93 os.makedirs(logs_path) 94 95 info_log(f"Parsed {len(custom_yamls['pathsToHeaders']['collection']['paths'])} / {len(sys.argv[3:])} headers.") 96 97 for _, value in statistics.items(): 98 with os.fdopen(os.open(value["log_file"], os.O_WRONLY | os.O_CREAT, mode=511), "w", encoding="utf-8") as f: 99 for item in value["collection"]: 100 f.write(f"{item}\n") 101