From 6f21e45b787cc99ef54f1b7b666ed6d94113a899 Mon Sep 17 00:00:00 2001 From: Archana Date: Tue, 23 Nov 2021 14:46:51 +0530 Subject: [PATCH] Fix Pylint errors and improve Python script Pylint errors are fixed. The Python script is improved to take default arguments when not passed (eg invoked from root of the tree) check-generated-files.sh and CMakeLists.sh updated. Signed-off-by: Archana --- library/CMakeLists.txt | 16 +++++++++ library/Makefile | 3 +- scripts/generate_driver_wrappers.py | 45 +++++++++++++++----------- scripts/make_generated_files.bat | 24 +++++++------- tests/scripts/check-generated-files.sh | 1 + 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 1884db937..7b7fca406 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -157,10 +157,26 @@ if(GEN_FILES) ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py ${error_headers} ) + + add_custom_command( + OUTPUT + ${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers.c + COMMAND + ${MBEDTLS_PYTHON_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py + ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf + ${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers.c + DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py + ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf + ) + + else() link_to_source(error.c) link_to_source(version_features.c) link_to_source(ssl_debug_helpers_generated.c) + link_to_source(psa_crypto_driver_wrappers.c) endif() if(CMAKE_COMPILER_IS_GNUCC) diff --git a/library/Makefile b/library/Makefile index 31abed51f..eb8b78e63 100644 --- a/library/Makefile +++ b/library/Makefile @@ -324,8 +324,7 @@ psa_crypto_driver_wrappers.c: ../scripts/data_files/driver_templates/psa_crypto_ psa_crypto_driver_wrappers.c: echo " Gen $@" $(PYTHON) ../scripts/generate_driver_wrappers.py \ - "../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf" \ - "psa_crypto_driver_wrappers.c" + "../" clean: ifndef WINDOWS diff --git a/scripts/generate_driver_wrappers.py b/scripts/generate_driver_wrappers.py index d713f3563..b05612ebc 100755 --- a/scripts/generate_driver_wrappers.py +++ b/scripts/generate_driver_wrappers.py @@ -1,28 +1,35 @@ #!/usr/bin/env python3 +"""This script is required for the auto generation of the + psa_crypto_driver_wrappers.c file""" import sys -import json import os import jinja2 -def render(tpl_path): - path, filename = os.path.split(tpl_path) - return jinja2.Environment( - loader=jinja2.FileSystemLoader(path or './'), - keep_trailing_newline=True, - ).get_template(filename).render() +def render(template_path: str) -> str: + environment = jinja2.Environment( + loader=jinja2.FileSystemLoader(os.path.dirname(template_path)), + keep_trailing_newline=True) + template = environment.get_template(os.path.basename(template_path)) + return template.render() -n = len(sys.argv) -if ( n != 3 ): - sys.exit("The template file name and output file name are expected as arguments") -# set template file name, output file name -driver_wrapper_template_filename = sys.argv[1] -driver_wrapper_output_filename = sys.argv[2] +N = len(sys.argv) +if N != 2: +# This is the Root directory. + ROOT_DIR = "" +else: +# Set the root based on the argument passed. + ROOT_DIR = sys.argv[1] -# render the template -result = render(driver_wrapper_template_filename) +# Set template file name, output file name from the root directory +DRIVER_WRAPPER_TEMPLATE_FILENAME = ROOT_DIR +\ + "scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf" +DRIVER_WRAPPER_OUTPUT_FILENAME = ROOT_DIR + "library/psa_crypto_driver_wrappers.c" -# write output to file -outFile = open(driver_wrapper_output_filename,"w") -outFile.write(result) -outFile.close() +# Render the template +RESULT = render(DRIVER_WRAPPER_TEMPLATE_FILENAME) + +# Write output to file +OUT_FILE = open(DRIVER_WRAPPER_OUTPUT_FILENAME, "w") +OUT_FILE.write(RESULT) +OUT_FILE.close() diff --git a/scripts/make_generated_files.bat b/scripts/make_generated_files.bat index 164a16edf..662da984c 100644 --- a/scripts/make_generated_files.bat +++ b/scripts/make_generated_files.bat @@ -1,15 +1,13 @@ -@rem Generate automatically-generated configuration-independent source files -@rem and build scripts. -@rem Perl and Python 3 must be on the PATH. -@rem psa_crypto_driver_wrappers.c needs to be generated prior to -@rem generate_visualc_files.pl being invoked. -python scripts/generate_driver_wrappers.py ^ - "scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf" ^ - "library/psa_crypto_driver_wrappers.c" || exit /b 1 -perl scripts\generate_errors.pl || exit /b 1 -perl scripts\generate_query_config.pl || exit /b 1 -perl scripts\generate_features.pl || exit /b 1 -python scripts\generate_ssl_debug_helpers.py || exit /b 1 -perl scripts\generate_visualc_files.pl || exit /b 1 +@rem Generate automatically-generated configuration-independent source files +@rem and build scripts. +@rem Perl and Python 3 must be on the PATH. +@rem psa_crypto_driver_wrappers.c needs to be generated prior to +@rem generate_visualc_files.pl being invoked. +python scripts\generate_driver_wrappers.py || exit /b 1 +perl scripts\generate_errors.pl || exit /b 1 +perl scripts\generate_query_config.pl || exit /b 1 +perl scripts\generate_features.pl || exit /b 1 +python scripts\generate_ssl_debug_helpers.py || exit /b 1 +perl scripts\generate_visualc_files.pl || exit /b 1 python scripts\generate_psa_constants.py || exit /b 1 python tests\scripts\generate_psa_tests.py || exit /b 1 diff --git a/tests/scripts/check-generated-files.sh b/tests/scripts/check-generated-files.sh index 994fd243b..f42ecd6fb 100755 --- a/tests/scripts/check-generated-files.sh +++ b/tests/scripts/check-generated-files.sh @@ -117,6 +117,7 @@ check() check scripts/generate_errors.pl library/error.c check scripts/generate_query_config.pl programs/test/query_config.c +check scripts/generate_driver_wrappers.py library/psa_crypto_driver_wrappers.c check scripts/generate_features.pl library/version_features.c check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c # generate_visualc_files enumerates source files (library/*.c). It doesn't