Skip to content

Commit f56f125

Browse files
committed
chore(common): improve compile to use lock file and buildpack env features
1 parent 6564e9e commit f56f125

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

bin/compile

100644100755
Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,51 @@ BUILD_DIR=$1
66
CACHE_DIR=$2
77
ENV_DIR=$3
88

9+
# Cloud Foundry environments may expose either `python3` or `python`,
10+
# so resolve one interpreter once and reuse it for the whole build.
11+
if command -v python3 >/dev/null 2>&1; then
12+
PYTHON_BIN="python3"
13+
elif command -v python >/dev/null 2>&1; then
14+
PYTHON_BIN="python"
15+
else
16+
echo "Python interpreter not found. Expected python3 or python on PATH."
17+
exit 1
18+
fi
19+
920
cd "$BUILD_DIR"
1021

11-
# Install Python if not present (optional: add logic here)
22+
# Only treat the app as a supported uv project when both the project
23+
# metadata and the lockfile are present.
24+
if [ -f "pyproject.toml" ] && [ -f "uv.lock" ]; then
25+
echo "Detected uv project with lockfile. Installing dependencies with uv."
26+
# Keep installer caches inside the buildpack cache directory so repeated
27+
# builds are faster and do not depend on user-specific cache locations.
28+
export PIP_CACHE_DIR="$CACHE_DIR/pip"
29+
export UV_CACHE_DIR="$CACHE_DIR/uv"
30+
31+
mkdir -p "$PIP_CACHE_DIR" "$UV_CACHE_DIR"
32+
33+
"$PYTHON_BIN" -m pip install uv
34+
35+
PYTHON_VERSION=$("$PYTHON_BIN" -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
36+
# Stage dependencies into a buildpack-owned location instead of a local venv.
37+
SITE_PACKAGES_DIR="$BUILD_DIR/.python_packages/lib/python${PYTHON_VERSION}/site-packages"
38+
PROFILE_DIR="$BUILD_DIR/.profile.d"
39+
EXPORT_FILE="$BUILD_DIR/.uv-export-requirements.txt"
40+
41+
mkdir -p "$SITE_PACKAGES_DIR" "$PROFILE_DIR"
42+
43+
# Export the exact locked dependency set from uv, then install those
44+
# packages into the staged site-packages directory.
45+
uv export --locked --format requirements-txt -o "$EXPORT_FILE"
46+
"$PYTHON_BIN" -m pip install --no-deps --target "$SITE_PACKAGES_DIR" -r "$EXPORT_FILE"
1247

13-
# Detect and install uv
14-
if grep -q '\[tool.uv\]' pyproject.toml || grep -q '\[project\]' pyproject.toml; then
15-
echo "Detected uv. Installing dependencies with uv."
16-
pip install uv
17-
uv pip install -r requirements.txt || uv pip install
48+
# Add the staged packages to PYTHONPATH so the app can import them at runtime.
49+
cat > "$PROFILE_DIR/python.sh" <<EOF
50+
export PYTHONPATH="$SITE_PACKAGES_DIR:\${PYTHONPATH}"
51+
EOF
1852
else
19-
echo "No supported dependency manager found."
53+
echo "No supported uv project found. Expected both pyproject.toml and uv.lock."
2054
exit 1
2155
fi
22-
56+

0 commit comments

Comments
 (0)