50 lines
1.8 KiB
Bash
Executable File
50 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
set +x
|
|
: "${REPORT_ZIP_PASSWORD:?Set REPORT_ZIP_PASSWORD to encrypt publication archives}"
|
|
build_root=${1:-}; artifact_dir=${2:-}
|
|
[[ -n "$build_root" && -n "$artifact_dir" && $# -eq 2 ]]||{ echo "Usage: $0 BUILD_ROOT ARTIFACT_DIR" >&2; exit 2; }
|
|
command -v uv >/dev/null || { echo "uv is required" >&2; exit 1; }
|
|
mkdir -p "$artifact_dir"
|
|
|
|
# Packaging dependencies belong to this tool and are resolved in uv's cache.
|
|
uv run --isolated --with pyzipper==0.3.6 python - "$build_root" "$artifact_dir" <<'PY'
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pyzipper
|
|
|
|
build_root = Path(sys.argv[1]).resolve()
|
|
artifact_dir = Path(sys.argv[2]).resolve()
|
|
password = os.environ["REPORT_ZIP_PASSWORD"].encode()
|
|
|
|
for category in ("Reports", "Diagnostics"):
|
|
source_dir = build_root / category.lower() / "pdfs"
|
|
if not source_dir.is_dir():
|
|
source_dir = build_root / category.lower()
|
|
|
|
pdfs = sorted(path for path in source_dir.rglob("*.pdf") if path.is_file())
|
|
if not pdfs:
|
|
raise SystemExit(f"No PDFs for {category}")
|
|
for pdf in pdfs:
|
|
if not pdf.read_bytes().startswith(b"%PDF-"):
|
|
raise SystemExit(f"Invalid PDF: {pdf}")
|
|
|
|
archive = artifact_dir / f"{category}.zip"
|
|
with pyzipper.AESZipFile(
|
|
archive, "w", compression=pyzipper.ZIP_DEFLATED, encryption=pyzipper.WZ_AES
|
|
) as output:
|
|
output.setpassword(password)
|
|
output.setencryption(pyzipper.WZ_AES, nbits=256)
|
|
for pdf in pdfs:
|
|
output.write(pdf, pdf.relative_to(source_dir))
|
|
|
|
with pyzipper.AESZipFile(archive) as check:
|
|
check.setpassword(password)
|
|
if check.testzip() is not None:
|
|
raise SystemExit(f"Archive verification failed: {archive}")
|
|
|
|
print(f"Created encrypted report archives in {artifact_dir}")
|
|
PY
|