51 lines
1.9 KiB
Bash
Executable File
51 lines
1.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Create fresh encrypted ZIPs containing only validated PDFs.
|
|
set +x
|
|
set -euo pipefail
|
|
: "${REPORT_ZIP_PASSWORD:?Set REPORT_ZIP_PASSWORD before packaging}"
|
|
command -v zip >/dev/null
|
|
command -v python3 >/dev/null
|
|
build_dir="$(realpath "${1:-build}")"
|
|
output_dir="$(realpath -m "${2:-.ci-artifacts}")"
|
|
mkdir -p "$output_dir"
|
|
staging_dir="$(mktemp -d)"
|
|
trap 'rm -rf -- "$staging_dir"' EXIT
|
|
|
|
for category in Reports Diagnostics; do
|
|
mkdir -p "$staging_dir/$category"
|
|
if [[ "$category" == Reports ]]; then
|
|
source_dir="$build_dir"
|
|
depth=(-maxdepth 1)
|
|
else
|
|
source_dir="$build_dir/diagnostics"
|
|
depth=()
|
|
fi
|
|
count=0
|
|
while IFS= read -r -d '' pdf; do
|
|
[[ -s "$pdf" && "$(head -c 5 "$pdf")" == '%PDF-' ]] || {
|
|
echo "Invalid or empty PDF: $pdf" >&2
|
|
exit 1
|
|
}
|
|
relative="${pdf#"$source_dir/"}"
|
|
mkdir -p "$staging_dir/$category/$(dirname "$relative")"
|
|
cp "$pdf" "$staging_dir/$category/$relative"
|
|
count=$((count + 1))
|
|
done < <(find "$source_dir" "${depth[@]}" -type f -name '*.pdf' -print0)
|
|
((count > 0)) || { echo "No PDFs for $category" >&2; exit 1; }
|
|
(
|
|
cd "$staging_dir/$category"
|
|
zip -q -r -P "$REPORT_ZIP_PASSWORD" "$staging_dir/$category.zip" .
|
|
)
|
|
# Check encryption and decrypt every entry to verify its CRC before publishing.
|
|
python3 - "$staging_dir/$category.zip" <<'PY_VERIFY'
|
|
import os, sys, zipfile
|
|
with zipfile.ZipFile(sys.argv[1]) as archive:
|
|
entries = [entry for entry in archive.infolist() if not entry.is_dir()]
|
|
assert entries, "Empty archive"
|
|
for entry in entries:
|
|
assert entry.flag_bits & 1, f"Unencrypted entry: {entry.filename}"
|
|
assert archive.read(entry, pwd=os.environ["REPORT_ZIP_PASSWORD"].encode()).startswith(b"%PDF-")
|
|
PY_VERIFY
|
|
cp "$staging_dir/$category.zip" "$output_dir/$category.zip"
|
|
done
|