yifan_datasets / unpack_restore.sh
yifanduan's picture
Upload folder using huggingface_hub
6f3c9d3 verified
Raw
History Blame Contribute Delete
3.1 kB
#!/usr/bin/env bash
set -euo pipefail
# Restore dataset from split archives created by pack_upload.sh.
# Supports both .tar.gz.part-* and .tar.zst.part-* archives.
ARCHIVE_DIR="${ARCHIVE_DIR:-.}"
RESTORE_BASE="${RESTORE_BASE:-./data_restored}"
MAX_JOBS="${MAX_JOBS:-1}"
ONLY_GROUP="${ONLY_GROUP:-}" # optional: e.g. packed__frames or packed__jsonl
if ! [[ "$MAX_JOBS" =~ ^[1-9][0-9]*$ ]]; then
echo "MAX_JOBS must be a positive integer, got: $MAX_JOBS" >&2
exit 1
fi
mkdir -p "$RESTORE_BASE"
wait_for_slot() {
while true; do
local running
running=$(jobs -rp | wc -l)
if [ "$running" -lt "$MAX_JOBS" ]; then
break
fi
wait -n
done
}
restore_one_prefix() {
local prefix="$1"
# prefix format: packed__frames__seed_101 (without .tar.*.part-*)
local group seed rel_dir out_dir
group="${prefix%%__seed_*}"
seed="${prefix##*__}"
if [ -n "$ONLY_GROUP" ] && [ "$group" != "$ONLY_GROUP" ]; then
echo "[SKIP] $prefix (group mismatch: $group)"
return 0
fi
# Map packed__vision_cache__frames -> vision_cache/frames, etc.
rel_dir="${group#packed__}"
rel_dir="${rel_dir//__//}"
out_dir="$RESTORE_BASE/$rel_dir"
mkdir -p "$out_dir"
local zst_parts=()
local gz_parts=()
# Use find instead of globstar to avoid shell-option dependent misses.
while IFS= read -r p; do
zst_parts+=("$p")
done < <(find "$ARCHIVE_DIR" -type f -name "$prefix.tar.zst.part-*" | sort)
while IFS= read -r p; do
gz_parts+=("$p")
done < <(find "$ARCHIVE_DIR" -type f -name "$prefix.tar.gz.part-*" | sort)
if [ "${#zst_parts[@]}" -gt 0 ]; then
if ! command -v zstd >/dev/null 2>&1; then
echo "[ERROR] zstd archives found for $prefix but 'zstd' is not installed." >&2
return 1
fi
echo "[UNPACK] $prefix (.tar.zst) -> $out_dir/$seed"
cat "${zst_parts[@]}" | tar --use-compress-program=unzstd -xf - -C "$out_dir"
return 0
fi
if [ "${#gz_parts[@]}" -gt 0 ]; then
echo "[UNPACK] $prefix (.tar.gz) -> $out_dir/$seed"
cat "${gz_parts[@]}" | tar -xzf - -C "$out_dir"
return 0
fi
echo "[WARN] no archive parts found for $prefix"
return 0
}
# Discover restore units from archive parts (works for downloaded HF datasets).
prefixes=()
declare -A seen_prefixes
while IFS= read -r part; do
[ -f "$part" ] || continue
base="$(basename "$part")"
case "$base" in
*.tar.zst.part-*) prefix="${base%.tar.zst.part-*}" ;;
*.tar.gz.part-*) prefix="${base%.tar.gz.part-*}" ;;
*) continue ;;
esac
if [ -z "${seen_prefixes[$prefix]:-}" ]; then
seen_prefixes[$prefix]=1
prefixes+=("$prefix")
fi
done < <(find "$ARCHIVE_DIR" -type f \( -name '*.tar.zst.part-*' -o -name '*.tar.gz.part-*' \) | sort)
if [ "${#prefixes[@]}" -eq 0 ]; then
echo "No archive parts found under $ARCHIVE_DIR. Nothing to restore." >&2
exit 1
fi
echo "[INFO] Restore base: $RESTORE_BASE"
echo "[INFO] Archive dir : $ARCHIVE_DIR"
echo "[INFO] Units : ${#prefixes[@]}"
for prefix in "${prefixes[@]}"; do
wait_for_slot
restore_one_prefix "$prefix" &
done
wait
echo "[DONE] Restore completed."