|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +if [ "$DEBUG" != "" ]; then |
| 6 | + set -x |
| 7 | +fi |
| 8 | + |
| 9 | +script=$(readlink -f "$0") |
| 10 | + |
| 11 | +show_usage() { |
| 12 | + echo "Usage: $script --appdir <path to AppDir>" |
| 13 | + echo |
| 14 | + echo "Bundles software available as conda packages into an AppDir" |
| 15 | +} |
| 16 | + |
| 17 | +_isterm() { |
| 18 | + tty -s && [[ "$TERM" != "" ]] && tput colors &>/dev/null |
| 19 | +} |
| 20 | + |
| 21 | +log() { |
| 22 | + _isterm && tput setaf 3 |
| 23 | + _isterm && tput bold |
| 24 | + echo -*- "$@" |
| 25 | + _isterm && tput sgr0 |
| 26 | + return 0 |
| 27 | +} |
| 28 | + |
| 29 | +APPDIR= |
| 30 | + |
| 31 | +while [ "$1" != "" ]; do |
| 32 | + case "$1" in |
| 33 | + --plugin-api-version) |
| 34 | + echo "0" |
| 35 | + exit 0 |
| 36 | + ;; |
| 37 | + --appdir) |
| 38 | + APPDIR="$2" |
| 39 | + shift |
| 40 | + shift |
| 41 | + ;; |
| 42 | + --help) |
| 43 | + show_usage |
| 44 | + exit 0 |
| 45 | + ;; |
| 46 | + *) |
| 47 | + log "Invalid argument: $1" |
| 48 | + log |
| 49 | + show_usage |
| 50 | + exit 1 |
| 51 | + ;; |
| 52 | + esac |
| 53 | +done |
| 54 | + |
| 55 | +if [ "$APPDIR" == "" ]; then |
| 56 | + show_usage |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +mkdir -p "$APPDIR" |
| 61 | + |
| 62 | +CONDA_DOWNLOAD_DIR="$PWD" |
| 63 | + |
| 64 | +# the user can specify a directory into which the conda installer is downloaded |
| 65 | +# if they don't specify one, we use a temporary directory with a predictable name to preserve downloaded files across runs |
| 66 | +# this should reduce the download overhead |
| 67 | +# if one is specified, the installer will not be re-downloaded unless it has changed |
| 68 | +if [ "$CONDA_DOWNLOAD_DIR" != "" ]; then |
| 69 | + # resolve path relative to cwd |
| 70 | + if [[ "$CONDA_DOWNLOAD_DIR" != /* ]]; then |
| 71 | + CONDA_DOWNLOAD_DIR="$(readlink -f "$CONDA_DOWNLOAD_DIR")" |
| 72 | + fi |
| 73 | + |
| 74 | + log "Using user-specified download directory: $CONDA_DOWNLOAD_DIR" |
| 75 | +else |
| 76 | + # create temporary directory into which downloaded files are put |
| 77 | + CONDA_DOWNLOAD_DIR="/tmp/linuxdeploy-plugin-conda-$(id -u)" |
| 78 | + |
| 79 | + log "Using default temporary download directory: $CONDA_DOWNLOAD_DIR" |
| 80 | +fi |
| 81 | + |
| 82 | +# make sure the directory exists |
| 83 | +mkdir -p "$CONDA_DOWNLOAD_DIR" |
| 84 | + |
| 85 | +# install Miniconda, a self contained Python distribution, into AppDir |
| 86 | +miniconda_installer_filename=Miniconda3-latest-Linux-x86_64.sh |
| 87 | + |
| 88 | +pushd "$CONDA_DOWNLOAD_DIR" |
| 89 | + miniconda_url=https://repo.anaconda.com/miniconda/"$miniconda_installer_filename" |
| 90 | + # let's make sure the file exists before we then rudimentarily ensure mutual exclusive access to it with flock |
| 91 | + # we set the timestamp to epoch 0; this should likely trigger a redownload for the first time |
| 92 | + touch "$miniconda_installer_filename" -d '@0' |
| 93 | + flock "$miniconda_installer_filename" wget -N -c "$miniconda_url" |
| 94 | +popd |
| 95 | + |
| 96 | +export CONDA_ALWAYS_YES="true" |
| 97 | + |
| 98 | +if [ -d "$APPDIR"/usr/conda ]; then |
| 99 | + log "WARNING: conda prefix directory exists: $APPDIR/usr/conda" |
| 100 | + log "Please make sure you perform a clean build before releases to make sure your process works properly." |
| 101 | + |
| 102 | + # activate environment |
| 103 | + . "$APPDIR"/usr/conda/bin/activate |
| 104 | +else |
| 105 | + # install into usr/conda/ instead of usr/ to make sure that the libraries shipped with conda don't overwrite or |
| 106 | + # interfere with libraries bundled by other plugins or linuxdeploy itself |
| 107 | + bash "$CONDA_DOWNLOAD_DIR"/"$miniconda_installer_filename" -b -p "$APPDIR"/usr/conda -f |
| 108 | + . "$APPDIR"/usr/conda/bin/activate |
| 109 | + conda env update -f environment_appimage.yaml |
| 110 | +fi |
| 111 | + |
| 112 | +# build and install local python package |
| 113 | +make docs package |
| 114 | +if [ -d dist ]; then |
| 115 | + find dist -iname "*.whl" | sort | tail -n 1 | xargs -I {} pip3 install --force-reinstall {} |
| 116 | +fi |
| 117 | + |
| 118 | +# create missing symlinks |
| 119 | +mkdir -p "$APPDIR"/usr/bin/ |
| 120 | +mkdir -p "$APPDIR"/usr/lib/ |
| 121 | +pushd "$APPDIR" |
| 122 | +for i in usr/conda/bin/*; do |
| 123 | + rm -f usr/bin/"$(basename "$i")" |
| 124 | + ln -s ../../"$i" usr/bin/ |
| 125 | +done |
| 126 | + |
| 127 | +for i in usr/conda/lib/*; do |
| 128 | + rm -f usr/lib/"$(basename "$i")" |
| 129 | + ln -s ../../"$i" usr/lib/ |
| 130 | +done |
| 131 | + |
| 132 | +rm -f usr/conda/bin/platforms |
| 133 | +ln -s ../plugins/platforms usr/conda/bin |
| 134 | +popd |
| 135 | + |
| 136 | +# disable history substitution, b/c we use ! in quoted strings |
| 137 | +set +H |
| 138 | +APPDIR_FULL="$(pwd)/$APPDIR" |
| 139 | +pushd "$APPDIR_FULL" |
| 140 | + |
| 141 | +# replace absolute paths in some specific files (regex could result in false replacements in other files) |
| 142 | +[ -f usr/conda/etc/profile.d/conda.sh ] && sed -i --follow-symlinks "s|'$APPDIR_FULL|\"\${APPDIR}\"'|g" usr/conda/etc/profile.d/conda.sh |
| 143 | +[ -f usr/conda/etc/profile.d/conda.sh ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/etc/profile.d/conda.sh |
| 144 | +[ -f usr/conda/etc/profile.d/conda.csh ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/etc/profile.d/conda.csh |
| 145 | +[ -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 |
| 146 | + |
| 147 | +# generic files in usr/conda/bin/ and usr/conda/condabin/ |
| 148 | +for i in usr/conda/bin/* usr/conda/condabin/*; do |
| 149 | + if [ -f "$i" ]; then |
| 150 | + # shebangs |
| 151 | + sed -i --follow-symlinks "s|^#!$APPDIR_FULL/usr/conda/bin/|#!/usr/bin/env |" "$i" |
| 152 | + # perl assignments (must be before bash assignments) |
| 153 | + sed -ri --follow-symlinks "s|^(my.*=[[:space:]]*\")$APPDIR_FULL|\1\$ENV{APPDIR} . \"|g" "$i" |
| 154 | + # bash assignments |
| 155 | + sed -ri --follow-symlinks "s|(=[[:space:]]*\")$APPDIR_FULL|\1\${APPDIR}|g" "$i" |
| 156 | + fi |
| 157 | +done |
| 158 | + |
| 159 | +# specific files in usr/conda/bin/ (regex could result in false replacements in other files) |
| 160 | +[ -f usr/conda/bin/python3-config ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/bin/python3-config |
| 161 | +[ -f usr/conda/bin/ncursesw6-config ] && sed -i --follow-symlinks "s|$APPDIR_FULL|\${APPDIR}|g" usr/conda/bin/ncursesw6-config |
| 162 | +[ -f usr/conda/etc/fonts/fonts.conf ] && sed -i --follow-symlinks "s|<cachedir>$APPDIR_FULL|<cachedir>/tmp|g" usr/conda/etc/fonts/fonts.conf |
| 163 | +[ -f usr/conda/etc/fonts/fonts.conf ] && sed -i --follow-symlinks "s|<dir>$APPDIR_FULL|<dir>|g" usr/conda/etc/fonts/fonts.conf |
| 164 | + |
| 165 | +popd |
| 166 | + |
| 167 | +# generate linuxdeploy-plugin-conda-hook |
| 168 | +mkdir -p "$APPDIR"/apprun-hooks |
| 169 | +cat > "$APPDIR"/apprun-hooks/linuxdeploy-plugin-conda-hook.sh <<\EOF |
| 170 | +# generated by linuxdeploy-plugin-conda |
| 171 | +
|
| 172 | +# export APPDIR variable to allow for running from extracted AppDir as well |
| 173 | +export APPDIR="${APPDIR:-$(readlink -f "$(dirname "$0")")}" |
| 174 | +# export PATH to allow /usr/bin/env shebangs to use the supplied applications |
| 175 | +export PATH="$APPDIR"/usr/bin:"$PATH" |
| 176 | +# set font config path |
| 177 | +export FONTCONFIG_PATH="$APPDIR"/usr/conda/etc/fonts |
| 178 | +export FONTCONFIG_FILE="$APPDIR"/usr/conda/etc/fonts/fonts.conf |
| 179 | +EOF |
| 180 | + |
| 181 | +exit 0 |
0 commit comments