57 lines
2.3 KiB
Bash
Executable File
57 lines
2.3 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}"
|
|
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
|
packaging_python="${PACKAGING_PYTHON:-$script_dir/../.ci-venv/bin/python}"
|
|
"$packaging_python" -c "import pyzipper"
|
|
build_dir="$(cd -- "${1:-build}" && pwd -P)"
|
|
mkdir -p -- "${2:-.ci-artifacts}"
|
|
output_dir="$(cd -- "${2:-.ci-artifacts}" && pwd -P)"
|
|
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=(-path "$build_dir/diagnostics" -prune -o)
|
|
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; }
|
|
"$packaging_python" - "$staging_dir/$category" "$staging_dir/$category.zip" <<'PY_VERIFY'
|
|
import os, sys
|
|
from pathlib import Path
|
|
import pyzipper
|
|
source = Path(sys.argv[1])
|
|
password = os.environ["REPORT_ZIP_PASSWORD"].encode()
|
|
with pyzipper.AESZipFile(sys.argv[2], "w", compression=pyzipper.ZIP_DEFLATED) as archive:
|
|
archive.setpassword(password)
|
|
archive.setencryption(pyzipper.WZ_AES, nbits=256)
|
|
for pdf in sorted(source.rglob("*.pdf")):
|
|
archive.write(str(pdf), pdf.relative_to(source).as_posix())
|
|
# Authenticate and decrypt every PDF before making the archive available.
|
|
with pyzipper.AESZipFile(sys.argv[2]) as archive:
|
|
entries = archive.infolist()
|
|
assert entries, "Empty archive"
|
|
for entry in entries:
|
|
assert entry.flag_bits & 1 and entry.wz_aes_strength == 3, f"Not AES-256: {entry.filename}"
|
|
assert archive.read(entry, pwd=password).startswith(b"%PDF-")
|
|
PY_VERIFY
|
|
cp "$staging_dir/$category.zip" "$output_dir/$category.zip"
|
|
done
|