"""Package a release binary built with the vendored-lua feature.""" import argparse import os from pathlib import Path import shutil import struct import subprocess import sys import tarfile import tempfile import tomllib import zipfile ROOT = Path(__file__).resolve().parents[1] PACKAGE = tomllib.loads((ROOT / "Cargo.toml").read_text(encoding="utf-8"))["package"] NAME = PACKAGE["name"] VERSION = PACKAGE["version"] NOTICES = { "LICENSE.txt": ROOT / "LICENSE", "LiberationSans.txt": ROOT / "assets/fonts/LiberationSans License.txt", "VeraMono.txt": ROOT / "assets/fonts/VeraMono License.txt", } def check_binary(binary, platform): with binary.open("rb") as stream: header = stream.read(64) if platform == "linux": # ELF64, little endian, EM_X86_64. if header[:6] == b"\x7fELF\x02\x01" and header[18:20] == b"\x3e\x00": return elif len(header) == 64 and header[:2] == b"MZ": stream.seek(struct.unpack_from("\n" f"Homepage: {PACKAGE['repository']}\n" f"Depends: {', '.join((linked, *extra))}\n" "Recommends: mesa-vulkan-drivers | vulkan-icd\n" f"Description: {PACKAGE['description']}\n" " Plan characters for Path of Exile 1 and 2.\n", encoding="utf-8", ) # The runner's umask must not give package files writable system permissions. for path in [package, *package.rglob("*")]: path.chmod(0o755 if path.is_dir() or path == executable else 0o644) installer = work / f"{NAME}_{VERSION}_amd64.deb" subprocess.run(["dpkg-deb", "--root-owner-group", "--build", package, installer], check=True) archive = work / f"{stem}.tar.gz" with tarfile.open(archive, "w:gz") as bundle: members = {NAME: executable, **{f"licenses/{key}": value for key, value in NOTICES.items()}} for name, source in members.items(): entry = bundle.gettarinfo(str(source), arcname=f"{stem}/{name}") entry.uid = entry.gid = 0 entry.uname = entry.gname = "root" entry.mode = 0o755 if name == NAME else 0o644 with source.open("rb") as stream: bundle.addfile(entry, stream) return archive, installer def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("platform", choices=("windows", "linux")) parser.add_argument("binary", type=Path) parser.add_argument("--output", type=Path, default=ROOT / "target/dist") args = parser.parse_args() binary = args.binary.resolve() output = args.output.resolve() check_binary(binary, args.platform) output.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(dir=output) as temp: package = package_windows if args.platform == "windows" else package_linux for artifact in package(binary, Path(temp)): destination = output / artifact.name artifact.replace(destination) print(destination) if __name__ == "__main__": try: main() except (OSError, ValueError, subprocess.CalledProcessError) as error: sys.exit(str(error))