Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix absolute paths in scripts #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions linuxdeploy-plugin-conda.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fi

script=$(readlink -f "$0")

CONDA_SKIP_ADJUST_PATHS=${CONDA_SKIP_ADJUST_PATHS:-"1"}
ARCH="${ARCH:-"$(uname -m)"}"

show_usage() {
Expand All @@ -23,6 +24,7 @@ show_usage() {
echo " PIP_REQUIREMENTS=\"packageA packageB -r requirements.txt -e git+https://...\""
echo " PIP_PREFIX=\"AppDir/usr/share/conda\""
echo " ARCH=\"$ARCH\" (supported values: x86_64, i368, i686)"
echo " CONDA_SKIP_ADJUST_PATHS=\"1\" (default: skip)"
echo " CONDA_SKIP_CLEANUP=\"[all;][conda-pkgs;][__pycache__;][strip;][.a;][cmake;][doc;][man;][site-packages;]\""
}

Expand Down Expand Up @@ -187,6 +189,45 @@ for i in usr/conda/bin/*; do
done
popd

# adjust absolute paths, by default skipped via $CONDA_SKIP_ADJUST_PATHS
if [ "$CONDA_SKIP_ADJUST_PATHS" != "1" ]; then
# disable history substitution, b/c we use ! in quoted strings
set +H
APPDIR_FULL="$(pwd)/$APPDIR"
pushd "$APPDIR_FULL"
# NOTE: --follow-symlinks is only working for GNU sed
# replace absolute paths in some specific files (regex could result in false replacements in other files)
[ -f usr/conda/etc/profile.d/conda.sh ] && sed -i --follow-symlinks "s|'$APPDIR_FULL|\"\${APPDIR}\"'|g" usr/conda/etc/profile.d/conda.sh
[ -f usr/conda/etc/profile.d/conda.sh ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/etc/profile.d/conda.sh
[ -f usr/conda/etc/profile.d/conda.csh ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/etc/profile.d/conda.csh
[ -f usr/conda/etc/fish/conf.d/conda.fish ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\$APPDIR|g" usr/conda/etc/fish/conf.d/conda.fish
# generic files in usr/conda/bin/ and usr/conda/condabin/
for i in usr/conda/bin/* usr/conda/condabin/*; do
[ -f "$i" ] || continue
# shebangs
sed -i --follow-symlinks "s|^#!$APPDIR_FULL/usr/conda/bin/|#!/usr/bin/env |" "$i"
# perl assignments (must be before bash assignments)
sed -ri --follow-symlinks "s|^(my.*=[[:space:]]*\")$APPDIR_FULL|\1\$ENV{APPDIR} . \"|g" "$i"
# bash assignments
sed -ri --follow-symlinks "s|(=[[:space:]]*\")$APPDIR_FULL|\1\${APPDIR}|g" "$i"
done
# specific files in usr/conda/bin/ (regex could result in false replacements in other files)
[ -f usr/conda/bin/python3-config ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/bin/python3-config
[ -f usr/conda/bin/ncursesw6-config ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/bin/ncursesw6-config
popd

# generate linuxdeploy-plugin-conda-hook
mkdir -p "$APPDIR"/apprun-hooks
cat > "$APPDIR"/apprun-hooks/linuxdeploy-plugin-conda-hook.sh <<\EOF
# generated by linuxdeploy-plugin-conda

# export APPDIR variable to allow for running from extracted AppDir as well
export APPDIR="${APPDIR:-$(readlink -f "$(dirname "$0")")}"
# export PATH to allow /usr/bin/env shebangs to use the supplied applications
export PATH="$APPDIR"/usr/bin:"$PATH"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break calling any sorts of external tools, because they'll then also have to use all tools contained in the AppDir. Can you think of a different solution perhaps? The use of environment variables like PATH and LD_LIBRARY_PATH is actually an antipattern and has to be done with great care.

To limit the effects, one possible solution would be to create a separate bin dir somewhere in the AppDir that only points to the bundled Python binary/binaries, but none of the other stuff in AppDir/usr/bin. But that's also a hack. I think you could even further limit the effects by generating some sort of unique name for the symlink to the main binary. Then, you can safely use /usr/bin/env linuxdeploy-plugin-conda-python or something like that with bundled tools.

Of course, bundled scripts that just point to /usr/bin/env python would still fail to run. But I'm not sure that issue can be fixed.

EOF

fi

# remove bloat, optionally skipped via $CONDA_SKIP_CLEANUP
IFS=';' read -ra cleanup <<< "$CONDA_SKIP_CLEANUP"
Expand Down