Last active
November 13, 2017 10:17
-
-
Save jacopofar/ab648c47fa3879ba4057cca2c8b95149 to your computer and use it in GitHub Desktop.
Create a virtualenv based on the hash of a requirements.txt file if it does not exist, and softlink it
This file contains hidden or 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/sh | |
# processes a requirements.txt file, creates the virtualenv in a given folder | |
# if it does not exist already and creates a symlink to that | |
if [[ ! $# -eq 3 ]] ; then | |
echo 'Parameters error' 1>&2 | |
echo 'Required: the requirements.txt path, the folder for the virtualenvs and the symlink to create' 1>&2 | |
exit 1 | |
fi | |
set -x | |
# Use sha256sum if present (Linux), shasum otherwise (MacOS) | |
if hash sha256sum 2>/dev/null; then | |
REQUIREMENTS_HASH="$(sort $1|sha256sum | cut -d ' ' -f 1)" | |
else | |
if hash shasum 2>/dev/null; then | |
REQUIREMENTS_HASH="$(sort $1|shasum -a 256 | cut -d ' ' -f 1)" | |
else | |
echo "Error! No sha256sum nor shasum command found, cannot calculate hash of requirements file" 1>&2 | |
exit 2 | |
fi | |
fi | |
VENV_TARGET_DIR="${2%/}/$REQUIREMENTS_HASH" | |
# NOTE: this ignores symlinks, the venv directory must be there | |
if [ ! -d $VENV_TARGET_DIR ]; then | |
python3 -m venv $VENV_TARGET_DIR | |
PIP_COMMAND="$VENV_TARGET_DIR/bin/pip install -r $1" | |
eval $PIP_COMMAND | |
if [ ! $? -eq 0 ]; then | |
echo 'Error while installing the packages in virtualenv, deleting it' 1>&2 | |
rm -rf $VENV_TARGET_DIR | |
fi | |
if [$(uname -s) -eq 'Linux'] then | |
# On Linux, sometimes the system has its own urllib3 wheel and we do not want that | |
$VENV_TARGET_DIR/bin/pip install --upgrade requests urllib3 && rm -vf $VENV_TARGET_DIR/share/python-wheels/{urlib3,chardet,requests}*.whl || echo 'wheel removal failed, probably the files were already not there' | |
fi | |
esac | |
else | |
echo "virtualenv dir already exists" | |
fi | |
# create a softlink, overwrite it if existing | |
ln -f -s $VENV_TARGET_DIR $3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment