|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to build, copy and install a Python wheel package from a source directory to a destination directory |
| 4 | + |
| 5 | +print_usage() { |
| 6 | + echo "Usage: $0 [OPTIONS]" |
| 7 | + echo "Options:" |
| 8 | + echo " --force-repackage Force rebuilding the wheel package" |
| 9 | + echo " --source-dir=DIR Source directory containing the Python package (default: ../hummingbot)" |
| 10 | + echo " --dest-dir=DIR Destination directory for the wheel file (default: ./wheels)" |
| 11 | + echo " --package-name=NAME Name of the package to build (default: hummingbot)" |
| 12 | + echo " --conda-env=NAME Conda environment to install into (required if not in environment.yml or .env)" |
| 13 | + echo " --build-env=NAME Conda environment to build the package in (default: hummingbot)" |
| 14 | + echo " --help Show this help message" |
| 15 | + echo "" |
| 16 | + echo "When called from Makefile, you can use this format for arguments not in the makefile:" |
| 17 | + echo "make reference-local-hummingbot ARGS=\"--force-repackage --source-dir=DIR ...\"" |
| 18 | +} |
| 19 | + |
| 20 | +# Default values |
| 21 | +FORCE_REPACKAGE=false |
| 22 | +SOURCE_DIR="" |
| 23 | +DEST_DIR="" |
| 24 | +PACKAGE_NAME="hummingbot" |
| 25 | +CONDA_ENV="" |
| 26 | +BUILD_ENV="hummingbot" |
| 27 | + |
| 28 | +# Get current working directory |
| 29 | +CWD="$(pwd)" |
| 30 | + |
| 31 | +# Parse arguments |
| 32 | +while [[ $# -gt 0 ]]; do |
| 33 | + case $1 in |
| 34 | + --force-repackage) |
| 35 | + FORCE_REPACKAGE=true |
| 36 | + shift |
| 37 | + ;; |
| 38 | + --source-dir=*) |
| 39 | + SOURCE_DIR="${1#*=}" |
| 40 | + shift |
| 41 | + ;; |
| 42 | + --dest-dir=*) |
| 43 | + DEST_DIR="${1#*=}" |
| 44 | + shift |
| 45 | + ;; |
| 46 | + --package-name=*) |
| 47 | + PACKAGE_NAME="${1#*=}" |
| 48 | + shift |
| 49 | + ;; |
| 50 | + --conda-env=*) |
| 51 | + CONDA_ENV="${1#*=}" |
| 52 | + shift |
| 53 | + ;; |
| 54 | + --build-env=*) |
| 55 | + BUILD_ENV="${1#*=}" |
| 56 | + shift |
| 57 | + ;; |
| 58 | + --help) |
| 59 | + print_usage |
| 60 | + exit 0 |
| 61 | + ;; |
| 62 | + *) |
| 63 | + echo "Unknown option: $1" |
| 64 | + print_usage |
| 65 | + exit 1 |
| 66 | + ;; |
| 67 | + esac |
| 68 | +done |
| 69 | + |
| 70 | +# Extract conda environment name from environment.yml |
| 71 | +if [ -z "$CONDA_ENV" ] && [ -f "$CWD/environment.yml" ]; then |
| 72 | + CONDA_ENV=$(grep "^name:" "$CWD/environment.yml" | cut -d ":" -f2 | tr -d " ") |
| 73 | +fi |
| 74 | + |
| 75 | +# If not found in environment.yml, try .env file |
| 76 | +if [ -z "$CONDA_ENV" ] && [ -f "$CWD/.env" ]; then |
| 77 | + CONDA_ENV=$(grep "^CONDA_ENV=" "$CWD/.env" | cut -d "=" -f2 | tr -d '"' | tr -d "'" | tr -d " ") |
| 78 | +fi |
| 79 | + |
| 80 | +# If still not found, throw an error |
| 81 | +if [ -z "$CONDA_ENV" ]; then |
| 82 | + echo "Error: Conda environment name not found in environment.yml or .env" |
| 83 | + echo "Please specify with --conda-env option or add CONDA_ENV to .env file or name to environment.yml" |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +# Set default paths if not provided |
| 88 | +if [ -z "$SOURCE_DIR" ]; then |
| 89 | + SOURCE_DIR="../$PACKAGE_NAME" |
| 90 | +fi |
| 91 | + |
| 92 | +if [ -z "$DEST_DIR" ]; then |
| 93 | + DEST_DIR="./wheels" |
| 94 | +fi |
| 95 | + |
| 96 | +# Convert to absolute paths if relative |
| 97 | +if [[ ! "$SOURCE_DIR" = /* ]]; then |
| 98 | + SOURCE_DIR="$CWD/$SOURCE_DIR" |
| 99 | +fi |
| 100 | + |
| 101 | +if [[ ! "$DEST_DIR" = /* ]]; then |
| 102 | + DEST_DIR="$CWD/$DEST_DIR" |
| 103 | +fi |
| 104 | + |
| 105 | +# Create wheels directory if it doesn't exist |
| 106 | +mkdir -p "$DEST_DIR" |
| 107 | + |
| 108 | +# Check for existing wheel in source directory |
| 109 | +cd "$SOURCE_DIR" || { echo "Error: Could not change to source directory: $SOURCE_DIR"; exit 1; } |
| 110 | +EXISTING_WHEEL=$(ls "dist/$PACKAGE_NAME-"*.whl 2>/dev/null) |
| 111 | +if [ -n "$EXISTING_WHEEL" ] && [ "$FORCE_REPACKAGE" = false ]; then |
| 112 | + echo "Found existing wheel: $EXISTING_WHEEL" |
| 113 | + WHEEL_FILE="$EXISTING_WHEEL" |
| 114 | +else |
| 115 | + if [ -n "$EXISTING_WHEEL" ]; then |
| 116 | + echo "Force repackage requested, rebuilding wheel..." |
| 117 | + rm -f "$EXISTING_WHEEL" |
| 118 | + echo "Removed existing wheel: $EXISTING_WHEEL" |
| 119 | + fi |
| 120 | + |
| 121 | + # Build wheel |
| 122 | + echo "Building $PACKAGE_NAME wheel in conda environment '$BUILD_ENV'..." |
| 123 | + conda run -n $BUILD_ENV python setup.py sdist bdist_wheel |
| 124 | + |
| 125 | + # Find the wheel file |
| 126 | + WHEEL_FILE=$(ls "dist/$PACKAGE_NAME-"*.whl 2>/dev/null) |
| 127 | + if [ -z "$WHEEL_FILE" ]; then |
| 128 | + echo "Error: No wheel file found in dist directory" |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | +fi |
| 132 | + |
| 133 | +# Get the wheel filename without path |
| 134 | +WHEEL_FILENAME=$(basename "$WHEEL_FILE") |
| 135 | + |
| 136 | +# Check if there's already a matching wheel file in the destination directory |
| 137 | +LOCAL_WHEEL_FILE="$DEST_DIR/$WHEEL_FILENAME" |
| 138 | +if [ -f "$LOCAL_WHEEL_FILE" ]; then |
| 139 | + # Calculate hashes for both files |
| 140 | + SOURCE_HASH=$(sha256sum "$WHEEL_FILE" | cut -d ' ' -f 1) |
| 141 | + LOCAL_HASH=$(sha256sum "$LOCAL_WHEEL_FILE" | cut -d ' ' -f 1) |
| 142 | + |
| 143 | + # Compare hashes |
| 144 | + if [ "$SOURCE_HASH" = "$LOCAL_HASH" ]; then |
| 145 | + if [ -z "$EXISTING_WHEEL" ] || [ "$FORCE_REPACKAGE" = true ]; then |
| 146 | + echo "Existing local $LOCAL_WHEEL_FILE already matches generated $WHEEL_FILE" |
| 147 | + else |
| 148 | + echo "Existing local $LOCAL_WHEEL_FILE already matches existing $WHEEL_FILE" |
| 149 | + fi |
| 150 | + echo "Skipping installation as files are identical." |
| 151 | + exit 0 |
| 152 | + else |
| 153 | + echo "Local wheel exists but has different hash, will update..." |
| 154 | + fi |
| 155 | +fi |
| 156 | + |
| 157 | +# Copy wheel to destination directory |
| 158 | +echo "Copying $(basename "$WHEEL_FILE") to wheels directory..." |
| 159 | +cp "$WHEEL_FILE" "$DEST_DIR/" |
| 160 | +WHEEL_FILE="$DEST_DIR/$(basename "$WHEEL_FILE")" |
| 161 | + |
| 162 | +# Install new wheel |
| 163 | +echo "Installing $(basename "$WHEEL_FILE") into conda environment '$CONDA_ENV'..." |
| 164 | +conda run -n $CONDA_ENV pip install --force-reinstall "$WHEEL_FILE" |
| 165 | + |
| 166 | +echo -e "\nSuccessfully installed $(basename "$WHEEL_FILE") into conda environment '$CONDA_ENV'" |
0 commit comments