| #!/usr/bin/env bash |
| set -euo pipefail |
|
|
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
|
|
| ARCHIVE="${1:-$SCRIPT_DIR/LiveCodeBench-venv-portable-20260407.tar.zst}" |
| TARGET="${2:-/data/repos/LiveCodeBench/.venv}" |
| OLD_PATH="${OLD_PATH:-/data/repos/LiveCodeBench/.venv}" |
| VERIFY_SHA="${VERIFY_SHA:-1}" |
|
|
| if [[ ! -f "$ARCHIVE" ]]; then |
| echo "archive not found: $ARCHIVE" >&2 |
| exit 1 |
| fi |
|
|
| if ! command -v tar >/dev/null 2>&1; then |
| echo "tar is required on the target server" >&2 |
| exit 1 |
| fi |
|
|
| if [[ "$ARCHIVE" == *.tar.zst ]] && ! tar --help 2>/dev/null | grep -q -- --zstd; then |
| echo "this tar does not support --zstd; install GNU tar with zstd support" >&2 |
| exit 1 |
| fi |
|
|
| sha_file="${ARCHIVE}.sha256" |
| if [[ "$VERIFY_SHA" != "0" && -f "$sha_file" ]] && command -v sha256sum >/dev/null 2>&1; then |
| ( |
| cd "$(dirname "$sha_file")" |
| sha256sum -c "$(basename "$sha_file")" |
| ) |
| fi |
|
|
| target_parent="$(dirname "$TARGET")" |
| tmp_root="$(mktemp -d)" |
| tmp_extract="$tmp_root/extract" |
|
|
| cleanup() { |
| rm -rf "$tmp_root" |
| } |
| trap cleanup EXIT |
|
|
| mkdir -p "$tmp_extract" "$target_parent" |
| tar --zstd -xpf "$ARCHIVE" -C "$tmp_extract" |
|
|
| if [[ ! -d "$tmp_extract/.venv" ]]; then |
| echo "archive did not contain .venv at its root" >&2 |
| exit 1 |
| fi |
|
|
| rm -rf "$TARGET" |
| mkdir -p "$TARGET" |
| cp -a "$tmp_extract/.venv/." "$TARGET/" |
|
|
| NEW_PATH="$TARGET" |
| BUNDLED_PYTHON_HOME="$TARGET/.python-home" |
|
|
| if [[ -x "$BUNDLED_PYTHON_HOME/bin/python3.11" ]]; then |
| rm -f "$TARGET/bin/python" "$TARGET/bin/python3" "$TARGET/bin/python3.11" |
| ln -s ../.python-home/bin/python3.11 "$TARGET/bin/python" |
| ln -s python "$TARGET/bin/python3" |
| ln -s python "$TARGET/bin/python3.11" |
| fi |
|
|
| NEW_PYTHON="$TARGET/bin/python3" |
| if [[ ! -x "$NEW_PYTHON" ]]; then |
| NEW_PYTHON="$TARGET/bin/python" |
| fi |
|
|
| rewrite_text_file() { |
| local file="$1" |
| OLD_PATH="$OLD_PATH" NEW_PATH="$NEW_PATH" perl -0pi -e 's/\Q$ENV{OLD_PATH}\E/$ENV{NEW_PATH}/g' "$file" |
| } |
|
|
| patch_shebang() { |
| local file="$1" |
| local first_line |
| first_line="$(head -n 1 "$file" || true)" |
| if [[ "$first_line" == "#!"*"$OLD_PATH/bin/python"* ]]; then |
| NEW_PYTHON="$NEW_PYTHON" perl -0pi -e 's@^#!.*$@#!$ENV{NEW_PYTHON}@m' "$file" |
| fi |
| } |
|
|
| while IFS= read -r -d '' file; do |
| if grep -Iq . "$file"; then |
| rewrite_text_file "$file" |
| patch_shebang "$file" |
| fi |
| done < <(find "$TARGET/bin" -maxdepth 1 -type f -print0) |
|
|
| if [[ -f "$TARGET/pyvenv.cfg" ]]; then |
| rewrite_text_file "$TARGET/pyvenv.cfg" |
| if [[ -d "$BUNDLED_PYTHON_HOME/bin" ]]; then |
| BUNDLED_PYTHON_HOME="$BUNDLED_PYTHON_HOME" perl -0pi -e 's/^home = .*$/home = $ENV{BUNDLED_PYTHON_HOME}\/bin/m' "$TARGET/pyvenv.cfg" |
| else |
| NEW_PATH="$NEW_PATH" perl -0pi -e 's/^home = .*$/home = $ENV{NEW_PATH}\/bin/m' "$TARGET/pyvenv.cfg" |
| fi |
| fi |
|
|
| "$NEW_PYTHON" -c 'import sys; print(sys.executable); print(sys.prefix)' |
|
|
| cat <<EOF |
| restore complete |
| archive: $ARCHIVE |
| target: $TARGET |
| python: $NEW_PYTHON |
| |
| activate with: |
| source $TARGET/bin/activate |
| |
| note: |
| native packages still require a compatible target server |
| (same CPU arch, similar glibc/libstdc++, and matching CUDA stack if used) |
| EOF |
|
|