From 33a3ff49df73528836f22de1eb4379033a61cf7f Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Tue, 6 Jul 2021 12:43:54 +0200 Subject: [PATCH] Fix support for zsh in conda binary activation scripts (#810) --- .../workflows/generate-conda-packages.yaml | 4 +- ...enerate_conda_recipes_from_metametadata.py | 38 ++++++++++++++++++- conda/recipe_template/bld.bat | 3 ++ conda/recipe_template/build.sh | 1 + 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.github/workflows/generate-conda-packages.yaml b/.github/workflows/generate-conda-packages.yaml index ed8d00670..f0e451dbb 100644 --- a/.github/workflows/generate-conda-packages.yaml +++ b/.github/workflows/generate-conda-packages.yaml @@ -70,7 +70,9 @@ jobs: - name: Dependencies for conda recipes generation and upload shell: bash -l {0} run: | - mamba install pyyaml jinja2 conda-build ninja anaconda-client conda-forge-pinning boa multisheller + mamba install pyyaml jinja2 conda-build ninja anaconda-client conda-forge-pinning boa + # Use multisheller version that include the fix https://github.com/mamba-org/multisheller/pull/13 + python -m pip install git+https://github.com/mamba-org/multisheller.git@31883c2fe325464a8d3510380afdc83e8f64c349 - name: Generate recipes [Linux&macOS] if: contains(matrix.os, 'macos') || contains(matrix.os, 'ubuntu') diff --git a/conda/python/generate_conda_recipes_from_metametadata.py b/conda/python/generate_conda_recipes_from_metametadata.py index b173a830e..af8a258aa 100644 --- a/conda/python/generate_conda_recipes_from_metametadata.py +++ b/conda/python/generate_conda_recipes_from_metametadata.py @@ -25,7 +25,7 @@ def dir_path(string): suffixes = { 'cmdexe': '.bat', - 'bash': '.sh', + 'bash': '.bash', 'zsh': '.zsh', 'xonsh': '.xsh', 'powershell': '.ps1', @@ -53,6 +53,37 @@ def write_script(generation_directory, name_without_extension, commands, interpr return +def write_bash_zsh_disambiguation_script(generation_directory, name_without_extension): + fname = generation_directory + "/" + name_without_extension + ".sh" + + print(f"Writing file to {fname}") + with open(fname, 'w') as f: + f.write(r'if [ ! -z "$ZSH_VERSION" ]; then') + f.write('\n') + f.write(r' SCRIPT_DIR=${0:a:h}') + f.write('\n') + f.write(r' SCRIPT_NAME=$(basename "${(%):-%x}")') + f.write('\n') + f.write(r' SCRIPT_NAME_WITHOUT_EXT=${SCRIPT_NAME%.*}') + f.write('\n') + f.write(r' source ${SCRIPT_DIR}/${SCRIPT_NAME_WITHOUT_EXT}.zsh') + f.write('\n') + f.write("else\n") + f.write(r' SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"') + f.write('\n') + f.write(r' SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")') + f.write('\n') + f.write(r' SCRIPT_NAME_WITHOUT_EXT=${SCRIPT_NAME%.*}') + f.write('\n') + f.write(r' source ${SCRIPT_DIR}/${SCRIPT_NAME_WITHOUT_EXT}.bash') + f.write('\n') + f.write('fi\n') + + f.close() + print(f"File wrote to {fname}") + + return + # Inspired from: # * https://github.com/wolfv/multisheller/blob/0cc03c68d0c68d2f9cf7b07ddb68afa531419a6d/multisheller/cli/main.py # * https://github.com/wolfv/multisheller/blob/0cc03c68d0c68d2f9cf7b07ddb68afa531419a6d/multisheller/backend/utils.py#L21 @@ -81,6 +112,11 @@ def generate_scripts_from_multisheller_file(multisheller_file, generation_direct write_script(generation_directory, name_without_extension, cmds, "xonsh") write_script(generation_directory, name_without_extension, cmds, "powershell") + # Conda do not explicitly support different activation scripts for zsh and bash, + # so we generate them as .bash and .zsh (ignored by conda) and then generate a + # small .sh script to source the correct one depending on the used shell + write_bash_zsh_disambiguation_script(generation_directory, name_without_extension) + def main(): # Parse parameters diff --git a/conda/recipe_template/bld.bat b/conda/recipe_template/bld.bat index 17191518d..7f8b70c07 100644 --- a/conda/recipe_template/bld.bat +++ b/conda/recipe_template/bld.bat @@ -34,6 +34,9 @@ for %%F in (activate deactivate) DO ( copy %RECIPE_DIR%\%%F.sh %PREFIX%\etc\conda\%%F.d\%PKG_NAME%_%%F.sh if %errorlevel% neq 0 exit /b %errorlevel% + copy %RECIPE_DIR%\%%F.bash %PREFIX%\etc\conda\%%F.d\%PKG_NAME%_%%F.bash + if %errorlevel% neq 0 exit /b %errorlevel% + copy %RECIPE_DIR%\%%F.ps1 %PREFIX%\etc\conda\%%F.d\%PKG_NAME%_%%F.ps1 if %errorlevel% neq 0 exit /b %errorlevel% diff --git a/conda/recipe_template/build.sh b/conda/recipe_template/build.sh index 17d6cfcd7..0f1e461bc 100644 --- a/conda/recipe_template/build.sh +++ b/conda/recipe_template/build.sh @@ -24,6 +24,7 @@ for CHANGE in "activate" "deactivate" do mkdir -p "${PREFIX}/etc/conda/${CHANGE}.d" cp "${RECIPE_DIR}/${CHANGE}.sh" "${PREFIX}/etc/conda/${CHANGE}.d/${PKG_NAME}_${CHANGE}.sh" + cp "${RECIPE_DIR}/${CHANGE}.bash" "${PREFIX}/etc/conda/${CHANGE}.d/${PKG_NAME}_${CHANGE}.bash" cp "${RECIPE_DIR}/${CHANGE}.xsh" "${PREFIX}/etc/conda/${CHANGE}.d/${PKG_NAME}_${CHANGE}.xsh" cp "${RECIPE_DIR}/${CHANGE}.zsh" "${PREFIX}/etc/conda/${CHANGE}.d/${PKG_NAME}_${CHANGE}.zsh" cp "${RECIPE_DIR}/${CHANGE}.ps1" "${PREFIX}/etc/conda/${CHANGE}.d/${PKG_NAME}_${CHANGE}.ps1"