beffroi-tools/scripts/package-portable.py

70 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Package already-built binaries plus an explicitly supplied target FFprobe."""
import argparse
import hashlib
from pathlib import Path
import tarfile
import tempfile
import shutil
import tomllib
import zipfile
ROOT = Path(__file__).resolve().parents[1]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--target", required=True, choices=("x86_64-pc-windows-gnu", "x86_64-unknown-linux-gnu"))
parser.add_argument("--ffprobe", type=Path, required=True, help="FFprobe built for the target platform")
parser.add_argument("--ffprobe-licenses", type=Path, required=True, help="Directory of notices and source/build provenance for that FFmpeg distribution")
parser.add_argument("--profile", default="release", choices=("debug", "release"))
parser.add_argument("--output", type=Path, default=ROOT / "dist")
args = parser.parse_args()
windows = "windows" in args.target
extension = ".exe" if windows else ""
binaries = [ROOT / "target" / args.target / args.profile / (name + extension)
for name in ("pptx-reporter", "pptx-reporter-gui")]
for path in [*binaries, args.ffprobe]:
if not path.is_file() or not path.stat().st_size or path.is_symlink():
parser.error(f"Missing or unsafe binary: {path}")
with path.open("rb") as file:
magic = file.read(4)
if not (magic.startswith(b"MZ") if windows else magic == b"\x7fELF"):
parser.error(f"Wrong target binary format: {path}")
if not args.ffprobe_licenses.is_dir() or not any(args.ffprobe_licenses.iterdir()):
parser.error("Supply the FFprobe distribution's licence/provenance directory.")
version = tomllib.loads((ROOT / "Cargo.toml").read_text())["package"]["version"]
stem = f"pptx-reporter-portable-{args.target}-v{version}"
args.output.mkdir(parents=True, exist_ok=True)
destination = args.output / (stem + (".zip" if windows else ".tar.gz"))
if destination.exists():
parser.error(f"Output exists: {destination}")
with tempfile.TemporaryDirectory(prefix="reporter-package-") as temporary:
folder = Path(temporary) / stem
folder.mkdir()
for binary in binaries:
shutil.copy2(binary, folder / binary.name)
shutil.copy2(args.ffprobe, folder / ("ffprobe" + extension))
licenses = folder / "LICENSES"
licenses.mkdir()
shutil.copy2(ROOT / "LICENSE", licenses / "reporter-MIT.txt")
shutil.copy2(ROOT / "assets/fonts/LICENSE", licenses / "DejaVu.txt")
shutil.copytree(args.ffprobe_licenses, licenses / "ffmpeg")
shutil.copy2(ROOT / "docs/windows-quickstart.txt", folder / "README.txt")
if windows:
with zipfile.ZipFile(destination, "x", zipfile.ZIP_DEFLATED) as archive:
for path in sorted(folder.rglob("*")):
if path.is_file():
archive.write(path, path.relative_to(folder.parent))
else:
with tarfile.open(destination, "w:gz") as archive:
archive.add(folder, arcname=stem)
with destination.open("rb") as binary:
digest = hashlib.file_digest(binary, "sha256").hexdigest()
destination.with_suffix(destination.suffix + ".sha256").write_text(f"{digest} {destination.name}\n")
print(destination)
if __name__ == "__main__":
main()