-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_python.sh
More file actions
executable file
·68 lines (62 loc) · 2.9 KB
/
Copy pathsetup_python.sh
File metadata and controls
executable file
·68 lines (62 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
#
# setup_python.sh — Create the pipeline's Python virtual environment
# ==================================================================
# Builds tools/venv/ from requirements.txt. Every Python stage is invoked as
# "${PYTHON}" <script>.py, so the system Python is never touched and nothing is
# installed globally.
#
# Versions are pinned exactly: LDSC in particular is sensitive to the numpy and
# pandas major versions, and pinning is what makes a re-run reproducible.
#
# Idempotent: exits immediately if the environment already imports cleanly.
#
# Usage:
# bash setup_python.sh
#
set -euo pipefail
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
VENV_DIR="${PROJECT_DIR}/tools/venv"
PYTHON_BIN="${VENV_DIR}/bin/python"
REQUIREMENTS="${PROJECT_DIR}/requirements.txt"
# ---------------------------------------------------------------------------
# Skip if already built
# ---------------------------------------------------------------------------
if [[ -x "${PYTHON_BIN}" ]] && "${PYTHON_BIN}" -c \
"import numpy, pandas, scipy, matplotlib, openpyxl, PIL, bitarray" \
> /dev/null 2>&1; then
echo " [skip] Python environment already complete — $("${PYTHON_BIN}" --version)"
exit 0
fi
# ---------------------------------------------------------------------------
# Build
# ---------------------------------------------------------------------------
BASE_PYTHON="${BASE_PYTHON:-python3}"
if ! command -v "${BASE_PYTHON}" > /dev/null 2>&1; then
echo "Error: ${BASE_PYTHON} not found. Install Python 3.9 or newer." >&2
exit 1
fi
# requirements.txt pins numpy 1.26.4 / scipy 1.14.1 / bitarray 2.9.2, whose
# published wheels cover CPython 3.10-3.12 only. Outside that window pip falls
# back to source builds that fail several minutes later, blaming the wrong
# package -- so refuse up front with an actionable message.
if ! "${BASE_PYTHON}" -c 'import sys; sys.exit(0 if (3,10) <= sys.version_info < (3,13) else 1)'; then
echo "Error: $("${BASE_PYTHON}" --version) is not supported." >&2
echo " The pinned dependencies need Python 3.10-3.12." >&2
echo " Re-run with BASE_PYTHON=python3.12 bash setup_python.sh, or use Docker." >&2
exit 1
fi
echo " Creating virtual environment with $("${BASE_PYTHON}" --version) ..."
"${BASE_PYTHON}" -m venv "${VENV_DIR}"
"${PYTHON_BIN}" -m pip install --quiet --no-cache-dir --upgrade pip
"${PYTHON_BIN}" -m pip install --quiet --no-cache-dir -r "${REQUIREMENTS}"
if ! "${PYTHON_BIN}" -c \
"import numpy, pandas, scipy, matplotlib, openpyxl, PIL, bitarray" \
> /dev/null 2>&1; then
echo "Error: the virtual environment is missing required packages." >&2
exit 1
fi
echo " -> $("${PYTHON_BIN}" --version) with $(grep -c . "${REQUIREMENTS}") pinned packages"