dzgui/scripts/release.py
2026-06-03 15:49:19 +09:00

78 lines
2.2 KiB
Python

import build
import os
import subprocess
import tarfile
from pathlib import Path
from concat_licenses import concat_licenses
from prebuild import get_pyapp, rebuild_cpython
root = Path(__file__).resolve().parents[1]
output = root.joinpath("dist")
build_dir = root.joinpath("build")
builder = build.ProjectBuilder(root)
wheel = builder.build("wheel", output_directory=output)
stem = Path(wheel).stem
tarname = f"{stem}.tar.gz"
metadata = stem.split("-")
appname = metadata[0]
version = metadata[1]
pyapp_dir = build_dir.joinpath("pyapp-latest")
if pyapp_dir.is_dir() is False:
get_pyapp()
# TODO: get resulting filename from this function
packaged_version = rebuild_cpython()
assert packaged_version == version
cpython = build_dir.joinpath("airgapped.tar.gz")
# TODO: parse pyproject directly
entrypoint = "dzgui.main:main"
env = os.environ
env["PYAPP_PROJECT_VERSION"] = version
env["PYAPP_PROJECT_NAME"] = appname
env["PYAPP_EXEC_SPEC"] = entrypoint
env["PYAPP_PROJECT_PATH"] = wheel
env["PYAPP_PASS_LOCATION"] = "true"
# NOTE: explicitly install all dependencies into distribution
env["PYAPP_SKIP_INSTALL"] = "true"
env["PYAPP_DISTRIBUTION_EMBED"] = "true"
env["PYAPP_FULL_ISOLATION"] = "true"
env["PYAPP_DISTRIBUTION_PATH"] = str(cpython)
env["PYAPP_DISTRIBUTION_PYTHON_PATH"] = "python/bin/python3"
subfolder = output.joinpath(stem)
subfolder.mkdir(parents=True)
combined_licenses = concat_licenses()
license_file = subfolder.joinpath("LICENSE")
license_file.write_text(combined_licenses)
proc = subprocess.run(["cargo", "build", "--release"], env=env, cwd=pyapp_dir)
if proc.returncode == 0:
output_exe = build_dir.joinpath("pyapp-latest/target/release/pyapp")
release_exe = subfolder.joinpath(appname)
output_exe.rename(release_exe)
tarpath = output.joinpath(tarname)
with tarfile.open(tarpath, "w:gz") as tar:
info = tar.gettarinfo(subfolder)
info.uname = appname
info.name = appname
with open(subfolder, "rb") as f:
tar.addfile(info, f)
print(f"Wrote tarfile to '{tarpath}'")
proc = subprocess.run([release_exe, "-v"], capture_output=True, text=True)
assert proc.stdout.rstrip() == version
release_exe.unlink()
Path(wheel).unlink()