File size: 4,832 Bytes
44a7ed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Workspace root containing train/ and test/ outputs; override via MVEB_ROOT.
ROOT_DIR="${MVEB_ROOT:-$(cd "${SCRIPT_DIR}/../../.." && pwd)}"
GDRIVE_FOLDER_ID="18EunmjOJsbE5Lh9zA0cZ4wKV6Um46dkg"
GDRIVE_URL="https://drive.google.com/drive/folders/${GDRIVE_FOLDER_ID}"
DOWNLOAD_DIR="${ROOT_DIR}/downloads/compcars"
EXTRACT_DIR="${ROOT_DIR}/source/compcars"
PASSWORD="d89551fd190e38"

need_cmd() {
    command -v "$1" >/dev/null 2>&1 || {
        echo "Missing command: $1" >&2
        exit 1
    }
}

need_cmd gdown
need_cmd zip
need_cmd unzip
need_cmd python3

mkdir -p "${DOWNLOAD_DIR}" "${EXTRACT_DIR}"

download_from_gdrive() {
    echo "==> Downloading files from Google Drive folder: ${GDRIVE_URL}"
    echo "    Save to: ${DOWNLOAD_DIR}"
    # --remaining-ok: skip files that already exist (resume-friendly).
    gdown --folder "${GDRIVE_URL}" -O "${DOWNLOAD_DIR}" --remaining-ok
}

check_split_parts() {
    local prefix="$1"
    local zipfile="${DOWNLOAD_DIR}/${prefix}.zip"

    if [[ ! -f "${zipfile}" ]]; then
        echo "[warn] ${prefix}.zip not found under ${DOWNLOAD_DIR}"
        return 1
    fi

    local missing=0
    local part
    for part in "${DOWNLOAD_DIR}/${prefix}".z*; do
        [[ -e "${part}" ]] || continue
        :
    done

    # data.z01 - data.z22
    if [[ "${prefix}" == "data" ]]; then
        local i
        for i in $(seq -w 1 22); do
            if [[ ! -f "${DOWNLOAD_DIR}/${prefix}.z${i}" ]]; then
                echo "[warn] missing ${prefix}.z${i}"
                missing=1
            fi
        done
    fi

    if [[ "${missing}" -eq 1 ]]; then
        echo "[warn] split archive for ${prefix} is incomplete; zip -F may still work if only a few parts are absent."
    fi
    return 0
}

check_truncated_parts() {
    # gdown --remaining-ok skips any existing file without size checks, so a
    # part truncated by a broken run would silently survive re-runs. Split
    # volumes (.zNN) are all cut at the same size, so any part smaller than
    # the largest one is truncated and must be deleted before re-running.
    local prefix="$1"
    local max_size=0 size part
    local bad_parts=()

    for part in "${DOWNLOAD_DIR}/${prefix}".z[0-9]*; do
        [[ -f "${part}" ]] || continue
        size=$(stat -c %s "${part}")
        if (( size > max_size )); then
            max_size=${size}
        fi
    done
    for part in "${DOWNLOAD_DIR}/${prefix}".z[0-9]*; do
        [[ -f "${part}" ]] || continue
        size=$(stat -c %s "${part}")
        if (( size < max_size )); then
            bad_parts+=("${part} (${size} < ${max_size} bytes)")
        fi
    done
    if [[ -f "${DOWNLOAD_DIR}/${prefix}.zip" ]] && [[ ! -s "${DOWNLOAD_DIR}/${prefix}.zip" ]]; then
        bad_parts+=("${DOWNLOAD_DIR}/${prefix}.zip (0 bytes)")
    fi

    if [[ ${#bad_parts[@]} -gt 0 ]]; then
        echo "[error] Truncated download detected (likely an interrupted gdown run):" >&2
        printf '    %s\n' "${bad_parts[@]}" >&2
        echo "Delete the file(s) above and re-run this script to re-download them." >&2
        exit 1
    fi
}

extract_split_archive() {
    local prefix="$1"
    local out_dir="$2"
    local zipfile="${DOWNLOAD_DIR}/${prefix}.zip"
    local combined="${DOWNLOAD_DIR}/combined_${prefix}.zip"
    local marker="${out_dir}/.extracted"

    if [[ -f "${marker}" ]]; then
        echo "==> Skip ${prefix}: already extracted (${marker})"
        return 0
    fi

    if [[ ! -f "${zipfile}" ]]; then
        echo "==> Skip ${prefix}: ${zipfile} not found"
        return 0
    fi

    check_split_parts "${prefix}" || true
    check_truncated_parts "${prefix}"

    echo "==> Combining ${prefix}.zip + ${prefix}.z* -> $(basename "${combined}")"
    (
        cd "${DOWNLOAD_DIR}"
        zip -F "${prefix}.zip" --out "$(basename "${combined}")"
    )

    echo "==> Extracting ${combined} -> ${out_dir}"
    mkdir -p "${out_dir}"
    unzip -o -P "${PASSWORD}" "${combined}" -d "${out_dir}"
    touch "${marker}"
    echo "==> Done: ${out_dir}"
}

main() {
    download_from_gdrive

    extract_split_archive "data" "${EXTRACT_DIR}/data"

    if [[ -f "${DOWNLOAD_DIR}/README.txt" ]]; then
        cp -f "${DOWNLOAD_DIR}/README.txt" "${ROOT_DIR}/README.txt"
        echo "==> Copied README.txt to ${ROOT_DIR}/README.txt"
    fi

    echo "==> Pack CompCars to parquet"
    python3 "${SCRIPT_DIR}/process_compcars.py" --overwrite \
        --image-root "${EXTRACT_DIR}/data/image" \
        --output-root "${ROOT_DIR}"

    echo "==> All done."
    echo "    downloads: ${DOWNLOAD_DIR}"
    echo "    extracted: ${EXTRACT_DIR}"
    echo "    output:    ${MVEB_TRAIN_DIR:-${ROOT_DIR}/MVEB-train}/CompCars , ${MVEB_TEST_DIR:-${ROOT_DIR}/MVEB-test}/CompCars"
}

main "$@"