From 25619bef040b25015a6e577983ccc7e8208e6486 Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Mon, 31 Aug 2020 16:00:57 -0700 Subject: [PATCH 1/4] tools/echo and cleanup fix in export scripts Signed-off-by: Marek Fiala Closes https://github.com/espressif/esp-idf/pull/5816 --- export.sh | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/export.sh b/export.sh index c3d4617ba0..21b9250d25 100644 --- a/export.sh +++ b/export.sh @@ -1,6 +1,6 @@ # This script should be sourced, not executed. -realpath_int() { +__realpath() { wdir="$PWD"; [ "$PWD" = "/" ] && wdir="" arg=$1 case "$arg" in @@ -12,7 +12,12 @@ realpath_int() { } -idf_export_main() { +__verbose() { + [[ -n ${IDF_QUIET} ]] && return + echo "$@" +} + +__main() { # The file doesn't have executable permissions, so this shouldn't really happen. # Doing this in case someone tries to chmod +x it and execute... @@ -82,15 +87,15 @@ idf_export_main() { echo "Detecting the Python interpreter" . "${IDF_PATH}/tools/detect_python.sh" - echo "Adding ESP-IDF tools to PATH..." + __verbose "Adding ESP-IDF tools to PATH..." # Call idf_tools.py to export tool paths export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1 eval "${idf_exports}" - echo "Using Python interpreter in $(which python)" - echo "Checking if Python packages are up to date..." + __verbose "Using Python interpreter in $(which python)" + __verbose "Checking if Python packages are up to date..." python "${IDF_PATH}/tools/check_python_dependencies.py" || return 1 @@ -108,17 +113,17 @@ idf_export_main() { # shellcheck disable=SC2169,SC2039 # unreachable with 'dash' paths="${path_prefix//:/ }" if [ -n "${paths}" ]; then - echo "Added the following directories to PATH:" + __verbose "Added the following directories to PATH:" else - echo "All paths are already set." + __verbose "All paths are already set." fi for path_entry in ${paths} do - echo " ${path_entry}" + __verbose " ${path_entry}" done else - echo "Updated PATH variable:" - echo " ${PATH}" + __verbose "Updated PATH variable:" + __verbose " ${PATH}" fi # Clean up @@ -133,11 +138,11 @@ idf_export_main() { # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system # to check whether we are using a private Python environment - echo "Done! You can now compile ESP-IDF projects." - echo "Go to the project directory and run:" - echo "" - echo " idf.py build" - echo "" + __verbose "Done! You can now compile ESP-IDF projects." + __verbose "Go to the project directory and run:" + __verbose "" + __verbose " idf.py build" + __verbose "" } enable_autocomplete() { @@ -164,9 +169,10 @@ enable_autocomplete() { } -idf_export_main +__main enable_autocomplete -unset realpath_int -unset idf_export_main +unset __realpath +unset __main +unset __verbose unset enable_autocomplete From 8dd6d9fa5ff8cc26bb7eb8c99243ada0dd3e88ad Mon Sep 17 00:00:00 2001 From: Marek Fiala Date: Wed, 6 Oct 2021 13:48:11 +0200 Subject: [PATCH 2/4] Edited Rob Walker's commit to keep ESP_IDF consistency --- export.bat | 19 +++++++++---------- export.fish | 6 +++--- export.sh | 8 ++++---- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/export.bat b/export.bat index cba8e4206d..ce3473cce3 100644 --- a/export.bat +++ b/export.bat @@ -12,7 +12,7 @@ if %errorlevel% neq 0 set "MISSING_REQUIREMENTS= python &echo\" git.exe --version >NUL 2>NUL if %errorlevel% neq 0 set "MISSING_REQUIREMENTS=%MISSING_REQUIREMENTS% git" -if not "%MISSING_REQUIREMENTS%" == "" goto :error_missing_requirements +if not "%MISSING_REQUIREMENTS%" == "" goto :__error_missing_requirements set PREFIX=python.exe %IDF_PATH% DOSKEY idf.py=%PREFIX%\tools\idf.py $* @@ -39,7 +39,7 @@ echo Adding ESP-IDF tools to PATH... :: but that way it is impossible to get the exit code of idf_tools.py. set "IDF_TOOLS_EXPORTS_FILE=%TEMP%\idf_export_vars.tmp" python.exe %IDF_PATH%\tools\idf_tools.py export --format key-value >"%IDF_TOOLS_EXPORTS_FILE%" -if %errorlevel% neq 0 goto :end +if %errorlevel% neq 0 goto :__end for /f "usebackq tokens=1,2 eol=# delims==" %%a in ("%IDF_TOOLS_EXPORTS_FILE%") do ( call set "%%a=%%b" @@ -48,12 +48,12 @@ for /f "usebackq tokens=1,2 eol=# delims==" %%a in ("%IDF_TOOLS_EXPORTS_FILE%") :: This removes OLD_PATH substring from PATH, leaving only the paths which have been added, :: and prints semicolon-delimited components of the path on separate lines call set PATH_ADDITIONS=%%PATH:%OLD_PATH%=%% -if "%PATH_ADDITIONS%"=="" call :print_nothing_added +if "%PATH_ADDITIONS%"=="" call :__print_nothing_added if not "%PATH_ADDITIONS%"=="" echo %PATH_ADDITIONS:;=&echo. % echo Checking if Python packages are up to date... python.exe %IDF_PATH%\tools\check_python_dependencies.py -if %errorlevel% neq 0 goto :end +if %errorlevel% neq 0 goto :__end echo. echo Done! You can now compile ESP-IDF projects. @@ -62,16 +62,16 @@ echo. echo idf.py build echo. -goto :end +goto :__end -:print_nothing_added +:__print_nothing_added echo No directories added to PATH: echo. echo %PATH% echo. goto :eof -:error_missing_requirements +:__error_missing_requirements echo. echo Error^: The following tools are not installed in your environment. echo. @@ -80,10 +80,9 @@ goto :end echo Please use the Windows Tool installer for setting up your environment. echo Download link: https://dl.espressif.com/dl/esp-idf/ echo For more details please visit our website: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/windows-setup.html - goto :end - -:end + goto :__end +:__end :: Clean up if not "%IDF_TOOLS_EXPORTS_FILE%"=="" ( del "%IDF_TOOLS_EXPORTS_FILE%" 1>nul 2>nul diff --git a/export.fish b/export.fish index 3725e677db..85fd69b73c 100644 --- a/export.fish +++ b/export.fish @@ -1,6 +1,6 @@ # This script should be sourced, not executed. -function idf_export_main +function __main if not set -q IDF_PATH echo "IDF_PATH must be set before sourcing this script" return 1 @@ -65,7 +65,7 @@ function idf_export_main echo "" end -idf_export_main +__main set click_version (python -c 'import click; print(click.__version__.split(".")[0])') if test $click_version -lt 8 @@ -75,4 +75,4 @@ else end -set -e idf_export_main +set -e __main diff --git a/export.sh b/export.sh index 21b9250d25..7fd6e09159 100644 --- a/export.sh +++ b/export.sh @@ -13,7 +13,7 @@ __realpath() { __verbose() { - [[ -n ${IDF_QUIET} ]] && return + [ -n "${IDF_EXPORT_QUIET}" ] && return echo "$@" } @@ -145,7 +145,7 @@ __main() { __verbose "" } -enable_autocomplete() { +__enable_autocomplete() { click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')" if [[ click_version -lt 8 ]] then @@ -170,9 +170,9 @@ enable_autocomplete() { } __main -enable_autocomplete +__enable_autocomplete unset __realpath unset __main unset __verbose -unset enable_autocomplete +unset __enable_autocomplete From 6c564bd6903ec233106454c9faf4db5dd2074018 Mon Sep 17 00:00:00 2001 From: Jen Chitty Date: Mon, 17 Aug 2020 12:08:32 -0700 Subject: [PATCH 3/4] Correct export.sh failure exit code and cleanup In failure cases, when idf_export_main() returns 1 (failure), the export.sh script returns 0 (success). This makes it very difficult to detect failure when writing scripts that use export.sh. Furthermore, idf_export_main() does not clean up all internal variables and functions in failure cases. Move all cleanup steps into a cleanup function and pass the return value from idf_export_main() to the cleanup function so it can return with that same return value. Signed-off-by: Marek Fiala Closes https://github.com/espressif/esp-idf/pull/5744 --- export.sh | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/export.sh b/export.sh index 7fd6e09159..3dfdc66997 100644 --- a/export.sh +++ b/export.sh @@ -126,7 +126,15 @@ __main() { __verbose " ${PATH}" fi - # Clean up + + __verbose "Done! You can now compile ESP-IDF projects." + __verbose "Go to the project directory and run:" + __verbose "" + __verbose " idf.py build" + __verbose "" +} + +__cleanup() { unset old_path unset paths unset path_prefix @@ -135,16 +143,19 @@ __main() { unset idf_exports unset ESP_PYTHON + unset __realpath + unset __main + unset __verbose + unset __enable_autocomplete + unset __cleanup + # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system # to check whether we are using a private Python environment - __verbose "Done! You can now compile ESP-IDF projects." - __verbose "Go to the project directory and run:" - __verbose "" - __verbose " idf.py build" - __verbose "" + return $1 } + __enable_autocomplete() { click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')" if [[ click_version -lt 8 ]] @@ -171,8 +182,4 @@ __enable_autocomplete() { __main __enable_autocomplete - -unset __realpath -unset __main -unset __verbose -unset __enable_autocomplete +__cleanup $? From d8a8eceef43c11b9a84ac6e13ed53154d8573dea Mon Sep 17 00:00:00 2001 From: Marek Fiala Date: Mon, 11 Oct 2021 14:11:09 +0200 Subject: [PATCH 4/4] Edited Jen Chitty's commit to keep ESP_IDF consistency --- export.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/export.sh b/export.sh index 3dfdc66997..da23d1f248 100644 --- a/export.sh +++ b/export.sh @@ -142,6 +142,8 @@ __cleanup() { unset IDF_ADD_PATHS_EXTRAS unset idf_exports unset ESP_PYTHON + unset SOURCE_ZSH + unset SOURCE_BASH unset __realpath unset __main @@ -174,12 +176,7 @@ __enable_autocomplete() { then eval "$(env LANG=en _IDF.PY_COMPLETE=$SOURCE_BASH idf.py)" || echo "WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!" fi - - unset SOURCE_ZSH - unset SOURCE_BASH - } -__main -__enable_autocomplete +__main && __enable_autocomplete __cleanup $?