File size: 3,150 Bytes
7c14868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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