|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# End-to-end release/build script for SerialUI + line_parsers. |
| 5 | +# |
| 6 | +# What it does: |
| 7 | +# 1) Installs/upgrades build tooling |
| 8 | +# 2) Builds C extensions in helpers/line_parsers |
| 9 | +# 3) Builds wheel/sdist for helpers package (line_parsers) |
| 10 | +# 4) Runs twine check |
| 11 | +# 5) Reinstalls the freshly built wheel locally |
| 12 | +# 6) Optionally uploads to PyPI |
| 13 | +# 7) Builds standalone app via PyInstaller spec |
| 14 | +# |
| 15 | +# Usage: |
| 16 | +# ./build_release.sh |
| 17 | +# |
| 18 | +# Optional environment variables: |
| 19 | +# PYTHON_BIN=python3 |
| 20 | +# PACKAGE_NAME=line_parsers |
| 21 | +# UPLOAD_PYPI=1 |
| 22 | +# TWINE_USERNAME=__token__ |
| 23 | +# TWINE_PASSWORD=<your-token> |
| 24 | +# TWINE_REPOSITORY_URL=https://upload.pypi.org/legacy/ |
| 25 | + |
| 26 | +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 27 | +HELPERS_DIR="${ROOT_DIR}/helpers" |
| 28 | +PYTHON_BIN="${PYTHON_BIN:-python3}" |
| 29 | +PACKAGE_NAME="${PACKAGE_NAME:-line_parsers}" |
| 30 | +UPLOAD_PYPI="${UPLOAD_PYPI:-0}" |
| 31 | +TWINE_REPOSITORY_URL="${TWINE_REPOSITORY_URL:-https://upload.pypi.org/legacy/}" |
| 32 | + |
| 33 | +log() { |
| 34 | + printf '\n[%s] %s\n' "$(date +'%H:%M:%S')" "$*" |
| 35 | +} |
| 36 | + |
| 37 | +require_dir() { |
| 38 | + local d="$1" |
| 39 | + if [[ ! -d "${d}" ]]; then |
| 40 | + echo "Required directory does not exist: ${d}" >&2 |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +require_file() { |
| 46 | + local f="$1" |
| 47 | + if [[ ! -f "${f}" ]]; then |
| 48 | + echo "Required file does not exist: ${f}" >&2 |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +run() { |
| 54 | + echo "+ $*" |
| 55 | + "$@" |
| 56 | +} |
| 57 | + |
| 58 | +require_dir "${HELPERS_DIR}" |
| 59 | +require_file "${ROOT_DIR}/SerialUI.spec" |
| 60 | +require_file "${HELPERS_DIR}/setup.py" |
| 61 | + |
| 62 | +log "Installing/upgrading build tools" |
| 63 | +run "${PYTHON_BIN}" -m pip install --upgrade pip build twine pyinstaller pybind11 setuptools wheel |
| 64 | + |
| 65 | +log "Building C-accelerated parsers" |
| 66 | +pushd "${HELPERS_DIR}" >/dev/null |
| 67 | +run "${PYTHON_BIN}" setup.py clean --all || true |
| 68 | +run rm -rf build dist ./*.egg-info .eggs |
| 69 | +run "${PYTHON_BIN}" setup.py build_ext --inplace -v |
| 70 | + |
| 71 | +log "Building wheel and source distribution" |
| 72 | +# Use the current environment (we installed build deps above), |
| 73 | +# so builds don't fail in restricted/offline environments. |
| 74 | +run "${PYTHON_BIN}" -m build --no-isolation |
| 75 | +run "${PYTHON_BIN}" -m twine check dist/* |
| 76 | +run ls -lh dist |
| 77 | + |
| 78 | +WHEEL_FILE="$(ls -1t dist/*.whl | head -n 1)" |
| 79 | +if [[ -z "${WHEEL_FILE}" ]]; then |
| 80 | + echo "No wheel file found in ${HELPERS_DIR}/dist" >&2 |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +log "Reinstalling built wheel locally (${WHEEL_FILE})" |
| 85 | +run "${PYTHON_BIN}" -m pip uninstall -y "${PACKAGE_NAME}" || true |
| 86 | +run "${PYTHON_BIN}" -m pip install --no-deps --force-reinstall --no-cache-dir "${WHEEL_FILE}" |
| 87 | + |
| 88 | +if [[ "${UPLOAD_PYPI}" == "1" ]]; then |
| 89 | + log "Uploading distributions to PyPI" |
| 90 | + export TWINE_USERNAME="${TWINE_USERNAME:-__token__}" |
| 91 | + : "${TWINE_PASSWORD:?Set TWINE_PASSWORD when UPLOAD_PYPI=1}" |
| 92 | + run "${PYTHON_BIN}" -m twine upload --repository-url "${TWINE_REPOSITORY_URL}" dist/* |
| 93 | +else |
| 94 | + log "Skipping PyPI upload (set UPLOAD_PYPI=1 to enable)" |
| 95 | +fi |
| 96 | +popd >/dev/null |
| 97 | + |
| 98 | +log "Building standalone executable with PyInstaller" |
| 99 | +pushd "${ROOT_DIR}" >/dev/null |
| 100 | +# Ignore user-site packages (e.g. obsolete pathlib backport in ~/.local). |
| 101 | +run env PYTHONNOUSERSITE=1 "${PYTHON_BIN}" -m PyInstaller --clean --noconfirm SerialUI.spec |
| 102 | +run ls -lh dist |
| 103 | +popd >/dev/null |
| 104 | + |
| 105 | +log "Done" |
| 106 | +echo "Wheel artifacts: ${HELPERS_DIR}/dist" |
| 107 | +echo "Standalone app: ${ROOT_DIR}/dist/SerialUI" |
0 commit comments