From 2a730df2a83a7ff8b2cb19833560cb2733b8081a Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 1 Mar 2022 18:03:50 +0100 Subject: [PATCH] update generator scripts --- generators/definitions.py | 15 ++++-- generators/events/event_parser.py | 46 +++++++++++------ generators/fsfwgen.py | 21 +++++--- generators/objects/objects.py | 50 ++++++++++++------- .../returnvalues/returnvalues_parser.py | 41 ++++++++------- 5 files changed, 108 insertions(+), 65 deletions(-) diff --git a/generators/definitions.py b/generators/definitions.py index 528c3019..e72024a5 100644 --- a/generators/definitions.py +++ b/generators/definitions.py @@ -2,9 +2,18 @@ import os import enum from pathlib import Path -PATH_VAR_ROOT = Path(os.path.abspath(os.curdir)) -ROOT_DIR = PATH_VAR_ROOT.absolute() -OBSW_ROOT_DIR = PATH_VAR_ROOT.parent.absolute() + +def determine_obsw_root_path() -> str: + for _ in range(5): + if os.path.exists("CMakeLists.txt"): + return os.curdir + else: + os.chdir("..") + + +PATH_VAR_ROOT = os.path.dirname(os.path.realpath(__file__)) +ROOT_DIR = PATH_VAR_ROOT +OBSW_ROOT_DIR = Path(determine_obsw_root_path()) DATABASE_NAME = "eive_mod.db" diff --git a/generators/events/event_parser.py b/generators/events/event_parser.py index 2275aa04..b6853018 100644 --- a/generators/events/event_parser.py +++ b/generators/events/event_parser.py @@ -3,12 +3,17 @@ Event exporter. """ import datetime import time +import os -from fsfwgen.events.event_parser import handle_csv_export, handle_cpp_export, \ - SubsystemDefinitionParser, EventParser +from fsfwgen.events.event_parser import ( + handle_csv_export, + handle_cpp_export, + SubsystemDefinitionParser, + EventParser, +) from fsfwgen.parserbase.file_list_parser import FileListParser from fsfwgen.utility.printer import PrettyPrinter -from fsfwgen.utility.file_management import copy_file, move_file +from fsfwgen.utility.file_management import copy_file from fsfwgen.core import get_console_logger from definitions import BspType, ROOT_DIR, OBSW_ROOT_DIR @@ -25,15 +30,16 @@ MOVE_CSV_FILE = True PARSE_HOST_BSP = True -CPP_FILENAME = f'{__package__}/translateEvents.cpp' -CPP_H_FILENAME = f'{__package__}/translateEvents.h' +# Store these files relative to the events folder +CPP_FILENAME = f"{os.path.dirname(os.path.realpath(__file__))}/translateEvents.cpp" +CPP_H_FILENAME = f"{os.path.dirname(os.path.realpath(__file__))}/translateEvents.h" BSP_SELECT = BspType.BSP_Q7S BSP_DIR_NAME = BSP_SELECT.value -CSV_FILENAME = f"{BSP_SELECT.value}_events.csv" -CSV_MOVE_DESTINATION = f'{ROOT_DIR}' +# Store this file in the root of the generators folder +CSV_FILENAME = f"{ROOT_DIR}/{BSP_SELECT.value}_events.csv" if BSP_SELECT == BspType.BSP_Q7S or BSP_SELECT == BspType.BSP_LINUX_BOARD: FSFW_CONFIG_ROOT = f"{OBSW_ROOT_DIR}/linux/fsfwconfig" @@ -47,16 +53,20 @@ FILE_SEPARATOR = ";" SUBSYSTEM_DEFINITION_DESTINATIONS = [ f"{FSFW_CONFIG_ROOT}/events/subsystemIdRanges.h", f"{OBSW_ROOT_DIR}/fsfw/src/fsfw/events/fwSubsystemIdRanges.h", - f"{OBSW_ROOT_DIR}/common/config/commonSubsystemIds.h" + f"{OBSW_ROOT_DIR}/common/config/commonSubsystemIds.h", ] HEADER_DEFINITION_DESTINATIONS = [ - f"{OBSW_ROOT_DIR}/mission/", f"{OBSW_ROOT_DIR}/fsfw/", f"{FSFW_CONFIG_ROOT}", - f"{OBSW_ROOT_DIR}/test/", f"{OBSW_ROOT_DIR}/bsp_q7s", f"{OBSW_ROOT_DIR}/linux/" + f"{OBSW_ROOT_DIR}/mission/", + f"{OBSW_ROOT_DIR}/fsfw/", + f"{FSFW_CONFIG_ROOT}", + f"{OBSW_ROOT_DIR}/test/", + f"{OBSW_ROOT_DIR}/bsp_q7s", + f"{OBSW_ROOT_DIR}/linux/", ] def parse_events( - generate_csv: bool = True, generate_cpp: bool = True, print_events: bool = True + generate_csv: bool = True, generate_cpp: bool = True, print_events: bool = True ): LOGGER.info("EventParser: Parsing events: ") # Small delay for clean printout @@ -73,11 +83,14 @@ def parse_events( ) if generate_cpp: handle_cpp_export( - event_list=event_list, date_string=DATE_STRING_FULL, file_name=CPP_FILENAME, - generate_header=GENERATE_CPP_H, header_file_name=CPP_H_FILENAME + event_list=event_list, + date_string=DATE_STRING_FULL, + file_name=CPP_FILENAME, + generate_header=GENERATE_CPP_H, + header_file_name=CPP_H_FILENAME, ) if COPY_CPP_FILE: - LOGGER.info(f'EventParser: Copying file to {CPP_COPY_DESTINATION}') + LOGGER.info(f"EventParser: Copying file to {CPP_COPY_DESTINATION}") copy_file(CPP_FILENAME, CPP_COPY_DESTINATION) copy_file(CPP_H_FILENAME, CPP_COPY_DESTINATION) @@ -85,7 +98,7 @@ def parse_events( def generate_event_list() -> list: subsystem_parser = SubsystemDefinitionParser(SUBSYSTEM_DEFINITION_DESTINATIONS) subsystem_table = subsystem_parser.parse_files() - LOGGER.info(f'Found {len(subsystem_table)} subsystem definitions.') + LOGGER.info(f"Found {len(subsystem_table)} subsystem definitions.") PrettyPrinter.pprint(subsystem_table) event_header_parser = FileListParser(HEADER_DEFINITION_DESTINATIONS) event_headers = event_header_parser.parse_header_files( @@ -94,8 +107,9 @@ def generate_event_list() -> list: # PrettyPrinter.pprint(event_headers) # myEventList = parseHeaderFiles(subsystem_table, event_headers) event_parser = EventParser(event_headers, subsystem_table) + event_parser.obsw_root_path = OBSW_ROOT_DIR event_parser.set_moving_window_mode(moving_window_size=7) event_table = event_parser.parse_files() event_list = sorted(event_table.items()) - LOGGER.info(f'Found {len(event_list)} entries') + LOGGER.info(f"Found {len(event_list)} entries") return event_list diff --git a/generators/fsfwgen.py b/generators/fsfwgen.py index 04c38d24..3d6c1038 100755 --- a/generators/fsfwgen.py +++ b/generators/fsfwgen.py @@ -4,26 +4,31 @@ import time from objects.objects import parse_objects from events.event_parser import parse_events from returnvalues.returnvalues_parser import parse_returnvalues -from fsfwgen.core import return_generic_args_parser, init_printout, get_console_logger, ParserTypes +from fsfwgen.core import ( + return_generic_args_parser, + init_printout, + get_console_logger, + ParserTypes, +) LOGGER = get_console_logger() def main(): - init_printout(project_string='EIVE') + init_printout(project_string="EIVE") parser = return_generic_args_parser() args = parser.parse_args() - if args.type == 'objects': - LOGGER.info(f'Generating objects data..') + if args.type == "objects": + LOGGER.info(f"Generating objects data..") time.sleep(0.05) parse_objects() - elif args.type == 'events': - LOGGER.info(f'Generating event data') + elif args.type == "events": + LOGGER.info(f"Generating event data") time.sleep(0.05) parse_events() - elif args.type == 'returnvalues': - LOGGER.info('Generating returnvalue data') + elif args.type == "returnvalues": + LOGGER.info("Generating returnvalue data") time.sleep(0.05) parse_returnvalues() pass diff --git a/generators/objects/objects.py b/generators/objects/objects.py index 194ee9da..b949ca73 100644 --- a/generators/objects/objects.py +++ b/generators/objects/objects.py @@ -2,13 +2,18 @@ Object exporter. """ import datetime +import os from fsfwgen.core import get_console_logger -from fsfwgen.objects.objects import sql_object_exporter, ObjectDefinitionParser, \ - write_translation_file, \ - export_object_file, write_translation_header_file +from fsfwgen.objects.objects import ( + sql_object_exporter, + ObjectDefinitionParser, + write_translation_file, + export_object_file, + write_translation_header_file, +) from fsfwgen.utility.printer import PrettyPrinter -from fsfwgen.utility.file_management import copy_file, move_file +from fsfwgen.utility.file_management import copy_file from definitions import BspType, DATABASE_NAME, OBSW_ROOT_DIR, ROOT_DIR @@ -34,16 +39,17 @@ else: EXPORT_TO_SQL = True CPP_COPY_DESTINATION = f"{FSFW_CONFIG_ROOT}/objects/" -CSV_MOVE_DESTINATION = f"{ROOT_DIR}" -CPP_FILENAME = f'{__package__}/translateObjects.cpp' -CPP_H_FILENAME = f'{__package__}/translateObjects.h' -CSV_OBJECT_FILENAME = f"{BSP_SELECT.value}_objects.csv" +CPP_FILENAME = f"{os.path.dirname(os.path.realpath(__file__))}//translateObjects.cpp" +CPP_H_FILENAME = f"{os.path.dirname(os.path.realpath(__file__))}//translateObjects.h" +CSV_OBJECT_FILENAME = f"{ROOT_DIR}/{BSP_SELECT.value}_objects.csv" FILE_SEPARATOR = ";" OBJECTS_PATH = f"{FSFW_CONFIG_ROOT}/objects/systemObjectList.h" -FRAMEWORK_OBJECT_PATH = f'{OBSW_ROOT_DIR}/fsfw/src/fsfw/objectmanager/frameworkObjects.h' -COMMON_OBJECTS_PATH = f'{OBSW_ROOT_DIR}/common/config/commonObjects.h' +FRAMEWORK_OBJECT_PATH = ( + f"{OBSW_ROOT_DIR}/fsfw/src/fsfw/objectmanager/frameworkObjects.h" +) +COMMON_OBJECTS_PATH = f"{OBSW_ROOT_DIR}/common/config/commonObjects.h" OBJECTS_DEFINITIONS = [OBJECTS_PATH, FRAMEWORK_OBJECT_PATH, COMMON_OBJECTS_PATH] SQL_DELETE_OBJECTS_CMD = """ @@ -70,35 +76,41 @@ def parse_objects(print_object_list: bool = True): subsystem_definitions = object_parser.parse_files() # id_subsystem_definitions.update(framework_subsystem_definitions) list_items = sorted(subsystem_definitions.items()) - LOGGER.info(f'ObjectParser: Number of objects: {len(list_items)}') + LOGGER.info(f"ObjectParser: Number of objects: {len(list_items)}") if print_object_list: PrettyPrinter.pprint(list_items) handle_file_export(list_items) if EXPORT_TO_SQL: - LOGGER.info('ObjectParser: Exporting to SQL') + LOGGER.info("ObjectParser: Exporting to SQL") sql_object_exporter( - object_table=list_items, delete_cmd=SQL_DELETE_OBJECTS_CMD, + object_table=list_items, + delete_cmd=SQL_DELETE_OBJECTS_CMD, insert_cmd=SQL_INSERT_INTO_OBJECTS_CMD, - create_cmd=SQL_CREATE_OBJECTS_CMD, db_filename=f"{ROOT_DIR}/{DATABASE_NAME}" + create_cmd=SQL_CREATE_OBJECTS_CMD, + db_filename=f"{ROOT_DIR}/{DATABASE_NAME}", ) def handle_file_export(list_items): if GENERATE_CPP: - LOGGER.info('ObjectParser: Generating translation C++ file') + LOGGER.info("ObjectParser: Generating C++ translation file") write_translation_file( - filename=CPP_FILENAME, list_of_entries=list_items, date_string_full=DATE_STRING_FULL + filename=CPP_FILENAME, + list_of_entries=list_items, + date_string_full=DATE_STRING_FULL, ) if COPY_CPP: - print("ObjectParser: Copying object file to " + CPP_COPY_DESTINATION) + LOGGER.info("ObjectParser: Copying object file to " + CPP_COPY_DESTINATION) copy_file(CPP_FILENAME, CPP_COPY_DESTINATION) if GENERATE_HEADER: write_translation_header_file(filename=CPP_H_FILENAME) copy_file(filename=CPP_H_FILENAME, destination=CPP_COPY_DESTINATION) if GENERATE_CSV: - print("ObjectParser: Generating text export.") + LOGGER.info("ObjectParser: Generating text export") export_object_file( - filename=CSV_OBJECT_FILENAME, object_list=list_items, file_separator=FILE_SEPARATOR + filename=CSV_OBJECT_FILENAME, + object_list=list_items, + file_separator=FILE_SEPARATOR, ) diff --git a/generators/returnvalues/returnvalues_parser.py b/generators/returnvalues/returnvalues_parser.py index eadb035f..22c1589e 100644 --- a/generators/returnvalues/returnvalues_parser.py +++ b/generators/returnvalues/returnvalues_parser.py @@ -24,33 +24,34 @@ EXPORT_TO_SQL = True PRINT_TABLES = True -FILE_SEPARATOR = ';' +FILE_SEPARATOR = ";" MAX_STRING_LENGTH = 32 BSP_SELECT = BspType.BSP_Q7S BSP_DIR_NAME = BSP_SELECT.value -CSV_RETVAL_FILENAME = f'{BSP_SELECT.value}_returnvalues.csv' -CSV_MOVE_DESTINATION = f'{ROOT_DIR}' +CSV_RETVAL_FILENAME = f"{ROOT_DIR}/{BSP_SELECT.value}_returnvalues.csv" ADD_LINUX_FOLDER = False if BSP_SELECT == BspType.BSP_Q7S or BSP_SELECT == BspType.BSP_LINUX_BOARD: - FSFW_CONFIG_ROOT = f'{OBSW_ROOT_DIR}/linux/fsfwconfig' + FSFW_CONFIG_ROOT = f"{OBSW_ROOT_DIR}/linux/fsfwconfig" ADD_LINUX_FOLDER = True else: - FSFW_CONFIG_ROOT = f'{OBSW_ROOT_DIR}/{BSP_DIR_NAME}/fsfwconfig' + FSFW_CONFIG_ROOT = f"{OBSW_ROOT_DIR}/{BSP_DIR_NAME}/fsfwconfig" BSP_PATH = f"{OBSW_ROOT_DIR}/{BSP_DIR_NAME}" INTERFACE_DEFINITION_FILES = [ - f'{OBSW_ROOT_DIR}/fsfw/src/fsfw/returnvalues/FwClassIds.h', - f'{OBSW_ROOT_DIR}/common/config/commonClassIds.h', - f'{FSFW_CONFIG_ROOT}/returnvalues/classIds.h' + f"{OBSW_ROOT_DIR}/fsfw/src/fsfw/returnvalues/FwClassIds.h", + f"{OBSW_ROOT_DIR}/common/config/commonClassIds.h", + f"{FSFW_CONFIG_ROOT}/returnvalues/classIds.h", ] RETURNVALUE_SOURCES = [ - f'{OBSW_ROOT_DIR}/mission/', f'{OBSW_ROOT_DIR}/fsfw/', f'{BSP_PATH}' + f"{OBSW_ROOT_DIR}/mission/", + f"{OBSW_ROOT_DIR}/fsfw/", + f"{BSP_PATH}", ] if ADD_LINUX_FOLDER: - RETURNVALUE_SOURCES.append(f'{OBSW_ROOT_DIR}/linux') + RETURNVALUE_SOURCES.append(f"{OBSW_ROOT_DIR}/linux") SQL_DELETE_RETURNVALUES_CMD = """ DROP TABLE IF EXISTS Returnvalues @@ -76,16 +77,20 @@ VALUES(?,?,?,?,?) def parse_returnvalues(): returnvalue_table = generate_returnvalue_table() if EXPORT_TO_FILE: - ReturnValueParser.export_to_file(CSV_RETVAL_FILENAME, returnvalue_table, FILE_SEPARATOR) + ReturnValueParser.export_to_file( + CSV_RETVAL_FILENAME, returnvalue_table, FILE_SEPARATOR + ) # if MOVE_CSV_FILE: # move_file(file_name=CSV_RETVAL_FILENAME, destination=CSV_MOVE_DESTINATION) if EXPORT_TO_SQL: - LOGGER.info('ReturnvalueParser: Exporting to SQL') - sql_retval_exporter(returnvalue_table, db_filename=f"{ROOT_DIR}/{DATABASE_NAME}") + LOGGER.info("ReturnvalueParser: Exporting to SQL") + sql_retval_exporter( + returnvalue_table, db_filename=f"{ROOT_DIR}/{DATABASE_NAME}" + ) def generate_returnvalue_table(): - """ Core function to parse for the return values """ + """Core function to parse for the return values""" interface_parser = InterfaceParser( file_list=INTERFACE_DEFINITION_FILES, print_table=PRINT_TABLES ) @@ -104,10 +109,8 @@ def sql_retval_exporter(returnvalue_table, db_filename: str): sql_writer.open(SQL_CREATE_RETURNVALUES_CMD) for entry in returnvalue_table.items(): sql_writer.write_entries( - SQL_INSERT_RETURNVALUES_CMD, (entry[0], - entry[1][2], - entry[1][4], - entry[1][3], - entry[1][1])) + SQL_INSERT_RETURNVALUES_CMD, + (entry[0], entry[1][2], entry[1][4], entry[1][3], entry[1][1]), + ) sql_writer.commit() sql_writer.close()