forked from JackMcKew/pyinstaller-action-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.sh
More file actions
291 lines (242 loc) · 7.12 KB
/
archive.sh
File metadata and controls
291 lines (242 loc) · 7.12 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env bash
set -euo pipefail
ZIP_METHODS=" deflate store "
SEVENZIP_METHOD="LZMA2"
SEVENZIP_DICTIONARY="4096m"
SEVENZIP_WORD_SIZE="273"
SEVENZIP_SOLID_BLOCK="16g"
declare -A FILES=()
fail() {
echo "$*" >&2
exit 1
}
find_7z() {
local command
for command in 7z 7zz 7za; do
if command -v "$command" >/dev/null 2>&1; then
command -v "$command"
return 0
fi
done
fail "7-Zip executable not found. Rebuild the base image with the CachyOS/Arch 7zip package."
}
resolve_inside_root() {
local path="$1"
local label="$2"
local resolved
resolved="$(realpath -m "$path")"
case "$resolved" in
"$REPO_ROOT" | "$REPO_ROOT"/*)
printf '%s\n' "$resolved"
;;
*)
fail "$label must stay inside the repository root: $resolved"
;;
esac
}
is_excluded() {
local source="$1"
local exclude
for exclude in "${EXCLUDES[@]}"; do
if [ "$source" = "$exclude" ]; then
return 0
fi
done
return 1
}
add_file() {
local arcname="$1"
local source="$2"
local existing="${FILES[$arcname]:-}"
if [ -n "$existing" ] && [ "$existing" != "$source" ]; then
fail "zip_paths maps multiple files to '$arcname': $existing and $source"
fi
FILES["$arcname"]="$source"
}
add_path() {
local source="$1"
local resolved
local child
local arcname
resolved="$(resolve_inside_root "$source" "zip_paths entry")"
if is_excluded "$resolved"; then
return 0
fi
if [ -f "$resolved" ]; then
add_file "$(basename "$resolved")" "$resolved"
return 0
fi
if [ -d "$resolved" ]; then
while IFS= read -r -d '' child; do
child="$(realpath -m "$child")"
if is_excluded "$child"; then
continue
fi
arcname="${child#"$resolved"/}"
add_file "$arcname" "$child"
done < <(find "$resolved" -type f -print0)
return 0
fi
fail "zip_paths entry is not a file or directory: $resolved"
}
load_excludes() {
local raw_excludes="$1"
local line
EXCLUDES=()
while IFS= read -r line; do
[ -n "$line" ] || continue
case "$line" in
/*)
;;
*)
EXCLUDES+=("$(resolve_inside_root "$REPO_ROOT/$line" "archive output")")
;;
esac
done <<< "$raw_excludes"
}
collect_files() {
local raw_paths="$1"
local raw_excludes="$2"
local entry
local matches
local match
local missing=()
REPO_ROOT="$(pwd -P)"
load_excludes "$raw_excludes"
FILES=()
if ! grep -q '[^[:space:]]' <<< "$raw_paths"; then
fail "zip_paths is required when creating an archive"
fi
shopt -s globstar nullglob dotglob
while IFS= read -r entry; do
entry="${entry#"${entry%%[![:space:]]*}"}"
entry="${entry%"${entry##*[![:space:]]}"}"
[ -n "$entry" ] || continue
mapfile -t matches < <(compgen -G "$entry" || true)
if [ "${#matches[@]}" -eq 0 ]; then
if [ -e "$entry" ]; then
matches=("$entry")
else
missing+=("$entry")
continue
fi
fi
for match in "${matches[@]}"; do
add_path "$match"
done
done <<< "$raw_paths"
shopt -u globstar nullglob dotglob
if [ "${#missing[@]}" -gt 0 ]; then
fail "zip_paths entries did not match anything: ${missing[*]}"
fi
if [ "${#FILES[@]}" -eq 0 ]; then
fail "zip_paths did not produce any files to archive"
fi
}
stage_files() {
local stage_root="$1"
local list_file="$2"
local arcname
local source
local target
: > "$list_file"
for arcname in "${!FILES[@]}"; do
source="${FILES[$arcname]}"
target="$stage_root/$arcname"
mkdir -p "$(dirname "$target")"
if ! ln "$source" "$target" 2>/dev/null; then
cp -p "$source" "$target"
fi
done
printf '%s\n' "${!FILES[@]}" | sort > "$list_file"
}
resolve_archive_output() {
local archive_name="$1"
local label="$2"
case "$archive_name" in
/*)
fail "$label must be relative to the repository root"
;;
esac
ARCHIVE_PATH="$(resolve_inside_root "$REPO_ROOT/$archive_name" "$label")"
mkdir -p "$(dirname "$ARCHIVE_PATH")"
}
create_zip() {
local archive_name="$1"
local raw_paths="$2"
local method_name="${3,,}"
local level_text="$4"
local raw_excludes="$5"
local sevenzip
local temp_dir
local list_file
local method_arg
if [[ "$ZIP_METHODS" != *" $method_name "* ]]; then
fail "Unsupported zip_method '$3'. Supported values: deflate, store. Use sevenzip_name for stronger compression."
fi
if ! [[ "$level_text" =~ ^[0-9]+$ ]] || [ "$level_text" -lt 0 ] || [ "$level_text" -gt 9 ]; then
fail "zip_level must be between 0 and 9, got $level_text"
fi
collect_files "$raw_paths" "$raw_excludes"
resolve_archive_output "$archive_name" "zip_name"
rm -f "$ARCHIVE_PATH"
sevenzip="$(find_7z)"
temp_dir="$(mktemp -d -t cythinst-zip-XXXXXX)"
list_file="$temp_dir/.cythinst-archive-list.txt"
stage_files "$temp_dir" "$list_file"
if [ "$method_name" = "store" ]; then
method_arg="-mm=Copy"
else
method_arg="-mm=Deflate"
fi
(
cd "$temp_dir"
"$sevenzip" a -tzip "-mx=$level_text" "$method_arg" -bd "$ARCHIVE_PATH" "@$list_file" >/dev/null
)
rm -rf "$temp_dir"
echo "Created ${ARCHIVE_PATH#"$REPO_ROOT"/} with ${#FILES[@]} files using zip/$method_name"
}
create_7z() {
local archive_name="$1"
local raw_paths="$2"
local raw_excludes="$3"
local sevenzip
local temp_dir
local list_file
collect_files "$raw_paths" "$raw_excludes"
resolve_archive_output "$archive_name" "sevenzip_name"
rm -f "$ARCHIVE_PATH"
sevenzip="$(find_7z)"
temp_dir="$(mktemp -d -t cythinst-7z-XXXXXX)"
list_file="$temp_dir/.cythinst-archive-list.txt"
stage_files "$temp_dir" "$list_file"
(
cd "$temp_dir"
"$sevenzip" a \
-t7z \
-mx=9 \
"-m0=${SEVENZIP_METHOD}" \
"-md=${SEVENZIP_DICTIONARY}" \
"-mfb=${SEVENZIP_WORD_SIZE}" \
"-ms=${SEVENZIP_SOLID_BLOCK}" \
-mmt=on \
-bd \
"$ARCHIVE_PATH" \
"@$list_file" >/dev/null
)
rm -rf "$temp_dir"
echo "Created ${ARCHIVE_PATH#"$REPO_ROOT"/} with ${#FILES[@]} files using 7z/${SEVENZIP_METHOD} ultra, dictionary ${SEVENZIP_DICTIONARY}, word ${SEVENZIP_WORD_SIZE}, solid block ${SEVENZIP_SOLID_BLOCK}"
}
case "${1:-}" in
zip)
[ "$#" -eq 6 ] || fail "usage: archive.sh zip <zip_name> <zip_paths> <zip_method> <zip_level> <exclude_paths>"
create_zip "$2" "$3" "$4" "$5" "$6"
;;
7z | sevenzip)
[ "$#" -eq 4 ] || fail "usage: archive.sh 7z <sevenzip_name> <zip_paths> <exclude_paths>"
create_7z "$2" "$3" "$4"
;;
*)
fail "usage: archive.sh <zip|7z> ..."
;;
esac