|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | + |
| 5 | +PYPI_REPO="${PYPI_REPO:-testpypi}" # "pypi" or "testpypi" |
| 6 | +PYPI_TOKEN="${PYPI_TOKEN:-}" # export PYPI_TOKEN="pypi-***" (or test token) |
| 7 | +PYTHON="${PYTHON:-python}" # "python" or "python3" |
| 8 | +DIST_DIR="dist" |
| 9 | +PYPROJECT="pyproject.toml" |
| 10 | + |
| 11 | + |
| 12 | +if [[ ! -f "$PYPROJECT" ]]; then |
| 13 | + echo "$PYPROJECT not found (are you in the project root?)" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +if [[ -z "$PYPI_TOKEN" ]]; then |
| 18 | + echo "PYPI_TOKEN not set. Run: export PYPI_TOKEN='pypi-XXXX...'" |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +# Read project metadata (name & version) from pyproject.toml using Python |
| 23 | +read_pyproject() { |
| 24 | + "$PYTHON" - <<'PY' |
| 25 | +import sys, pathlib |
| 26 | +pp = pathlib.Path("pyproject.toml") |
| 27 | +if not pp.exists(): |
| 28 | + print("unknown|unknown"); sys.exit(0) |
| 29 | +try: |
| 30 | + import tomllib |
| 31 | +except ModuleNotFoundError: |
| 32 | + # Python <3.11 fallback |
| 33 | + import json, re |
| 34 | + # very small fallback parser: good enough to grab name/version in simple cases |
| 35 | + text = pp.read_text() |
| 36 | + name = re.search(r'(?m)^\s*name\s*=\s*"([^"]+)"', text) |
| 37 | + ver = re.search(r'(?m)^\s*version\s*=\s*"([^"]+)"', text) |
| 38 | + print(f"{name.group(1) if name else 'unknown'}|{ver.group(1) if ver else 'unknown'}") |
| 39 | + sys.exit(0) |
| 40 | +
|
| 41 | +data = tomllib.loads(pp.read_bytes()) |
| 42 | +proj = data.get("project", {}) |
| 43 | +print(f"{proj.get('name','unknown')}|{proj.get('version','unknown')}") |
| 44 | +PY |
| 45 | +} |
| 46 | + |
| 47 | +IFS="|" read -r PKG_NAME PKG_VER < <(read_pyproject) |
| 48 | +echo "Package: ${PKG_NAME} Version: ${PKG_VER}" |
| 49 | + |
| 50 | + |
| 51 | +echo "Cleaning old builds..." |
| 52 | +rm -rf "$DIST_DIR" build ./*.egg-info |
| 53 | + |
| 54 | + |
| 55 | +echo "Installing build tools..." |
| 56 | +$PYTHON -m pip install -q --upgrade pip build twine |
| 57 | + |
| 58 | +echo "Building sdist & wheel..." |
| 59 | +$PYTHON -m build # uses hatchling per your [build-system] |
| 60 | + |
| 61 | +echo "Validating with twine..." |
| 62 | +$PYTHON -m twine check "$DIST_DIR"/* |
| 63 | + |
| 64 | + |
| 65 | +if [[ "$PYPI_REPO" == "testpypi" ]]; then |
| 66 | + REPO_URL="https://test.pypi.org/legacy/" |
| 67 | + echo "Target: TestPyPI" |
| 68 | +else |
| 69 | + REPO_URL="https://upload.pypi.org/legacy/" |
| 70 | + echo "Target: PyPI" |
| 71 | +fi |
| 72 | + |
| 73 | +# ----------------------------- |
| 74 | +# Upload |
| 75 | +# ----------------------------- |
| 76 | +echo "Uploading distributions..." |
| 77 | +$PYTHON -m twine upload \ |
| 78 | + --non-interactive \ |
| 79 | + --repository-url "$REPO_URL" \ |
| 80 | + -u __token__ \ |
| 81 | + -p "$PYPI_TOKEN" \ |
| 82 | + "$DIST_DIR"/* |
| 83 | + |
| 84 | +echo "Published ${PKG_NAME} ${PKG_VER} to ${PYPI_REPO}" |
0 commit comments