#!/bin/bash set -x; if [ -d /projects/nlpl ]; then host="abel"; root="/projects/nlpl"; suffix="tcl"; elif [ -d /proj/nlpl ]; then host="taito"; root="/proj/nlpl"; suffix="lua"; elif [ -d /cluster/shared/nlpl ]; then host="saga"; root="/cluster/shared/nlpl"; suffix="lua"; elif [ -d /projappl/nlpl ]; then host="puhti"; root="/projappl/nlpl"; suffix="lua"; fi module use -a ${root}/software/modules/etc; if [ -z "${host}" ]; then echo "unable to locate NLPL project directory; exit." exit 1; fi python="python3"; version=$(command date "+%Y%m"); tmp="/tmp/.initialize.${USER}.$$"; while [ $# -gt 0 -a "${1#-}" != "$1" ]; do case ${1} in --p*) python=${2}; shift 2; ;; --v*) version=${2}; shift 2; ;; --nvidia) nvidia=yes; shift 1; ;; *) echo "initialize: invalid option \`${1}'; exit."; exit 1; ;; esac done name=${1} if [ -z "${name}" ]; then echo "initialize: missing obligatory ‘name’ argument; exit."; exit 1; fi log="${root}/operation/log/${host}/python.${name}.$(date "+%y-%m-%d-%H:%M")"; { if [ ${version} = "pypi" ]; then case ${name} in pytorch) key="torch"; ;; *) key=${name}; ;; esac; version=$(command python3 -m pip install ${key}== 2>&1 \ | grep "Could not find" \ | awk '{print $NF}' | sed 's/)$//'); fi if [ -z "$(type -t ${python})" ]; then echo "initialize: invalid interpreter ‘${python}’; exit."; exit 1; fi implementation=$(${python} --version 2>&1 | sed 's/^Python //'); dialect=$(${python} --version 2>&1 | sed 's/^Python \([0-9]\.[0-9]\).*$/\1/'); family=$(${python} --version 2>&1 | sed 's/^Python \([0-9]\)\..*$/\1/'); interpreter="python${dialect}"; requirements="${root}/operation/python/${name}.txt"; pip="./${interpreter} -m pip"; if [ ${host} = "abel" -o ${host} = "taito" ]; then target="${root}/software/${name}/${version}"; definition="${root}/software/modulefiles/nlpl-${name}/${version}/${dialect}"; else target="${root}/software/modules/${name}/${version}"; definition="${root}/software/modules/etc/nlpl-${name}/${version}/${dialect}.lua"; fi if [ -f ${definition} ]; then echo "initialize: existing ‘nlpl-${name}/${version}/${dialect}’; exit."; exit 1; fi command mkdir -pv ${target}/bin; command mkdir -pv $(dirname ${definition}); cd ${target}/bin; command ls > ${tmp}; ${python} -m virtualenv --always-copy ${target}; # # if need be, enable our own more modern GLIBC environment # if [ ${host} = "abel" -o ${host} = "taito" ]; then command mv ${interpreter} .${interpreter}; command cp -va ${root}/software/glibc/2.23/wrapper ${interpreter}; command rm python${family}; command ln -s ${interpreter} python${family}; fi # # the 3.7.3 (anaconda-based) Python on Puhti does not seem to provide # the most specific reference to the interpreter # [ ! -f ./${interpreter} ] && command cp python${family} ${interpreter}; if false; then source activate command ${pip} install --upgrade pip command ${pip} install -U $(command ${pip} list | command tail -n +3 | command awk '{print $1}'); if [ "$(type -t deactivate)" = "function" ]; then deactivate; else source deactivate; fi fi # # our in-house definition files can contain shell directives # command egrep '^#\$' ${requirements} > ${tmp}; while read line; do eval ${line##\#\$ }; done < ${tmp}; if [ -n "${nvidia}" -a -f /usr/lib64/libcuda.so ]; then command cp -av /usr/lib64/libcuda.so* /usr/lib64/libnvidia* ../lib; fi command sed \ -e "s@__root__@${root}@g" \ -e "s/__name__/${name}/g" \ -e "s/__version__/${version}/g" \ -e "s/__dialect__/${dialect}/g" \ -e "s/__family__/${family}/g" \ ${root}/operation/python/prologue.${suffix} \ | grep -v "VIRTUAL_ENV" > ${definition}; { echo; for i in $(set +x; module --terse list 2>&1 | grep -v "Currently Loaded"); do if [ ${suffix} = "tcl" ]; then echo "module load ${i}"; else echo "if not (isloaded(\"${i}\")) then"; echo " load(\"${i}\")"; echo "end"; fi done echo; if [ ${suffix} = "tcl" ]; then echo "prepend-path PYTHONPATH \$root/\$name/\$version/lib/python${dialect}/site-packages"; echo "prepend-path LD_LIBRARY_PATH \$root/\$name/\$version/lib"; echo "prepend-path PATH \$root/\$name/\$version/bin/${dialect}"; else echo "prepend_path(\"PYTHONPATH\", pathJoin(root, name, version, \"lib/python${dialect}/site-packages\"))"; echo "prepend_path(\"LD_LIBRARY_PATH\", pathJoin(root, name, version, \"lib\"))"; echo "prepend_path(\"PATH\", pathJoin(root, name, version, \"bin/${dialect}\"))"; fi echo; case ${name} in nltk) if [ ${suffix} = "tcl" ]; then echo "setenv NLTK_DATA \$root/nltk/\$version/data"; else echo "setenv(\"NLTK_DATA\", pathJoin(root, \"nltk\", version, \"data\"))"; fi echo; ;; esac } >> ${definition}; # # our in-house definition files can contain configuration snippets # command egrep '^#>' ${requirements} \ | command sed 's/^#> *//g' >> ${definition}; command cat ${root}/operation/python/epilogue.${suffix} >> ${definition}; module --ignore-cache load nlpl-${name}/${version}/${dialect}; ${pip} install --upgrade pip; if [ -f ${requirements} ]; then ${pip} install --upgrade -r ${requirements}; case ${name} in nltk) if [ ${host} = "abel" -o ${host} = "taito" ]; then data="${root}/software/nltk/${version}/data"; else data="${root}/software/modules/nltk/${version}/data"; fi if [ ! -d ${data} ]; then ${target}/bin/${interpreter} -m nltk.downloader -d ${data} all; fi ;; spacy) for i in en de es pt fr it nl xx; do ${target}/bin/${interpreter} -m spacy download ${i}; done ;; esac fi cd ${target}/bin; command mkdir ./${dialect}; [ -f .${interpreter} ] && command mv .${interpreter} ./${dialect}; for i in $(command ls); do if [ -f ${i} -a -z "$(command egrep "^${i}$" ${tmp})" ]; then if [ -h ${i} ]; then command mv ${i} ./${dialect}; else if [ ${host} = "abel" ]; then sed="s@${name}/${version}/bin/.python${dialect}\$"; sed="${sed}@${name}/${version}/bin/${dialect}/python${dialect}@"; else sed="s@/${name}/${version}/bin/python"; sed="${sed}@/${name}/${version}/bin/${dialect}/python@"; fi command sed -e ${sed} ${i} > ./${dialect}/${i}; command chmod +x ./${dialect}/${i}; command rm -f ${i}; fi fi done cd ${dialect}; if [ -f python -a -f .python${dialect} ]; then if command cmp python .python${dialect}; then command rm python; command ln -s python${dialect} python; fi fi #command rm -f ${tmp}; } 2>&1 | command tee ${log}