Files
coulobre/tools/build_deb.py
T

134 lines
4.8 KiB
Python
Raw Permalink Normal View History

"""Build a Debian package from reviewed Coulobre release artifacts."""
import argparse
import os
import shutil
import subprocess
import tarfile
import tomllib
from pathlib import Path
from tempfile import TemporaryDirectory
from check_core_release import ROOT, git_files, verify_release
def build_package(source, wheel, ref, output):
commit = subprocess.check_output(
[
"git",
"rev-parse",
"--verify",
"--end-of-options",
f"{ref}^{{commit}}",
],
cwd=ROOT,
text=True,
).strip()
verify_release(source, wheel, commit)
tracked = git_files(commit)
project = tomllib.loads(tracked["pyproject.toml"].decode())["project"]
version = project["version"].replace("rc", "~rc")
epoch = subprocess.check_output(
["git", "show", "-s", "--format=%ct", commit], cwd=ROOT, text=True
).strip()
output.mkdir(parents=True, exist_ok=True)
target = output / f"coulobre-panel_{version}_all.deb"
with TemporaryDirectory() as temporary:
root = Path(temporary) / "package"
assets = root / "usr/share/coulobre-panel"
source_dir = assets / "source"
source_dir.mkdir(parents=True)
with tarfile.open(source, "r:gz") as archive:
prefix = source.name.removesuffix(".tar.gz")
for member in archive:
name = member.name.removeprefix(f"{prefix}/")
if member.isfile() and name in tracked:
destination = source_dir / name
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_bytes(archive.extractfile(member).read())
destination.chmod(0o755 if member.mode & 0o111 else 0o644)
(assets / "wheel").mkdir()
packaged_wheel = assets / "wheel" / wheel.name
shutil.copyfile(wheel, packaged_wheel)
packaged_wheel.chmod(0o644)
control = root / "DEBIAN"
control.mkdir()
metadata = control / "control"
metadata.write_text(
f"Package: coulobre-panel\nVersion: {version}\n"
"Architecture: all\nSection: web\nPriority: optional\n"
"Maintainer: Hippogrief <info@hippogrief.nl>\n"
"Homepage: https://code.hosting.hippogrief.nl/hippogrief/coulobre\n"
"Depends: python3 (>= 3.12), python3-venv, ca-certificates, "
"openssh-client\n"
"Description: Coulobre web hosting control panel\n"
" Deployment package for Ubuntu 26.04 LTS (resolute) and "
"Debian 13 (trixie).\n"
" On a fresh host, run sudo coulobre-install --interactive "
"after APT exits.\n"
)
metadata.chmod(0o644)
sbin = root / "usr/sbin"
sbin.mkdir()
for name, directory in (
("postinst", control),
("prerm", control),
("coulobre-install", sbin),
):
path = directory / name
shutil.copyfile(source_dir / "deploy/debian" / name, path)
path.chmod(0o755)
units = root / "usr/lib/systemd/system"
units.mkdir(parents=True)
for name in (
"coulobre-remote-deploy.socket",
"coulobre-remote-deploy@.service",
):
path = units / name
shutil.copyfile(source_dir / "deploy/systemd" / name, path)
path.chmod(0o644)
import_access = (
"coulobre-mailbox-import-worker.service.d/receiver.conf"
)
import_unit = units / import_access
import_unit.parent.mkdir()
shutil.copyfile(
source_dir / "deploy/systemd" / import_access, import_unit
)
import_unit.chmod(0o644)
doc = root / "usr/share/doc/coulobre-panel"
doc.mkdir(parents=True)
shutil.copyfile(source_dir / "LICENSE", doc / "copyright")
(doc / "copyright").chmod(0o644)
root.chmod(0o755)
for path in root.rglob("*"):
if path.is_dir():
path.chmod(0o755)
os.utime(path, (int(epoch), int(epoch)))
subprocess.run(
[
"dpkg-deb",
"--root-owner-group",
"-Zxz",
"--build",
root,
target,
],
env=os.environ | {"SOURCE_DATE_EPOCH": epoch},
check=True,
)
return target
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("source", type=Path)
parser.add_argument("wheel", type=Path)
parser.add_argument("--ref", default="HEAD")
parser.add_argument("--output", type=Path, default=Path("dist"))
args = parser.parse_args()
print(build_package(args.source, args.wheel, args.ref, args.output))
if __name__ == "__main__":
main()