Froink commited on
Commit
99752f7
·
verified ·
1 Parent(s): 6193826

The script used to unzip the datasets zipped by abstract level

Browse files
Files changed (1) hide show
  1. unzip_by_level.sh +153 -0
unzip_by_level.sh ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # extract_haid_level.sh (fixed version)
5
+ # Extract .zip archives named like <shapes>_shapes_mode<mode>.zip across a HAID tree.
6
+ # This variant avoids process-substitution/mapfile to be more portable.
7
+ #
8
+ # Usage:
9
+ # ./extract_haid_level.sh <SRC_ROOT> [--shapes N] [--mode M] [--dest DEST_ROOT] [--dry-run] [--no-overwrite]
10
+ #
11
+ # Examples:
12
+ # ./extract_haid_level.sh /data/HAID
13
+ # ./extract_haid_level.sh /data/HAID --shapes 10
14
+ # ./extract_haid_level.sh /data/HAID --shapes 10 --mode 0 --dest /data/HAID_extracted --dry-run
15
+
16
+ print_usage() {
17
+ cat <<EOF
18
+ Usage: $0 <SRC_ROOT> [--shapes N] [--mode M] [--dest DEST_ROOT] [--dry-run] [--no-overwrite] [-h|--help]
19
+
20
+ Options:
21
+ --shapes N Shape count (e.g., 10). If omitted, matches all X.
22
+ --mode M Mode (e.g., 0). Requires --shapes to be provided.
23
+ --dest DIR Destination root for extraction (default: SRC_ROOT).
24
+ --dry-run Only list archives that would be extracted (no extraction).
25
+ --no-overwrite Do not overwrite existing files when extracting.
26
+ -h, --help Show this help message.
27
+ EOF
28
+ }
29
+
30
+ # ---- parse args ----
31
+ if [[ $# -lt 1 ]]; then
32
+ print_usage
33
+ exit 2
34
+ fi
35
+
36
+ SRC_RAW="$1"; shift
37
+
38
+ SHAPES=""
39
+ MODE=""
40
+ DEST_RAW=""
41
+ DRY_RUN=false
42
+ NO_OVERWRITE=false
43
+
44
+ while [[ $# -gt 0 ]]; do
45
+ case "$1" in
46
+ --shapes) SHAPES="$2"; shift 2 ;;
47
+ --mode) MODE="$2"; shift 2 ;;
48
+ --dest) DEST_RAW="$2"; shift 2 ;;
49
+ --dry-run) DRY_RUN=true; shift ;;
50
+ --no-overwrite) NO_OVERWRITE=true; shift ;;
51
+ -h|--help) print_usage; exit 0 ;;
52
+ *) echo "Unknown argument: $1" >&2; print_usage; exit 2 ;;
53
+ esac
54
+ done
55
+
56
+ if [[ -n "$MODE" && -z "$SHAPES" ]]; then
57
+ echo "Error: --mode requires --shapes to be provided." >&2
58
+ exit 3
59
+ fi
60
+
61
+ if ! command -v unzip >/dev/null 2>&1; then
62
+ echo "Error: 'unzip' not found. Install with: sudo apt install unzip" >&2
63
+ exit 4
64
+ fi
65
+
66
+ SRC="$(realpath "$SRC_RAW")"
67
+ if [[ -z "$DEST_RAW" ]]; then
68
+ DEST="$SRC"
69
+ else
70
+ mkdir -p "$DEST_RAW"
71
+ DEST="$(realpath "$DEST_RAW")"
72
+ fi
73
+
74
+ echo "Source root: $SRC"
75
+ echo "Destination root: $DEST"
76
+
77
+ if [[ -n "$SHAPES" ]]; then
78
+ if [[ -n "$MODE" ]]; then
79
+ echo "Will extract archives matching: ${SHAPES}_shapes_mode${MODE}.zip"
80
+ PATTERN="${SHAPES}_shapes_mode${MODE}.zip"
81
+ else
82
+ echo "Will extract archives matching: ${SHAPES}_shapes_mode*.zip"
83
+ PATTERN="${SHAPES}_shapes_mode*.zip"
84
+ fi
85
+ else
86
+ echo "Will extract ALL '*_shapes_mode*.zip' archives"
87
+ PATTERN="*_shapes_mode*.zip"
88
+ fi
89
+
90
+ echo "Dry run: $DRY_RUN"
91
+ echo "No overwrite existing files: $NO_OVERWRITE"
92
+ echo
93
+
94
+ # ---- find files into a temporary file (null-separated) ----
95
+ TMPFILE="$(mktemp)"
96
+ # ensure tmpfile removed on exit
97
+ trap 'rm -f "$TMPFILE"' EXIT
98
+
99
+ # Use find -print0 to preserve file names with spaces/newlines
100
+ find "$SRC" -type f -name "$PATTERN" -print0 > "$TMPFILE"
101
+
102
+ # Count found
103
+ # awk -v RS='\0' 'END{print NR-1}' won't be portable; use loop to count below while extracting.
104
+ found=0
105
+ extracted=0
106
+ errors=0
107
+ skipped=0
108
+
109
+ # Read entries one-by-one using read -d '' from the tmp file
110
+ while IFS= read -r -d '' zipf; do
111
+ found=$((found+1))
112
+
113
+ # compute relative path and destination dir
114
+ rel="${zipf#$SRC/}" # path like HAID-MiniImageNet/class_000/10_shapes_mode0.zip
115
+ rel_dir="$(dirname "$rel")" # e.g. HAID-MiniImageNet/class_000
116
+ dest_dir="$DEST/$rel_dir" # preserve same relative structure under DEST
117
+
118
+ echo "Archive: $rel"
119
+
120
+ if [[ $DRY_RUN == true ]]; then
121
+ echo " DRYRUN: would extract '$zipf' into '$dest_dir/'"
122
+ continue
123
+ fi
124
+
125
+ mkdir -p "$dest_dir"
126
+
127
+ # unzip flags
128
+ if [[ $NO_OVERWRITE == true ]]; then
129
+ UNZIP_OPTS="-n"
130
+ else
131
+ UNZIP_OPTS="-o"
132
+ fi
133
+
134
+ if unzip -q $UNZIP_OPTS "$zipf" -d "$dest_dir"; then
135
+ echo " EXTRACTED -> $dest_dir"
136
+ extracted=$((extracted+1))
137
+ else
138
+ echo " ERROR extracting: $zipf" >&2
139
+ errors=$((errors+1))
140
+ fi
141
+
142
+ done < "$TMPFILE"
143
+
144
+ echo
145
+ echo "Summary:"
146
+ echo " Matching archives found: $found"
147
+ echo " Successfully extracted: $extracted"
148
+ echo " Errors: $errors"
149
+ if [[ $DRY_RUN == true ]]; then
150
+ echo " (dry-run: no files were actually extracted)"
151
+ fi
152
+
153
+ exit 0