|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | # Release script for Python buildpack with uv support |
3 | 3 |
|
| 4 | +if command -v python3 >/dev/null 2>&1; then |
| 5 | + PYTHON_BIN="python3" |
| 6 | +elif command -v python >/dev/null 2>&1; then |
| 7 | + PYTHON_BIN="python" |
| 8 | +else |
| 9 | + PYTHON_BIN="python" |
| 10 | +fi |
| 11 | + |
4 | 12 | # If the app provides its own Procfile, let Cloud Foundry use that. |
5 | 13 | if [ -f "Procfile" ]; then |
6 | 14 | exit 0 |
7 | | -# Otherwise, choose a reasonable default entrypoint based on common app files. |
| 15 | +# Otherwise, prefer a `start` entry under `[project.scripts]` in pyproject.toml. |
| 16 | +elif [ -n "$PYTHON_BIN" ] && [ -f "pyproject.toml" ]; then |
| 17 | + START_TARGET=$("$PYTHON_BIN" -c ' |
| 18 | +import sys |
| 19 | +try: |
| 20 | + import tomllib |
| 21 | +except ModuleNotFoundError: |
| 22 | + import tomli as tomllib |
| 23 | +
|
| 24 | +with open("pyproject.toml", "rb") as f: |
| 25 | + data = tomllib.load(f) |
| 26 | +
|
| 27 | +print(data.get("project", {}).get("scripts", {}).get("start", "")) |
| 28 | +') |
| 29 | + |
| 30 | + if [ -n "$START_TARGET" ]; then |
| 31 | + MODULE_NAME="${START_TARGET%%:*}" |
| 32 | + FUNCTION_NAME="${START_TARGET##*:}" |
| 33 | + DEFAULT_WEB_PROCESS="${PYTHON_BIN} -c \"from ${MODULE_NAME} import ${FUNCTION_NAME}; ${FUNCTION_NAME}()\"" |
| 34 | + elif [ -f "main.py" ]; then |
| 35 | + DEFAULT_WEB_PROCESS="${PYTHON_BIN} main.py" |
| 36 | + elif [ -f "app.py" ]; then |
| 37 | + DEFAULT_WEB_PROCESS="${PYTHON_BIN} app.py" |
| 38 | + else |
| 39 | + DEFAULT_WEB_PROCESS="${PYTHON_BIN} -m uvicorn main:app --host 0.0.0.0 --port \${PORT:-8000}" |
| 40 | + fi |
| 41 | +# If pyproject.toml is missing or cannot be parsed here, fall back to common app files. |
8 | 42 | elif [ -f "main.py" ]; then |
9 | | - DEFAULT_WEB_PROCESS="python main.py" |
| 43 | + DEFAULT_WEB_PROCESS="${PYTHON_BIN} main.py" |
10 | 44 | elif [ -f "app.py" ]; then |
11 | | - DEFAULT_WEB_PROCESS="python app.py" |
| 45 | + DEFAULT_WEB_PROCESS="${PYTHON_BIN} app.py" |
12 | 46 | else |
13 | | - DEFAULT_WEB_PROCESS="python -m uvicorn main:app --host 0.0.0.0 --port \${PORT:-8000}" |
| 47 | + DEFAULT_WEB_PROCESS="${PYTHON_BIN} -m uvicorn main:app --host 0.0.0.0 --port \${PORT:-8000}" |
14 | 48 | fi |
15 | 49 |
|
16 | 50 | cat <<EOF |
|
0 commit comments