1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2022 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import argparse 20import json 21import os 22import re 23 24 25class ConfigHeader: 26 27 def __init__(self, config_json_path): 28 self.read_buffer = [] 29 self.write_buffer = [] 30 with os.fdopen(os.open(config_json_path, os.O_RDONLY, 0o755), 'r') as file: 31 self.configs = json.load(file) 32 33 def parse_file(self, file_path): 34 with os.fdopen(os.open(file_path, os.O_RDONLY, 0o755), 'r') as file: 35 self.read_buffer = file.readlines() 36 for item in self.read_buffer: 37 if not item.startswith("#cmakedefine") and not item.startswith("#define"): 38 self.write_buffer.append(item) 39 continue 40 41 value = item 42 if item.startswith("#cmakedefine"): 43 value = item.replace("#cmakedefine", "#define") 44 match_object = re.search(r' \w+ ', value) 45 if match_object is None: 46 self.write_buffer.append("/* " + item + " */") 47 continue 48 key = match_object.group() 49 key_index = key.replace(" ", "") 50 if key_index not in self.configs: 51 self.write_buffer.append("/* " + item + " */") 52 continue 53 54 match_object = re.search(r'@\w+@', value) 55 if match_object is None: 56 self.write_buffer.append(value) 57 continue 58 key = match_object.group() 59 key_index = key.replace("@", "") 60 if key_index not in self.configs: 61 self.write_buffer.append("/* " + item + " */") 62 continue 63 value = value.replace(key, self.configs[key_index]) 64 self.write_buffer.append(value) 65 66 def write_file(self, file_path): 67 self.write_buffer.append("#define HAVE_SNPRINTF 1\n") 68 with os.fdopen(os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o755), 'w') as file: 69 file.truncate(0) 70 file.writelines(self.write_buffer) 71 72 73class XmlVersionHeader: 74 75 def __init__(self, xmlversion_json_path): 76 self.read_buffer = [] 77 self.write_buffer = [] 78 with os.fdopen(os.open(xmlversion_json_path, os.O_RDONLY, 0o755), 'r') as file: 79 self.configs = json.load(file) 80 81 def parse_file(self, file_path): 82 with os.fdopen(os.open(file_path, os.O_RDONLY, 0o755), 'r') as file: 83 self.read_buffer = file.readlines() 84 for item in self.read_buffer: 85 if not item.startswith("#if") and not item.startswith("#define"): 86 self.write_buffer.append(item) 87 continue 88 89 match_object = re.search(r'@\w+@', item) 90 if match_object is None: 91 self.write_buffer.append(item) 92 continue 93 key = match_object.group() 94 index_key = key.replace("@", "") 95 value = "" 96 if index_key in self.configs: 97 value = item.replace(key, self.configs[index_key]) 98 else: 99 value = item.replace(key, "0") 100 self.write_buffer.append(value) 101 102 def write_file(self, file_path): 103 with os.fdopen(os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o755), 'w') as file: 104 file.truncate(0) 105 file.writelines(self.write_buffer) 106 107if __name__ == "__main__": 108 parser = argparse.ArgumentParser() 109 parser.add_argument('--config-input-path', help='input path to config.h') 110 parser.add_argument('--config-path', help='output path to config.h') 111 parser.add_argument('--xmlversion-input-path', help='input path to xmlversion.h') 112 parser.add_argument('--xmlversion-path', help='output path to xmlversion.h') 113 parser.add_argument('--config-json', help='config value for config.h') 114 parser.add_argument('--xmlversion-json', help='config value for xmlversion.h') 115 options = parser.parse_args() 116 config_header = ConfigHeader(options.config_json) 117 config_header.parse_file(options.config_input_path) 118 config_header.write_file(options.config_path) 119 120 xmlversion_header = XmlVersionHeader(options.xmlversion_json) 121 xmlversion_header.parse_file(options.xmlversion_input_path) 122 xmlversion_header.write_file(options.xmlversion_path) 123