• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    with open("{}{}".format(output_dir, output_file_name), 'w') as fd_struct:
45        for i in struct_vals:
46            fd_struct.write(i)
47            fd_struct.write('\n')
48
49def write_hdc_file():
50    if hdc_file == "":
51        return
52    with open("{}{}".format(output_dir, hdc_file), 'w') as fd_hdc:
53        fd_hdc.write("#ifndef HDC_HASH_GEN_H\n")
54        fd_hdc.write("#define HDC_HASH_GEN_H\n")
55        fd_hdc.write('\n')
56        fd_hdc.write("#include <stdio.h>\n")
57        context = "{}{}{}".format("#define HDC_MSG_HASH \"", str(algorithm.hexdigest())[0:16], "\"")
58        fd_hdc.write(context)
59        fd_hdc.write("\n\n")
60        fd_hdc.write("#endif\n")
61
62def read_struct():
63    if input_dir == "":
64        return
65    with open("{}{}".format(input_dir , cfg_file_name), mode='r', encoding='utf-8') as fd_path:
66        for line in fd_path.readlines():
67            file_name = line.strip()
68            with open("{}{}".format(input_dir , file_name), mode='r', encoding='utf-8') as fd_file:
69                is_find = False
70                is_end = False
71                begin_count = 0
72                end_count = 0
73                for file_line in fd_file.readlines():
74                    context = file_line.strip()
75                    if is_find and not is_end:
76                        struct_vals.append(context)
77                        if context.find("{") != -1:
78                            begin_count = begin_count + 1
79                        if context.find("}") != -1:
80                            end_count = end_count + 1
81                        if begin_count == end_count and begin_count != 0:
82                            is_end = True
83                            begin_count = 0
84                            end_count = 0
85                    if context.find("struct") != -1:
86                        is_find = True
87                        is_end = False
88                        struct_vals.append(context)
89                        if context.find("{") != -1:
90                            begin_count = begin_count + 1
91
92def main():
93    parser = argparse.ArgumentParser(
94        description='Hdc proto code generator.')
95    parser.add_argument('-f', dest='hdc_file', required=True, type=str,
96                        help='output file name')
97    parser.add_argument('-i', dest='input_dir', required=True, type=str,
98                        help='input directory')
99    parser.add_argument('-o', dest='output_dir', required=True, type=str,
100                        help='output directory')
101
102    args = parser.parse_args(sys.argv[1:])
103    global hdc_file
104    hdc_file = args.hdc_file
105    print("hdc_file:", hdc_file)
106    global input_dir
107    input_dir = args.input_dir
108    print("input_dir:", input_dir)
109    global output_dir
110    output_dir = args.output_dir
111    print("output_dir:", output_dir)
112
113if __name__ == '__main__':
114    print("~~~~~~~~~~~~~~~~ hdc_hash begin ~~~~~~~~~~~~~~~~~~")
115    main()
116    read_struct()
117    write_output_file()
118    calc_file_hash()
119    write_hdc_file()