1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import sys 18import argparse 19import hashlib 20 21algorithm = None 22hdc_file = "" 23input_dir = "" 24output_dir = "" 25struct_vals = [] 26cfg_file_name = r"/scripts/file_path.cfg" 27output_file_name = r"all.txt" 28 29def calc_file_hash(): 30 if output_dir == "": 31 return 32 global algorithm 33 algorithm = hashlib.sha256() 34 size = os.path.getsize("{}{}".format(output_dir, output_file_name)) 35 with open("{}{}".format(output_dir, output_file_name), 'rb') as fd: 36 while size >= 1024 * 1024: 37 algorithm.update(fd.read(1024 * 1024)) 38 size -= 1024 * 1024 39 algorithm.update(fd.read()) 40 41def write_output_file(): 42 if output_dir == "": 43 return 44 if not os.path.exists(output_dir): 45 os.makedirs(output_dir, exist_ok=True) 46 with open("{}{}".format(output_dir, output_file_name), 'w') as fd_struct: 47 for i in struct_vals: 48 fd_struct.write(i) 49 fd_struct.write('\n') 50 51def write_hdc_file(): 52 if hdc_file == "": 53 return 54 with open("{}{}".format(output_dir, hdc_file), 'w') as fd_hdc: 55 fd_hdc.write("#ifndef HDC_HASH_GEN_H\n") 56 fd_hdc.write("#define HDC_HASH_GEN_H\n") 57 fd_hdc.write('\n') 58 fd_hdc.write("#include <stdio.h>\n") 59 context = "{}{}{}".format("#define HDC_MSG_HASH \"", str(algorithm.hexdigest())[0:16], "\"") 60 fd_hdc.write(context) 61 fd_hdc.write("\n\n") 62 fd_hdc.write("#endif\n") 63 64def read_struct(): 65 if input_dir == "": 66 return 67 with open("{}{}".format(input_dir, cfg_file_name), mode='r', encoding='utf-8') as fd_path: 68 for line in fd_path.readlines(): 69 file_name = line.strip() 70 with open("{}{}".format(input_dir, file_name), mode='r', encoding='utf-8') as fd_file: 71 is_find = False 72 is_end = False 73 begin_count = 0 74 end_count = 0 75 for file_line in fd_file.readlines(): 76 context = file_line.strip() 77 if is_find and not is_end: 78 struct_vals.append(context) 79 if context.find("{") != -1: 80 begin_count = begin_count + 1 81 if context.find("}") != -1: 82 end_count = end_count + 1 83 if begin_count == end_count and begin_count != 0: 84 is_end = True 85 begin_count = 0 86 end_count = 0 87 if context.find("struct") != -1: 88 is_find = True 89 is_end = False 90 struct_vals.append(context) 91 if context.find("{") != -1: 92 begin_count = begin_count + 1 93 94def main(): 95 parser = argparse.ArgumentParser( 96 description='Hdc proto code generator.') 97 parser.add_argument('-f', dest='hdc_file', required=True, type=str, 98 help='output file name') 99 parser.add_argument('-i', dest='input_dir', required=True, type=str, 100 help='input directory') 101 parser.add_argument('-o', dest='output_dir', required=True, type=str, 102 help='output directory') 103 104 args = parser.parse_args(sys.argv[1:]) 105 global hdc_file 106 hdc_file = args.hdc_file 107 print("hdc_file:", hdc_file) 108 global input_dir 109 input_dir = args.input_dir 110 print("input_dir:", input_dir) 111 global output_dir 112 output_dir = args.output_dir 113 print("output_dir:", output_dir) 114 115if __name__ == '__main__': 116 print("~~~~~~~~~~~~~~~~ hdc_hash begin ~~~~~~~~~~~~~~~~~~") 117 main() 118 read_struct() 119 write_output_file() 120 calc_file_hash() 121 write_hdc_file()