|
| 1 | +# code: language=shellscript insertSpaces=true tabSize=2 |
| 2 | +# shellcheck disable=SC2148 |
| 3 | + |
| 4 | +dotenv_if_exists |
| 5 | + |
| 6 | +if test -z "${PYTHON_VERSION:-}" && test -f "${PWD}/.python-version"; then |
| 7 | + PYTHON_VERSION=$(head -n 1 "${PWD}/.python-version") |
| 8 | +fi |
| 9 | + |
| 10 | +if test -z "${PYTHON_VERSION:-}"; then |
| 11 | + PYTHON_VERSION=$(semver_search /usr/lib python 3) |
| 12 | +fi |
| 13 | + |
| 14 | +if test -z "${PYTHON_VERSION:-}"; then |
| 15 | + echo "-> No Python version found. Please create a .python-version file or set PYTHON_VERSION in your environment." >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +export VIRTUAL_ENV="${PWD}/.venv" |
| 20 | +layout python "python${PYTHON_VERSION}" |
| 21 | + |
| 22 | +echo '*' > "${PWD}/.venv/.gitignore" |
| 23 | + |
| 24 | +if ! test -f "${PWD}/.venv/.pip-updated"; then |
| 25 | + pip install --upgrade pip |
| 26 | + touch "${PWD}/.venv/.pip-updated" |
| 27 | +fi |
| 28 | + |
| 29 | +if test -z "${PYTHON_NO_INSTALL_REQUIREMENTS:-}"; then |
| 30 | + all_req_files=("${PWD}"/requirements*.txt) |
| 31 | + if [[ ${#all_req_files[@]} -gt 0 ]]; then |
| 32 | + |
| 33 | + install_needed=0 |
| 34 | + |
| 35 | + for req_file in "${all_req_files[@]}"; do |
| 36 | + watch_file "$req_file" |
| 37 | + |
| 38 | + venv_file="${PWD}/.venv/.$(basename "$req_file")" |
| 39 | + if ! test -f "$venv_file" || test "$venv_file" -ot "$req_file"; then |
| 40 | + install_needed=1 |
| 41 | + fi |
| 42 | + done |
| 43 | + |
| 44 | + if [[ "$install_needed" -eq 1 ]]; then |
| 45 | + pip_install_args=() |
| 46 | + for req_file in "${all_req_files[@]}"; do |
| 47 | + pip_install_args+=("-r" "$req_file") |
| 48 | + done |
| 49 | + |
| 50 | + if pip install -U "${pip_install_args[@]}"; then |
| 51 | + for req_file in "${all_req_files[@]}"; do |
| 52 | + venv_file="${PWD}/.venv/.$(basename "$req_file")" |
| 53 | + touch "$venv_file" |
| 54 | + done |
| 55 | + else |
| 56 | + echo "-> pip install command failed. Tracking files remain untouched." >&2 |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + fi |
| 60 | + fi |
| 61 | +fi |
| 62 | + |
| 63 | +if test -f "${PWD}/.pre-commit-config.yaml" && ! test -f "${PWD}/.venv/.pre-commit-installed"; then |
| 64 | + if test -z "$(pip list 2>/dev/null | grep -oE 'pre[-_]commit')"; then |
| 65 | + pip install pre-commit |
| 66 | + fi |
| 67 | + pre-commit install |
| 68 | + touch "${PWD}/.venv/.pre-commit-installed" |
| 69 | +fi |
0 commit comments