#!/usr/bin/env bash
set -e
REQUIREMENTS_FILE="./py/requirements.txt"
VENV="./temp_virtualenv"
cd "$(git rev-parse --show-toplevel)"
if [[ ! -f "${REQUIREMENTS_FILE}" ]]; then
echo "can't find: ${REQUIREMENTS_FILE}"
exit 1
fi
if [[ -d "${VENV}" ]]; then
echo "${VENV} already exists"
exit 1
fi
echo "creating virtual env: ${VENV}"
python3 -m venv "${VENV}"
echo "activating virtual env"
source "${VENV}/bin/activate"
echo "upgrading pip"
python -m pip install --upgrade pip > /dev/null
echo "installing dev dependencies from: ${REQUIREMENTS_FILE}"
pip install -r "${REQUIREMENTS_FILE}" > /dev/null
echo "upgrading outdated dependencies ..."
echo
pip list --outdated | while read -r line; do
if [[ ! "${line}" =~ "Version Latest" && ! "${line}" =~ "----" ]]; then
read -ra fields <<< "${line}"
package="${fields[0]}"
echo "upgrading ${package} from ${fields[1]} to ${fields[2]}"
pip install --upgrade "${package}==${fields[2]}" > /dev/null
fi
done
echo
echo "generating new ${REQUIREMENTS_FILE}"
pip freeze > "${REQUIREMENTS_FILE}"
if [[ "${OSTYPE}" == "linux"* ]]; then
sed -i "s/urllib3/urllib3[socks]/g" "${REQUIREMENTS_FILE}"
else
sed -i "" "s/urllib3/urllib3[socks]/g" "${REQUIREMENTS_FILE}"
fi
echo "generating new lock file"
bazel run //py:requirements.update
echo
echo "deleting virtual env: ${VENV}"
deactivate
rm -rf "${VENV}"
echo
git status
echo
echo "done!"