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
29
30def calc_file_hash():
31    if OUTPUT_DIR == "":
32        return
33    global ALGORITHM
34    ALGORITHM = hashlib.sha256()
35    size = os.path.getsize("{}{}".format(OUTPUT_DIR, OUTPUT_FILE_NAME))
36    with open("{}{}".format(OUTPUT_DIR, OUTPUT_FILE_NAME), 'rb') as fd:
37        while size >= 1024 * 1024:
38            ALGORITHM.update(fd.read(1024 * 1024))
39            size -= 1024 * 1024
40        ALGORITHM.update(fd.read())
41
42
43def write_output_file():
44    if OUTPUT_DIR == "":
45        return
46    if not os.path.exists(OUTPUT_DIR):
47        os.makedirs(OUTPUT_DIR, exist_ok=True)
48    with open("{}{}".format(OUTPUT_DIR, OUTPUT_FILE_NAME), 'w') as fd_struct:
49        for i in struct_vals:
50            fd_struct.write(i)
51            fd_struct.write('\n')
52
53
54def write_hdc_file():
55    if HDC_FILE == "":
56        return
57    with open("{}{}".format(OUTPUT_DIR, HDC_FILE), 'w') as fd_hdc:
58        fd_hdc.write("#ifndef HDC_HASH_GEN_H\n")
59        fd_hdc.write("#define HDC_HASH_GEN_H\n")
60        fd_hdc.write('\n')
61        fd_hdc.write("#include <stdio.h>\n")
62        context = "{}{}{}".format("#define HDC_MSG_HASH \"", str(ALGORITHM.hexdigest())[0:16], "\"")
63        fd_hdc.write(context)
64        fd_hdc.write("\n\n")
65        fd_hdc.write("#endif\n")
66
67
68def read_struct():
69    if INPUT_DIR == "":
70        return
71    with open("{}/{}".format(INPUT_DIR, CFG_FILE_NAME), mode='r', encoding='utf-8') as fd_path:
72        for line in fd_path.readlines():
73            file_name = line.strip()
74            with open("{}{}".format(INPUT_DIR, file_name), mode='r', encoding='utf-8') as fd_file:
75                is_find = False
76                is_end = False
77                begin_count = 0
78                end_count = 0
79                for file_line in fd_file.readlines():
80                    context = file_line.strip()
81                    if is_find and not is_end:
82                        struct_vals.append(context)
83                        if context.find("{") != -1:
84                            begin_count = begin_count + 1
85                        if context.find("}") != -1:
86                            end_count = end_count + 1
87                        if begin_count == end_count and begin_count != 0:
88                            is_end = True
89                            begin_count = 0
90                            end_count = 0
91                    if context.find("struct") != -1:
92                        is_find = True
93                        is_end = False
94                        struct_vals.append(context)
95                        if context.find("{") != -1:
96                            begin_count = begin_count + 1
97
98
99def main():
100    parser = argparse.ArgumentParser(
101        description='Hdc proto code generator.')
102    parser.add_argument('-f', dest='HDC_FILE', required=True, type=str,
103                        help='output file name')
104    parser.add_argument('-i', dest='INPUT_DIR', required=True, type=str,
105                        help='input directory')
106    parser.add_argument('-o', dest='OUTPUT_DIR', required=True, type=str,
107                        help='output directory')
108
109    args = parser.parse_args(sys.argv[1:])
110    global HDC_FILE
111    HDC_FILE = args.HDC_FILE
112    print("HDC_FILE:", HDC_FILE)
113    global INPUT_DIR
114    INPUT_DIR = args.INPUT_DIR
115    print("INPUT_DIR:", INPUT_DIR)
116    global OUTPUT_DIR
117    OUTPUT_DIR = args.OUTPUT_DIR
118    print("OUTPUT_DIR:", OUTPUT_DIR)
119
120
121if __name__ == '__main__':
122    print("~~~~~~~~~~~~~~~~ hdc_hash begin ~~~~~~~~~~~~~~~~~~")
123    main()
124    read_struct()
125    write_output_file()
126    calc_file_hash()
127    write_hdc_file()