prepare_wheel.sh 8.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash -e
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A script to download a Python wheel, patch it, copy additional files,
# repackage it, and optionally install the new wheel.


###############################################################################
#  CONFIGURATION & DEFAULTS
###############################################################################
DEFAULT_WHEEL_URL="https://files.pythonhosted.org/packages/4a/4c/ee65ba33467a4c0de350ce29fbae39b9d0e7fcd887cc756fa993654d1228/vllm-0.6.3.post1-cp38-abi3-manylinux1_x86_64.whl"
DEFAULT_PATCH_FILE="vllm_patch_063post1.patch"
DEFAULT_DATA_PLANE_DIR="data_plane"
DEFAULT_WHEEL_DIR="wheel"
DEFAULT_OUTPUT_WHEEL="vllm-dist-0.6.3.post1-cp38-abi3-manylinux1_x86_64.whl"
# Optionally set a default SHA256 checksum for the downloaded wheel
# DEFAULT_CHECKSUM="SOME_SHA256_HERE"

###############################################################################
#  HELPER FUNCTIONS
###############################################################################
usage() {
  cat << EOF
Usage: $0 [OPTIONS]

Options:
  -u, --url <URL>              Wheel URL to download (default: $DEFAULT_WHEEL_URL)
  -p, --patch <FILE>           Patch file path (default: $DEFAULT_PATCH_FILE)
  -d, --data-plane <DIR>       Directory with additional data-plane files (default: $DEFAULT_DATA_PLANE_DIR)
  -w, --wheel-dir <DIR>        Extract destination directory (default: $DEFAULT_WHEEL_DIR)
  -o, --output-wheel <FILE>    Name/path for the repackaged wheel (default: $DEFAULT_OUTPUT_WHEEL)
  -f, --force                  Force overwriting existing directories/files without prompts
  -i, --install                Install the new wheel after repackaging
  -D, --debug                  Enable debug mode (verbose output)
  -h, --help                   Show this help and exit

Example:
  $0 \\
    --url "https://example.com/path/to/vllm.whl" \\
    --patch my_patch.patch \\
    --data-plane custom_data/ \\
    --wheel-dir extracted_wheel \\
    --output-wheel my_vllm_dist.whl \\
    --install \\
    --force \\
    --debug

EOF
}

info_log() {
  # Always prints, showing a timestamp.
  echo "[$(date +'%Y-%m-%d %H:%M:%S')] INFO: $*"
}

debug_log() {
  # Prints only if DEBUG=true
  if [[ "$DEBUG" == true ]]; then
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] DEBUG: $*"
  fi
}

error_exit() {
  echo "ERROR: $*" >&2
  exit 1
}

###############################################################################
#  PARSE ARGUMENTS
###############################################################################
FORCE_OVERWRITE=false
INSTALL_WHEEL=false
DEBUG=false

WHEEL_URL="$DEFAULT_WHEEL_URL"
PATCH_FILE="$DEFAULT_PATCH_FILE"
DATA_PLANE_DIR="$DEFAULT_DATA_PLANE_DIR"
WHEEL_DIR="$DEFAULT_WHEEL_DIR"
OUTPUT_WHEEL="$DEFAULT_OUTPUT_WHEEL"

while [[ $# -gt 0 ]]; do
  case "$1" in
    -u|--url)
      WHEEL_URL="$2"
      shift 2
      ;;
    -p|--patch)
      PATCH_FILE="$2"
      shift 2
      ;;
    -d|--data-plane)
      DATA_PLANE_DIR="$2"
      shift 2
      ;;
    -w|--wheel-dir)
      WHEEL_DIR="$2"
      shift 2
      ;;
    -o|--output-wheel)
      OUTPUT_WHEEL="$2"
      shift 2
      ;;
    -f|--force)
      FORCE_OVERWRITE=true
      shift
      ;;
    -i|--install)
      INSTALL_WHEEL=true
      shift
      ;;
    -D|--debug)
      DEBUG=true
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      error_exit "Unknown option: $1"
      ;;
  esac
done

###############################################################################
#  MAIN SCRIPT
###############################################################################
# Enable debug mode if requested
if [[ "$DEBUG" == true ]]; then
  set -x
fi

info_log "Starting wheel patching script..."


# Function to check if a command exists
command_exists() {
    command -v "$1" >/dev/null 2>&1 || { echo >&2 "I require $1 but it's not installed. Aborting."; exit 1; }
}

# Check for required commands
command_exists pip
command_exists unzip
command_exists zip
command_exists patch

# ---------------------------------------------------------------------------
# 1. Check for existing wheel file or directory
# ---------------------------------------------------------------------------
WHEEL_FILENAME=$(basename "$WHEEL_URL")

if [[ -f "$WHEEL_FILENAME" && "$FORCE_OVERWRITE" != true ]]; then
  info_log "File '$WHEEL_FILENAME' already exists. Reusing existing file."
  info_log "If you want to redownload, remove '$WHEEL_FILENAME' or use --force."
else
  info_log "Downloading wheel from $WHEEL_URL..."
  rm -f "$WHEEL_FILENAME" 2>/dev/null || true  # Remove existing file if forcing
  wget -O "$WHEEL_FILENAME" "$WHEEL_URL"
fi

# ---------------------------------------------------------------------------
# 2. Optional: Verify checksum (commented out by default)
# ---------------------------------------------------------------------------
# if [[ -n "$DEFAULT_CHECKSUM" ]]; then
#   info_log "Verifying SHA256 checksum..."
#   echo "${DEFAULT_CHECKSUM}  ${WHEEL_FILENAME}" | sha256sum --check - || error_exit "Checksum mismatch!"
# fi

# ---------------------------------------------------------------------------
# 3. Create/clean wheel extraction directory
# ---------------------------------------------------------------------------
if [[ -d "$WHEEL_DIR" ]]; then
  if [[ "$FORCE_OVERWRITE" == true ]]; then
    info_log "Removing existing directory '$WHEEL_DIR' due to --force..."
    rm -rf "$WHEEL_DIR"
  else
    error_exit "Directory '$WHEEL_DIR' already exists. Use --force to overwrite."
  fi
fi

info_log "Creating directory '$WHEEL_DIR'..."
mkdir -p "$WHEEL_DIR"

# ---------------------------------------------------------------------------
# 4. Unzip the wheel into the specified directory
# ---------------------------------------------------------------------------
info_log "Unzipping wheel into directory '$WHEEL_DIR'..."
unzip -q "$WHEEL_FILENAME" -d "$WHEEL_DIR"

# ---------------------------------------------------------------------------
# 5. Check/Apply patch
# ---------------------------------------------------------------------------
if [[ ! -f "$PATCH_FILE" ]]; then
  error_exit "Patch file '$PATCH_FILE' not found."
fi

PATCH_TARGET_DIR="$WHEEL_DIR/vllm"
if [[ ! -d "$PATCH_TARGET_DIR" ]]; then
  error_exit "Could not find directory '$PATCH_TARGET_DIR' in unzipped wheel."
fi

info_log "Applying patch '$PATCH_FILE' to '$PATCH_TARGET_DIR'..."
debug_log "Executing: (cd \"$PATCH_TARGET_DIR\" && patch -p1 < \"../../$PATCH_FILE\")"
(
  cd "$PATCH_TARGET_DIR"
  patch -p1 < "../../$PATCH_FILE"
)

# ---------------------------------------------------------------------------
# 6. Copy data plane files
# ---------------------------------------------------------------------------
if [[ ! -d "$DATA_PLANE_DIR" ]]; then
  error_exit "Data plane directory '$DATA_PLANE_DIR' not found."
fi

info_log "Copying files from '$DATA_PLANE_DIR' to '$PATCH_TARGET_DIR/distributed'..."
mkdir -p "$PATCH_TARGET_DIR/distributed"
cp -r "$DATA_PLANE_DIR/"* "$PATCH_TARGET_DIR/distributed/"

# ---------------------------------------------------------------------------
# 7. Re-package into a new wheel
# ---------------------------------------------------------------------------
if [[ -f "$OUTPUT_WHEEL" && "$FORCE_OVERWRITE" != true ]]; then
  error_exit "Output wheel '$OUTPUT_WHEEL' already exists. Use --force to overwrite."
fi

info_log "Creating new wheel file '$OUTPUT_WHEEL'..."
debug_log "Executing: (cd \"$WHEEL_DIR\" && zip -rq \"../$OUTPUT_WHEEL\" .)"
(
  cd "$WHEEL_DIR"
  zip -rq "../$OUTPUT_WHEEL" .
)

# ---------------------------------------------------------------------------
# 8. Optional: Install the new wheel
# ---------------------------------------------------------------------------
if [[ "$INSTALL_WHEEL" == true ]]; then
  # Check if pip is installed
  if ! command -v pip >/dev/null 2>&1; then
    error_exit "pip is not installed or not found in PATH."
  fi

  info_log "Installing newly created wheel '$OUTPUT_WHEEL'..."
  pip install --force-reinstall --upgrade --break-system-packages "$OUTPUT_WHEEL"
fi

info_log "Patch and repackage completed successfully!"
info_log "New wheel: $OUTPUT_WHEEL"
if [[ "$INSTALL_WHEEL" == true ]]; then
  info_log "Wheel has been installed."
fi