1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: metadata.json generate 18""" 19 20import argparse 21import os 22import json 23 24 25def parse_args(): 26 parser = argparse.ArgumentParser() 27 parser.add_argument('--enum', 28 type=str, 29 required=True, 30 help='type_enum.json file path') 31 parser.add_argument('--metadata', 32 type=str, 33 required=True, 34 help='metadata folder path') 35 parser.add_argument('--output', 36 type=str, 37 required=True, 38 help='generated metadata.json file path') 39 parser.add_argument('--version', 40 type=str, 41 required=True, 42 help='version number') 43 args = parser.parse_args() 44 return args 45 46 47def parse_type_enums(file_path): 48 with open(file_path, 'r') as f: 49 type_enums = json.load(f) 50 return type_enums["type_enum"] 51 52 53def parse_type_list(dir_path): 54 type_list, type_layout = [], {} 55 for file_name in os.listdir(dir_path): 56 if not os.path.isfile(os.path.join(dir_path, file_name)) or not _is_metadata_file(file_name): 57 continue 58 with open(os.path.join(dir_path, file_name), 'r') as f: 59 metadata = json.load(f) 60 if file_name.endswith("_layout.json"): 61 type_layout["Dictionary_layout"] = metadata 62 elif file_name == "type_range.json": 63 type_layout["Type_range"] = metadata 64 else: 65 type_list.append(metadata) 66 return type_list, type_layout 67 68 69def _is_metadata_file(file_name): 70 return file_name.endswith(".json") and file_name != "type_enums.json" 71 72 73def generate_metadata_json(enum_list, metadata_list, type_layout, file_path, version): 74 metadata_dict = {"type_enum": enum_list, "type_list": [], "type_layout": type_layout, "version": version} 75 for metadata in metadata_list: 76 metadata_dict["type_list"].append(metadata) 77 with os.fdopen(os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o777), 'w+') as f: 78 json_str = json.dumps(metadata_dict) 79 f.write(json_str) 80 81 82if __name__ == '__main__': 83 arguments = parse_args() 84 type_enum_list = parse_type_enums(arguments.enum) 85 type_metadata_list, type_layout_list = parse_type_list(arguments.metadata) 86 generate_metadata_json(type_enum_list, type_metadata_list, type_layout_list, arguments.output, arguments.version) 87