added checksum to auto-generated files
This commit is contained in:
parent
925167ec3c
commit
58c6bc820a
@ -2,6 +2,9 @@ import os
|
||||
import time
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
import json
|
||||
import sys
|
||||
|
||||
import filechecksum as fcs
|
||||
|
||||
def build_dtcs():
|
||||
# Pfad zur Eingabedatei und Ausgabedatei
|
||||
@ -81,10 +84,15 @@ def build_dtcs():
|
||||
# Sortieren der Zeilen nach der Nummer aufsteigend
|
||||
dtc_lines.sort(key=lambda x: x[0])
|
||||
|
||||
checksum = fcs.calculate_checksum(dtc_lines)
|
||||
timestamp = int(time.time())
|
||||
|
||||
if fcs.read_and_compare_checksum(output_file, checksum):
|
||||
print("Keine Änderungen im DTC-Headerfile erforderlich.")
|
||||
else:
|
||||
# DTC_NAME_CONSTANT-Makros initialisieren
|
||||
dtc_macros = []
|
||||
dtc_structs = []
|
||||
dtc_table_data = []
|
||||
|
||||
# Verarbeiten der sortierten Zeilen
|
||||
for i, line in enumerate(dtc_lines):
|
||||
@ -92,10 +100,6 @@ def build_dtcs():
|
||||
dtc_macros.append(f"#define {dtc_name:<30} {num}")
|
||||
comma = "," if i < len(dtc_lines) - 1 else " "
|
||||
dtc_structs.append(f" {{ {dtc_name:<30}, {dtc_severity:<12} }}{comma} // {description}")
|
||||
dtc_table_data.append({"num": num, "title": title, "description": description})
|
||||
|
||||
# Unix-Zeitstempel hinzufügen
|
||||
timestamp = int(time.time())
|
||||
|
||||
env = Environment(loader=FileSystemLoader('codegen/templates', encoding='utf-8'))
|
||||
# Lade das Jinja2-Template aus der Datei
|
||||
@ -108,6 +112,7 @@ def build_dtcs():
|
||||
'date' : time.strftime('%d.%m.%Y', time.localtime(timestamp)),
|
||||
'dtc_macros': dtc_macros, # Übergebe die dtc_macros-Liste direkt
|
||||
'dtc_structs': dtc_structs, # Übergebe die dtc_structs-Liste direkt
|
||||
'checksum' : checksum
|
||||
}
|
||||
|
||||
# Rendere das Template mit den Werten und erhalte den Header-Text
|
||||
@ -119,9 +124,22 @@ def build_dtcs():
|
||||
|
||||
print(f"Header-Datei wurde erstellt: {output_file}")
|
||||
|
||||
if fcs.read_and_compare_json_checksum(json_output_file, checksum):
|
||||
print("Keine Änderungen im DTC-JSON-file erforderlich.")
|
||||
else:
|
||||
dtc_info = {
|
||||
"codegenerator_checksum": checksum,
|
||||
'timestamp' : time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp)),
|
||||
"dtc_table_data": []
|
||||
}
|
||||
|
||||
# Verarbeiten der sortierten Zeilen
|
||||
for i, line in enumerate(dtc_lines):
|
||||
num, dtc_name, dtc_severity, title, description = line
|
||||
dtc_info["dtc_table_data"].append({"num": num, "title": title, "description": description})
|
||||
|
||||
# JSON-Datei mit UTF-8-Zeichencodierung erstellen
|
||||
with open(json_output_file, 'w', encoding='utf-8') as json_f:
|
||||
json.dump(dtc_table_data, json_f, ensure_ascii=False, indent=4, separators=(',', ': '))
|
||||
json.dump(dtc_info, json_f, ensure_ascii=False, indent=4, separators=(',', ': '))
|
||||
|
||||
print(f"JSON-Datei wurde erstellt: {json_output_file}")
|
||||
|
45
Software/codegen/filechecksum.py
Normal file
45
Software/codegen/filechecksum.py
Normal file
@ -0,0 +1,45 @@
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
# Funktion zum Berechnen der SHA-256-Checksumme
|
||||
def calculate_checksum(data):
|
||||
sha256 = hashlib.sha256()
|
||||
sha256.update(str(data).encode('utf-8'))
|
||||
return sha256.hexdigest()
|
||||
|
||||
# Funktion zum Lesen und Vergleichen der Checksumme in einer Datei
|
||||
def read_and_compare_checksum(file_path, expected_checksum):
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
content = file.read()
|
||||
# Suche nach der Zeile mit der Checksumme
|
||||
checksum_line_start = content.find("// CODEGENERATOR_CHECKSUM:")
|
||||
if checksum_line_start != -1:
|
||||
# Extrahiere die Checksumme aus der Zeile
|
||||
existing_checksum = content[checksum_line_start + len("// CODEGENERATOR_CHECKSUM:"):].strip()
|
||||
# Vergleiche die Checksummen
|
||||
if existing_checksum == expected_checksum:
|
||||
return True
|
||||
except FileNotFoundError:
|
||||
pass # Datei existiert nicht, was nicht schlimm ist
|
||||
|
||||
return False
|
||||
|
||||
def read_and_compare_json_checksum(json_file_path, expected_checksum):
|
||||
try:
|
||||
with open(json_file_path, 'r') as json_file:
|
||||
# Lade das JSON aus der Datei
|
||||
data = json.load(json_file)
|
||||
|
||||
# Überprüfe, ob "codegenerator_checksum" im JSON vorhanden ist
|
||||
if "codegenerator_checksum" in data:
|
||||
existing_checksum = data["codegenerator_checksum"]
|
||||
|
||||
# Vergleiche die Checksummen
|
||||
if existing_checksum == expected_checksum:
|
||||
return True
|
||||
except FileNotFoundError:
|
||||
pass # Datei existiert nicht, was nicht schlimm ist
|
||||
|
||||
return False
|
@ -3,6 +3,8 @@ import time
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
import re
|
||||
|
||||
import filechecksum as fcs
|
||||
|
||||
# Pfad zur Eingabedatei und Ausgabedatei
|
||||
input_file = "include/config.h"
|
||||
output_sourcefile = "src/struct2json.cpp"
|
||||
@ -67,6 +69,7 @@ def struct2json():
|
||||
|
||||
variable_types = get_types(content, variable_names)
|
||||
structs = extract_struct_fields(content, variable_types)
|
||||
checksum = fcs.calculate_checksum(structs)
|
||||
|
||||
env = Environment(loader=FileSystemLoader('codegen/templates', encoding='utf-8'))
|
||||
# Lade das Jinja2-Template aus der Datei
|
||||
@ -78,16 +81,28 @@ def struct2json():
|
||||
'timestamp_unix': timestamp,
|
||||
'timestamp' : time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp)),
|
||||
'date' : time.strftime('%d.%m.%Y', time.localtime(timestamp)),
|
||||
'structs': structs, # Übergebe die foo-Liste direkt
|
||||
'structs': structs,
|
||||
'checksum': checksum
|
||||
}
|
||||
|
||||
# Überprüfe, ob die Checksummen übereinstimmen
|
||||
if fcs.read_and_compare_checksum(output_sourcefile, checksum):
|
||||
print("Keine Änderungen in der Source-Datei erforderlich.")
|
||||
else:
|
||||
# Rendere das Template mit den Werten und erhalte den Source-Text
|
||||
source_text = template_c.render(context)
|
||||
header_text = template_h.render(context)
|
||||
|
||||
# Schreibe den generierten Source-Text in die Source-Dateien
|
||||
# Schreibe den generierten Source-Text in die Source-Datei
|
||||
with open(output_sourcefile, "w", encoding='utf-8') as f:
|
||||
f.write(source_text)
|
||||
print(f"Source-Datei wurde erstellt: {output_sourcefile}")
|
||||
|
||||
# Überprüfe, ob die Checksummen übereinstimmen
|
||||
if fcs.read_and_compare_checksum(output_headerfile, checksum):
|
||||
print("Keine Änderungen in der Header-Datei erforderlich.")
|
||||
else:
|
||||
# Rendere das Template mit den Werten und erhalte den Header-Text
|
||||
header_text = template_h.render(context)
|
||||
# Schreibe den generierten Header-Text in die Header-Datei
|
||||
with open(output_headerfile, "w", encoding='utf-8') as f:
|
||||
f.write(header_text)
|
||||
print(f"Source-Dateien wurde erstellt: {output_sourcefile}, {output_headerfile}")
|
||||
print(f"Header-Datei wurde erstellt: {output_headerfile}")
|
@ -49,6 +49,6 @@ const DTC_t dtc_definitions[] = {
|
||||
{% endfor -%}
|
||||
};
|
||||
|
||||
const uint32_t dtc_generation_timestamp = {{ timestamp_unix }};
|
||||
|
||||
#endif // DTC_DEFS_H
|
||||
|
||||
// CODEGENERATOR_CHECKSUM: {{ checksum }}
|
@ -21,3 +21,5 @@ void generateJsonObject_{{ var_name }}(JsonObject& data)
|
||||
}
|
||||
|
||||
{% endfor %}
|
||||
|
||||
// CODEGENERATOR_CHECKSUM: {{ checksum }}
|
@ -22,3 +22,5 @@ void generateJsonObject_{{ var_name }}(JsonObject& data);
|
||||
{% endfor %}
|
||||
|
||||
#endif /* _STRUCT2JSON_H_ */
|
||||
|
||||
// CODEGENERATOR_CHECKSUM: {{ checksum }}
|
@ -1,4 +1,7 @@
|
||||
[
|
||||
{
|
||||
"codegenerator_checksum": "23dff82a4745f67041012080b05ec367c2dd44c6bb02974143b227ba49682b6f",
|
||||
"timestamp": "2024-01-10 19:06:30",
|
||||
"dtc_table_data": [
|
||||
{
|
||||
"num": 0,
|
||||
"title": "No Error",
|
||||
@ -89,4 +92,5 @@
|
||||
"title": "Last Error",
|
||||
"description": "Last Error"
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
@ -7,10 +7,10 @@
|
||||
* It includes enums for DTC active status, severity levels, and specific DTC codes.
|
||||
* The file also defines an array of DTC definitions and a timestamp indicating the generation time.
|
||||
*
|
||||
* @note This file is auto-generated by a script on 2024-01-09 12:08:43.
|
||||
* @note This file is auto-generated by a script on 2024-01-10 18:37:05.
|
||||
*
|
||||
* @author Marcel Peterkau
|
||||
* @date 09.01.2024
|
||||
* @date 10.01.2024
|
||||
*/
|
||||
|
||||
#ifndef DTC_DEFS_H
|
||||
@ -80,6 +80,6 @@ const DTC_t dtc_definitions[] = {
|
||||
{ DTC_LAST_DTC , DTC_NONE } // Last Error
|
||||
};
|
||||
|
||||
const uint32_t dtc_generation_timestamp = 1704798523;
|
||||
|
||||
#endif // DTC_DEFS_H
|
||||
|
||||
// CODEGENERATOR_CHECKSUM: 23dff82a4745f67041012080b05ec367c2dd44c6bb02974143b227ba49682b6f
|
@ -3,10 +3,10 @@
|
||||
*
|
||||
* @brief Header file for converting structs to JSON objects.
|
||||
*
|
||||
* @note This file is auto-generated by a script on 2024-01-09 20:41:57.
|
||||
* @note This file is auto-generated by a script on 2024-01-10 18:01:52.
|
||||
*
|
||||
* @author Marcel Peterkau
|
||||
* @date 09.01.2024
|
||||
* @date 10.01.2024
|
||||
*/
|
||||
|
||||
#ifndef _STRUCT2JSON_H_
|
||||
@ -22,3 +22,5 @@ void generateJsonObject_PersistenceData(JsonObject& data);
|
||||
|
||||
|
||||
#endif /* _STRUCT2JSON_H_ */
|
||||
|
||||
// CODEGENERATOR_CHECKSUM: 9e8dd21170fd6ef8fbf8c4b156d9af751836a76081f811bf0e3ab2b1eb8ee48c
|
@ -3,10 +3,10 @@
|
||||
*
|
||||
* @brief Implementation file for converting structs to JSON objects.
|
||||
*
|
||||
* @note This file is auto-generated by a script on 2024-01-09 20:41:57.
|
||||
* @note This file is auto-generated by a script on 2024-01-10 18:01:52.
|
||||
*
|
||||
* @author Marcel Peterkau
|
||||
* @date 09.01.2024
|
||||
* @date 10.01.2024
|
||||
*/
|
||||
|
||||
|
||||
@ -45,3 +45,6 @@ void generateJsonObject_PersistenceData(JsonObject& data)
|
||||
data["checksum"] = PersistenceData.checksum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// CODEGENERATOR_CHECKSUM: 9e8dd21170fd6ef8fbf8c4b156d9af751836a76081f811bf0e3ab2b1eb8ee48c
|
Loading…
x
Reference in New Issue
Block a user