Skip to content

Commit

Permalink
Fix support for zsh in conda binary activation scripts (robotology#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
traversaro authored and Nicogene committed Feb 14, 2022
1 parent 6063d64 commit 33a3ff4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/generate-conda-packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
38 changes: 37 additions & 1 deletion conda/python/generate_conda_recipes_from_metametadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def dir_path(string):

suffixes = {
'cmdexe': '.bat',
'bash': '.sh',
'bash': '.bash',
'zsh': '.zsh',
'xonsh': '.xsh',
'powershell': '.ps1',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions conda/recipe_template/bld.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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%

Expand Down
1 change: 1 addition & 0 deletions conda/recipe_template/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 33a3ff4

Please sign in to comment.