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