• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        with os.fdopen(os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o755), 'w') as file:
68            file.writelines(self.write_buffer)
69
70
71class XmlVersionHeader:
72
73    def __init__(self, xmlversion_json_path):
74        self.read_buffer = []
75        self.write_buffer = []
76        with os.fdopen(os.open(xmlversion_json_path, os.O_RDONLY, 0o755), 'r') as file:
77            self.configs = json.load(file)
78
79    def parse_file(self, file_path):
80        with os.fdopen(os.open(file_path, os.O_RDONLY, 0o755), 'r') as file:
81            self.read_buffer = file.readlines()
82        for item in self.read_buffer:
83            if not item.startswith("#if") and not item.startswith("#define"):
84                self.write_buffer.append(item)
85                continue
86
87            match_object = re.search(r'@\w+@', item)
88            if match_object is None:
89                self.write_buffer.append(item)
90                continue
91            key = match_object.group()
92            index_key = key.replace("@", "")
93            value = ""
94            if index_key in self.configs:
95                value = item.replace(key, self.configs[index_key])
96            else:
97                value = item.replace(key, "0")
98            self.write_buffer.append(value)
99
100    def write_file(self, file_path):
101        with os.fdopen(os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o755), 'w') as file:
102            file.writelines(self.write_buffer)
103
104if __name__ == "__main__":
105    parser = argparse.ArgumentParser()
106    parser.add_argument('--config-input-path', help='input path to config.h')
107    parser.add_argument('--config-path', help='output path to config.h')
108    parser.add_argument('--xmlversion-input-path', help='input path to xmlversion.h')
109    parser.add_argument('--xmlversion-path', help='output path to xmlversion.h')
110    parser.add_argument('--config-json', help='config value for config.h')
111    parser.add_argument('--xmlversion-json', help='config value for xmlversion.h')
112    options = parser.parse_args()
113    config_header = ConfigHeader(options.config_json)
114    config_header.parse_file(options.config_input_path)
115    config_header.write_file(options.config_path)
116
117    xmlversion_header = XmlVersionHeader(options.xmlversion_json)
118    xmlversion_header.parse_file(options.xmlversion_input_path)
119    xmlversion_header.write_file(options.xmlversion_path)
120