Last active
December 30, 2024 20:20
-
-
Save hiranp/ff076d8dfed7067cb1720ee6dbcacd8e to your computer and use it in GitHub Desktop.
Python Binary Dependency Generator and Installer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # This script builds a tarball of all the dependencies for the project | |
| # It is meant to be run on a Linux machine without internet access | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | |
| PYTHON_BIN="/usr/bin/python3.9" | |
| REQUIREMENTS_FILE="requirements.txt" | |
| DEPENDENCIES_DIR="${SCRIPT_DIR}/dependencies" | |
| DEPENDENCIES_TARBALL="${SCRIPT_DIR}/dependencies.tar.gz" | |
| function updir() { | |
| cd "$(printf '../%.0s' $(seq 1 "$1"))" || exit | |
| } | |
| function cleanup { | |
| echo "Cleaning up dependencies directory and tarball..." | |
| rm -rf "${DEPENDENCIES_DIR}" | |
| rm -f "${DEPENDENCIES_TARBALL}" | |
| } | |
| function package_dependencies { | |
| if [[ ! -f "${REQUIREMENTS_FILE}" ]]; then | |
| echo "Requirements file not found: ${REQUIREMENTS_FILE}" | |
| exit 1 | |
| fi | |
| mkdir -p "${DEPENDENCIES_DIR}" | |
| # Change to the directory of the requirements file | |
| updir "$(echo "${REQUIREMENTS_FILE}" | awk -F/ '{print NF-1}')" | |
| echo "Packaging dependencies from $(pwd)" | |
| "${PYTHON_BIN}" -m pip download -r "${REQUIREMENTS_FILE}" --platform manylinux1_x86_64 --only-binary=:all: -d "${DEPENDENCIES_DIR}" | |
| tar cvfz "${DEPENDENCIES_TARBALL}" "${DEPENDENCIES_DIR}" | |
| } | |
| function install_dependencies { | |
| if [[ ! -d "${DEPENDENCIES_DIR}" ]]; then | |
| echo "Dependencies directory not found: ${DEPENDENCIES_DIR}" | |
| exit 1 | |
| fi | |
| "${PYTHON_BIN}" -m pip install -r "${REQUIREMENTS_FILE}" --no-index --find-links "${DEPENDENCIES_DIR}" --no-deps | |
| } | |
| function print_help { | |
| echo "Usage: $0 [options]" | |
| echo " -h, --help Print this help message" | |
| echo " -p, --package_dependencies Package dependencies" | |
| echo " -r, --requirements FILE Full path to requirements.txt file" | |
| echo " -c, --cleanup Cleanup dependencies" | |
| echo " -i, --install_dependencies Install dependencies" | |
| } | |
| function args { | |
| while [[ $# -gt 0 ]]; do | |
| key="$1" | |
| case $key in | |
| -h|--help) | |
| print_help | |
| exit 0 | |
| ;; | |
| -r|--requirements) | |
| REQUIREMENTS_FILE="$2" | |
| shift | |
| shift | |
| ;; | |
| -p|--package_dependencies) | |
| package_dependencies | |
| exit 0 | |
| ;; | |
| -c|--cleanup) | |
| cleanup | |
| exit 0 | |
| ;; | |
| -i|--install_dependencies) | |
| install_dependencies | |
| exit 0 | |
| ;; | |
| *) | |
| echo "Unknown argument: $key" | |
| print_help | |
| exit 1 | |
| ;; | |
| esac | |
| shift | |
| done | |
| } | |
| args "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment