101 lines
5.2 KiB
Python
101 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Package already-built binaries with supplied or release-time downloaded FFprobe."""
|
|
import argparse
|
|
import hashlib
|
|
from pathlib import Path
|
|
import tarfile
|
|
import tempfile
|
|
import shutil
|
|
import tomllib
|
|
import zipfile
|
|
import sys
|
|
import subprocess
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
from fetch_ffprobe import fetch
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--target", required=True, choices=("x86_64-pc-windows-gnu", "x86_64-unknown-linux-gnu"))
|
|
source = parser.add_mutually_exclusive_group(required=True)
|
|
source.add_argument("--ffprobe", type=Path, help="FFprobe built for the target platform")
|
|
source.add_argument("--fetch-ffprobe", action="store_true", help="Download and verify the target FFprobe at packaging time")
|
|
parser.add_argument("--ffprobe-release", default="latest", help="BtbN release tag for --fetch-ffprobe")
|
|
parser.add_argument("--ffprobe-sha256", help="Optional archive digest pin for --fetch-ffprobe")
|
|
parser.add_argument("--ffprobe-licenses", type=Path, help="Directory of notices and source/build provenance for a supplied FFmpeg distribution")
|
|
parser.add_argument("--profile", default="release", choices=("debug", "release"))
|
|
parser.add_argument("--output", type=Path, default=ROOT / "dist")
|
|
args = parser.parse_args()
|
|
if args.fetch_ffprobe and args.ffprobe_licenses:
|
|
parser.error("--ffprobe-licenses is only used with --ffprobe")
|
|
if args.ffprobe and not args.ffprobe_licenses:
|
|
parser.error("--ffprobe requires --ffprobe-licenses")
|
|
if not args.fetch_ffprobe and (args.ffprobe_release != "latest" or args.ffprobe_sha256):
|
|
parser.error("FFprobe release/digest options require --fetch-ffprobe")
|
|
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:
|
|
if not path.is_file():
|
|
parser.error(f"Missing reporter binary: {path}; build the target first")
|
|
if args.fetch_ffprobe:
|
|
try:
|
|
args.ffprobe, args.ffprobe_licenses = fetch(
|
|
args.target, ROOT / "downloads/ffprobe", args.ffprobe_release, args.ffprobe_sha256)
|
|
except (OSError, ValueError, KeyError, subprocess.SubprocessError,
|
|
zipfile.BadZipFile, tarfile.TarError) as error:
|
|
parser.exit(1, f"FFprobe fetch failed: {error}\n")
|
|
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())["workspace"]["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 / "crates/pptx-reporter/assets/fonts/LICENSE", licenses / "DejaVu.txt")
|
|
shutil.copytree(args.ffprobe_licenses, licenses / "ffmpeg")
|
|
shutil.copy2(ROOT / "docs/pptx-reporter/windows-quickstart.txt", folder / "README.txt")
|
|
icons = folder / "icons/pptx-reporter"
|
|
icons.mkdir(parents=True)
|
|
for extension in ("png", "ico", "icns"):
|
|
shutil.copy2(ROOT / f"crates/pptx-reporter/assets/app-icon.{extension}", icons)
|
|
(folder / "scripts").mkdir()
|
|
for script in ("create-desktop-launcher.py", "desktop_icons.py"):
|
|
shutil.copy2(ROOT / "scripts" / script, folder / "scripts" / script)
|
|
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()
|