#!/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 "$@"