2022-04-28 10:53:40 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#
|
|
|
|
# Copyright (c) 2022 Huawei Device Co., Ltd.
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2022-09-16 07:48:53 +00:00
|
|
|
|
2022-09-09 07:43:23 +00:00
|
|
|
def decode_cfg_line(data):
|
2022-04-28 10:53:40 +00:00
|
|
|
data.replace('\n', '').replace('\r', '')
|
|
|
|
data = data.strip()
|
|
|
|
if (len(data) == 0 or data[0] == '#'):
|
|
|
|
return "", ""
|
|
|
|
strs = data.split('=')
|
|
|
|
if len(strs) <= 1:
|
|
|
|
return "", ""
|
|
|
|
return strs[0].strip(), strs[1].strip()
|
|
|
|
|
2022-09-16 07:48:53 +00:00
|
|
|
|
2022-09-09 07:43:23 +00:00
|
|
|
def get_param_from_cfg(cfg_name):
|
2022-09-16 07:48:53 +00:00
|
|
|
data_dict = {}
|
2022-09-09 07:43:23 +00:00
|
|
|
with open(cfg_name) as afile:
|
2022-04-28 10:53:40 +00:00
|
|
|
data = afile.readline()
|
2022-05-25 04:41:36 +00:00
|
|
|
while data:
|
2022-09-09 07:43:23 +00:00
|
|
|
name, value = decode_cfg_line(data)
|
2022-04-28 10:53:40 +00:00
|
|
|
if len(name) != 0 and len(value) != 0:
|
2022-09-16 07:48:53 +00:00
|
|
|
data_dict[name] = value
|
|
|
|
print("sample file name={%s %s}" % (name, value))
|
2022-04-28 10:53:40 +00:00
|
|
|
data = afile.readline()
|
2022-09-16 07:48:53 +00:00
|
|
|
return data_dict
|
|
|
|
|
2022-04-28 10:53:40 +00:00
|
|
|
|
2022-09-09 07:43:23 +00:00
|
|
|
def decode_code_line(data):
|
2022-04-28 10:53:40 +00:00
|
|
|
data.replace('\n', '').replace('\r', '')
|
|
|
|
data = data.strip()
|
|
|
|
if (not data.startswith("PARAM_MAP")):
|
2022-05-25 04:41:36 +00:00
|
|
|
return "", ""
|
2022-09-09 07:43:23 +00:00
|
|
|
data_len = len(data)
|
|
|
|
data = data[len("PARAM_MAP") + 1 : data_len - 1]
|
2022-04-28 10:53:40 +00:00
|
|
|
data = data.strip()
|
|
|
|
strs = data.split(',')
|
|
|
|
if len(strs) <= 1:
|
|
|
|
return "", ""
|
2022-09-16 07:48:53 +00:00
|
|
|
return strs[0].strip(), data[len(strs[0]) + 1:].strip()
|
|
|
|
|
2022-04-28 10:53:40 +00:00
|
|
|
|
2022-09-09 07:43:23 +00:00
|
|
|
def get_param_from_c_code(code_name):
|
2022-09-16 07:48:53 +00:00
|
|
|
data_dict = {}
|
2022-09-09 07:43:23 +00:00
|
|
|
with open(code_name, "r+") as afile:
|
2022-04-28 10:53:40 +00:00
|
|
|
data = afile.readline()
|
2022-05-25 04:41:36 +00:00
|
|
|
while data:
|
2022-09-09 07:43:23 +00:00
|
|
|
name, value = decode_code_line(data)
|
2022-04-28 10:53:40 +00:00
|
|
|
if len(name) != 0 and len(value) != 0:
|
2022-09-16 07:48:53 +00:00
|
|
|
data_dict[name] = value
|
2022-04-28 10:53:40 +00:00
|
|
|
data = afile.readline()
|
2022-06-01 09:58:06 +00:00
|
|
|
afile.truncate(0)
|
2022-09-16 07:48:53 +00:00
|
|
|
return data_dict
|
2022-04-28 10:53:40 +00:00
|
|
|
|
2022-09-16 07:48:53 +00:00
|
|
|
|
|
|
|
def write_map_to_code(code_name, data_dict):
|
2022-04-28 10:53:40 +00:00
|
|
|
try:
|
2022-09-09 07:43:23 +00:00
|
|
|
with open(code_name, "w") as f:
|
2022-08-23 05:01:21 +00:00
|
|
|
# start with 0
|
|
|
|
f.seek(0)
|
|
|
|
# write file header
|
|
|
|
f.write('#ifndef PARAM_LITE_DEF_CFG_' + os.linesep)
|
|
|
|
f.write('#define PARAM_LITE_DEF_CFG_' + os.linesep)
|
|
|
|
f.write('#include <stdint.h>' + os.linesep + os.linesep)
|
|
|
|
f.write('#ifdef __cplusplus' + os.linesep)
|
|
|
|
f.write('#if __cplusplus' + os.linesep)
|
|
|
|
f.write('extern "C" {' + os.linesep)
|
|
|
|
f.write('#endif' + os.linesep)
|
|
|
|
f.write('#endif' + os.linesep + os.linesep)
|
|
|
|
|
|
|
|
#define struct
|
|
|
|
f.write('typedef struct Node_ {' + os.linesep)
|
|
|
|
f.write(' const char *name;' + os.linesep)
|
|
|
|
f.write(' const char *value;' + os.linesep)
|
|
|
|
f.write('} Node;' + os.linesep + os.linesep)
|
|
|
|
f.write('#define PARAM_MAP(name, value) {(const char *)#name, (const char *)#value},')
|
|
|
|
f.write(os.linesep + os.linesep)
|
|
|
|
# write data
|
|
|
|
f.write('static Node g_paramDefCfgNodes[] = {' + os.linesep)
|
2022-09-16 07:48:53 +00:00
|
|
|
for name, value in data_dict.items():
|
2022-08-23 05:01:21 +00:00
|
|
|
if (value.startswith("\"")):
|
2022-09-16 07:48:53 +00:00
|
|
|
tmp_str = " PARAM_MAP({0}, {1})".format(name, value)
|
|
|
|
f.write(tmp_str + os.linesep)
|
2022-08-23 05:01:21 +00:00
|
|
|
else:
|
2022-09-16 07:48:53 +00:00
|
|
|
tmp_str = " PARAM_MAP({0}, \"{1}\")".format(name, value)
|
|
|
|
f.write(tmp_str + os.linesep)
|
2022-08-23 05:01:21 +00:00
|
|
|
f.write('};' + os.linesep + os.linesep)
|
|
|
|
|
|
|
|
#end
|
|
|
|
f.write('#ifdef __cplusplus' + os.linesep)
|
|
|
|
f.write('#if __cplusplus' + os.linesep)
|
|
|
|
f.write('}' + os.linesep)
|
|
|
|
f.write('#endif' + os.linesep)
|
|
|
|
f.write('#endif' + os.linesep)
|
|
|
|
f.write('#endif // PARAM_LITE_DEF_CFG_' + os.linesep)
|
|
|
|
f.write(os.linesep)
|
|
|
|
f.truncate()
|
2022-04-28 10:53:40 +00:00
|
|
|
except IOError:
|
2022-09-16 07:48:53 +00:00
|
|
|
print("Error: open or write file %s fail" % {code_name})
|
2022-04-28 10:53:40 +00:00
|
|
|
return 0
|
|
|
|
|
2022-09-16 07:48:53 +00:00
|
|
|
|
|
|
|
def add_to_code_dict(code_dict, cfg_dict, high=True):
|
2022-09-09 07:43:23 +00:00
|
|
|
for name, value in cfg_dict.items():
|
2022-04-28 10:53:40 +00:00
|
|
|
# check if name exit
|
2022-09-16 07:48:53 +00:00
|
|
|
has_key = name in code_dict
|
2022-09-09 07:43:23 +00:00
|
|
|
if has_key and high:
|
|
|
|
code_dict[name] = value
|
|
|
|
elif not has_key:
|
|
|
|
code_dict[name] = value
|
|
|
|
return code_dict
|
2022-05-25 04:41:36 +00:00
|
|
|
|
2022-09-16 07:48:53 +00:00
|
|
|
|
2022-04-28 10:53:40 +00:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='A common change param.para file to h.')
|
|
|
|
parser.add_argument(
|
|
|
|
'--source',
|
2022-06-30 09:42:27 +00:00
|
|
|
action='append',
|
2022-04-28 10:53:40 +00:00
|
|
|
help='The source to change.',
|
|
|
|
required=True)
|
|
|
|
parser.add_argument(
|
|
|
|
'--dest_dir',
|
|
|
|
help='Path that the source should be changed to.',
|
|
|
|
required=True)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
out_dir = args.dest_dir
|
|
|
|
if not os.path.exists(out_dir):
|
|
|
|
os.makedirs(out_dir, exist_ok=True)
|
2022-08-23 05:01:21 +00:00
|
|
|
print("out_dir {}".format(out_dir))
|
2022-04-28 10:53:40 +00:00
|
|
|
|
2022-06-30 09:42:27 +00:00
|
|
|
for source in args.source:
|
2022-08-23 05:01:21 +00:00
|
|
|
print("source {}".format(out_dir))
|
2022-09-09 08:15:48 +00:00
|
|
|
if not os.path.exists(source):
|
2022-10-21 01:51:13 +00:00
|
|
|
continue
|
2022-04-28 10:53:40 +00:00
|
|
|
|
2022-09-09 07:43:23 +00:00
|
|
|
src_dict = get_param_from_cfg(source)
|
2022-08-23 05:01:21 +00:00
|
|
|
dst = "".join([out_dir, "param_cfg.h"])
|
2022-05-25 04:41:36 +00:00
|
|
|
|
2022-06-30 09:42:27 +00:00
|
|
|
if os.path.exists(dst):
|
2022-09-09 07:43:23 +00:00
|
|
|
dst_dict = get_param_from_c_code(dst)
|
2022-06-30 09:42:27 +00:00
|
|
|
else:
|
2022-09-09 07:43:23 +00:00
|
|
|
dst_dict = {}
|
2022-05-25 04:41:36 +00:00
|
|
|
|
2022-09-09 07:43:23 +00:00
|
|
|
dst_dict = add_to_code_dict(dst_dict, src_dict, False)
|
|
|
|
write_map_to_code(dst, dst_dict)
|
2022-04-28 10:53:40 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|