Last active
May 8, 2023 16:06
-
-
Save kpedro88/39024c924d39ec1d65eddb9f5619d723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# check for CMSSW environment | |
if [ -z "$CMSSW_BASE" ]; then | |
echo '$CMSSW_BASE not set' | |
exit 1 | |
fi | |
# check for python3 in actual CMSSW area | |
if ! (cd $CMSSW_BASE && scram tool info python3 >& /dev/null); then | |
echo "$CMSSW_VERSION does not provide python3" | |
exit 1 | |
fi | |
# get info from existing python | |
PYPATH=$(which python3) | |
PYHOME=$(dirname $PYPATH) | |
PYVERSION=$(python3 --version | cut -d' ' -f2) | |
PYVERSHORT=$(echo $PYVERSION | cut -d'.' -f1-2) | |
PYVERSHORTER=$(echo $PYVERSION | cut -d'.' -f1) | |
PYNAME=python$PYVERSHORT | |
# create venv directories | |
VENVDIR=$CMSSW_BASE/venv/$SCRAM_ARCH | |
DIRS=( | |
$VENVDIR \ | |
$VENVDIR/bin \ | |
$VENVDIR/include \ | |
$VENVDIR/lib/$PYNAME/site-packages \ | |
) | |
for DIR in ${DIRS[@]}; do | |
mkdir -p $DIR | |
done | |
(cd $VENVDIR; ln -s lib lib64) | |
# create venv config | |
cat << EOF > $VENVDIR/pyvenv.cfg | |
home = $PYHOME | |
include-system-site-packages = true | |
version = $PYVERSION | |
EOF | |
# link venv executables | |
EXES=( | |
python$PYVERSHORTER \ | |
python$PYVERSHORT \ | |
) | |
for EXE in ${EXES[@]}; do | |
ln -s $PYPATH $VENVDIR/bin/$EXE | |
done | |
# set up scram hook infrastructure if missing | |
HOOKDIR=$CMSSW_BASE/config/SCRAM/hooks/runtime | |
HOOKFILE=${HOOKDIR}-hook | |
if [ ! -f "$HOOKFILE" ]; then | |
mkdir -p $HOOKDIR | |
cat << 'EOF' > $HOOKFILE | |
#!/bin/bash | |
SCRIPT_DIR=$(dirname $0) | |
if [ -e ${SCRIPT_DIR}/runtime ] ; then | |
for tool in $(find ${SCRIPT_DIR}/runtime -type f | sort) ; do | |
[ -x $tool ] && $tool | |
done | |
fi | |
EOF | |
chmod +x $HOOKFILE | |
fi | |
# install hook for venv | |
HOOK=${HOOKDIR}/py3-venv | |
cat << 'EOF' > $HOOK | |
#!/bin/bash | |
echo "RUNTIME:path:prepend:PATH=${LOCALTOP}/venv/$SCRAM_ARCH/bin" | |
EOF | |
chmod +x $HOOK | |
# instructions to activate venv hook and update environment | |
echo "scram-venv succeeded! please call 'cmsenv' now" |
PR opened at cms-sw/cms-common#6
@kpedro88 @smuzaffar This is great! Would be good to have some documentation on how to use it, e.g. somewhere at https://cms-sw.github.io/, now that it's merged.
agree @clelange . @kpedro88 can you please open a PR for https://github.com/cms-sw/cms-sw.github.io/tree/code ?
Will do - after my CHEP plenary today...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@smuzaffar the symlink is needed for the venv to be recognized: PEP 405:
The
home
area needs to be writable, so we need to link to thecvmfs
(read-only) Python executable in the local working area.Only
PATH
that changes here;LD_LIBRARY_PATH
andPYTHON3PATH
do not need to be updated for this to work. So it's actually less overhead than adding a new external package...However, after thinking about it more, I agree that it's a good idea to start as an optional feature in cms-common so that users can test it and find any edge cases (and we avoid any interference with production in the meantime). This also gives CRAB time to update, etc. Maybe in the future it can be propagated to
scram project
.