267 lines
10 KiB
Python
Executable File
267 lines
10 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Linux System Updater Package Builder.
|
|
|
|
Generates:
|
|
1. .tar.gz standalone portable distribution with installer
|
|
2. .deb package for Debian/Ubuntu (pure Python ar/tar packaging or dpkg-deb)
|
|
3. .rpm build tree and package for RHEL/Fedora/CentOS/Rocky
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import tarfile
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
from typing import Dict, List, Optional
|
|
|
|
|
|
def write_ar_header(name: str, size: int, mtime: int = 0) -> bytes:
|
|
"""Creates a standard 60-byte Unix 'ar' file header."""
|
|
file_id = f"{name:<16}".encode("ascii")[:16]
|
|
file_mtime = f"{mtime:<12}".encode("ascii")[:12]
|
|
owner_id = b"0 "[:6]
|
|
group_id = b"0 "[:6]
|
|
file_mode = b"100644 "[:8]
|
|
file_size = f"{size:<10}".encode("ascii")[:10]
|
|
trailer = b"`\n"
|
|
return file_id + file_mtime + owner_id + group_id + file_mode + file_size + trailer
|
|
|
|
|
|
def create_native_deb(control_tar_gz: Path, data_tar_gz: Path, output_deb_path: Path) -> Path:
|
|
"""Builds a binary-compatible Debian .deb package using pure Python standard library."""
|
|
debian_binary = b"2.0\n"
|
|
now = int(time.time())
|
|
|
|
with open(str(output_deb_path), "wb") as deb:
|
|
# 1. Global ar magic identifier
|
|
deb.write(b"!<arch>\n")
|
|
|
|
# 2. debian-binary member
|
|
deb.write(write_ar_header("debian-binary", len(debian_binary), now))
|
|
deb.write(debian_binary)
|
|
if len(debian_binary) % 2 != 0:
|
|
deb.write(b"\n")
|
|
|
|
# 3. control.tar.gz member
|
|
ctrl_bytes = control_tar_gz.read_bytes()
|
|
deb.write(write_ar_header("control.tar.gz", len(ctrl_bytes), now))
|
|
deb.write(ctrl_bytes)
|
|
if len(ctrl_bytes) % 2 != 0:
|
|
deb.write(b"\n")
|
|
|
|
# 4. data.tar.gz member
|
|
data_bytes = data_tar_gz.read_bytes()
|
|
deb.write(write_ar_header("data.tar.gz", len(data_bytes), now))
|
|
deb.write(data_bytes)
|
|
if len(data_bytes) % 2 != 0:
|
|
deb.write(b"\n")
|
|
|
|
return output_deb_path
|
|
|
|
|
|
class PackageBuilder:
|
|
def __init__(
|
|
self,
|
|
source_root: Optional[Path] = None,
|
|
output_dir: Optional[Path] = None,
|
|
version: str = "1.3.0",
|
|
) -> None:
|
|
self.source_root = source_root or Path(__file__).resolve().parent.parent
|
|
self.output_dir = output_dir or (self.source_root / "dist")
|
|
self.output_dir.mkdir(parents=True, exist_ok=True)
|
|
self.version = version
|
|
|
|
def build_tarball(self) -> Path:
|
|
"""Builds a standalone linux-updater-<version>.tar.gz with installer."""
|
|
tar_name = f"linux-updater-{self.version}.tar.gz"
|
|
out_path = self.output_dir / tar_name
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_str:
|
|
tmp_root = Path(tmp_str) / f"linux-updater-{self.version}"
|
|
tmp_root.mkdir(parents=True)
|
|
|
|
# Copy package modules
|
|
shutil.copytree(str(self.source_root / "linux_updater"), str(tmp_root / "linux_updater"))
|
|
|
|
# Copy launcher script
|
|
bin_dir = tmp_root / "bin"
|
|
bin_dir.mkdir()
|
|
shutil.copy2(str(self.source_root / "bin" / "linux-updater"), str(bin_dir / "linux-updater"))
|
|
os.chmod(str(bin_dir / "linux-updater"), 0o755)
|
|
|
|
# Copy installer
|
|
shutil.copy2(str(self.source_root / "packaging" / "install.sh"), str(tmp_root / "install.sh"))
|
|
|
|
# Copy example config
|
|
shutil.copy2(
|
|
str(self.source_root / "linux-updater.json.example"),
|
|
str(tmp_root / "linux-updater.json.example"),
|
|
)
|
|
|
|
# Copy man page
|
|
if (self.source_root / "man").exists():
|
|
shutil.copytree(str(self.source_root / "man"), str(tmp_root / "man"))
|
|
|
|
# Archive the tree
|
|
with tarfile.open(str(out_path), "w:gz") as tar:
|
|
tar.add(str(tmp_root), arcname=f"linux-updater-{self.version}")
|
|
|
|
return out_path
|
|
|
|
def build_deb(self) -> Path:
|
|
"""Builds Debian .deb package."""
|
|
deb_filename = f"linux-updater_{self.version}_all.deb"
|
|
deb_output_path = self.output_dir / deb_filename
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_str:
|
|
root = Path(tmp_str)
|
|
control_dir = root / "control"
|
|
data_dir = root / "data"
|
|
control_dir.mkdir()
|
|
data_dir.mkdir()
|
|
|
|
# Render control file
|
|
template_path = self.source_root / "packaging" / "deb" / "control.template"
|
|
template_content = template_path.read_text(encoding="utf-8")
|
|
rendered_control = template_content.format(version=self.version)
|
|
(control_dir / "control").write_text(rendered_control, encoding="utf-8")
|
|
|
|
# Prepare data directory
|
|
usr_bin = data_dir / "usr" / "bin"
|
|
usr_lib = data_dir / "usr" / "lib" / "linux-updater"
|
|
etc_dir = data_dir / "etc"
|
|
var_log = data_dir / "var" / "log" / "linux-updater"
|
|
usr_bin.mkdir(parents=True)
|
|
usr_lib.mkdir(parents=True)
|
|
etc_dir.mkdir(parents=True)
|
|
var_log.mkdir(parents=True)
|
|
|
|
shutil.copy2(str(self.source_root / "bin" / "linux-updater"), str(usr_bin / "linux-updater"))
|
|
os.chmod(str(usr_bin / "linux-updater"), 0o755)
|
|
shutil.copytree(str(self.source_root / "linux_updater"), str(usr_lib / "linux_updater"))
|
|
shutil.copy2(
|
|
str(self.source_root / "linux-updater.json.example"),
|
|
str(etc_dir / "linux-updater.json"),
|
|
)
|
|
if (self.source_root / "man" / "linux-updater.8").exists():
|
|
man_dir = data_dir / "usr" / "share" / "man" / "man8"
|
|
man_dir.mkdir(parents=True, exist_ok=True)
|
|
shutil.copy2(str(self.source_root / "man" / "linux-updater.8"), str(man_dir / "linux-updater.8"))
|
|
|
|
# If dpkg-deb is available, use it; otherwise build native .deb
|
|
if shutil.which("dpkg-deb"):
|
|
pkg_tree = root / "deb_tree"
|
|
shutil.copytree(str(data_dir), str(pkg_tree))
|
|
debian_meta = pkg_tree / "DEBIAN"
|
|
debian_meta.mkdir()
|
|
shutil.copy2(str(control_dir / "control"), str(debian_meta / "control"))
|
|
subprocess.run(["dpkg-deb", "-b", str(pkg_tree), str(deb_output_path)], check=True)
|
|
else:
|
|
# Pure Python build with strict GNU_FORMAT (dpkg rejects PAX 'x' headers)
|
|
def reset_tarinfo(tarinfo):
|
|
tarinfo.uid = 0
|
|
tarinfo.gid = 0
|
|
tarinfo.uname = "root"
|
|
tarinfo.gname = "root"
|
|
tarinfo.pax_headers = {}
|
|
return tarinfo
|
|
|
|
ctrl_tar = root / "control.tar.gz"
|
|
data_tar = root / "data.tar.gz"
|
|
|
|
with tarfile.open(str(ctrl_tar), "w:gz", format=tarfile.GNU_FORMAT) as tar:
|
|
tar.add(str(control_dir / "control"), arcname="./control", filter=reset_tarinfo)
|
|
|
|
with tarfile.open(str(data_tar), "w:gz", format=tarfile.GNU_FORMAT) as tar:
|
|
for item in sorted(data_dir.iterdir()):
|
|
tar.add(str(item), arcname=f"./{item.name}", filter=reset_tarinfo)
|
|
|
|
create_native_deb(ctrl_tar, data_tar, deb_output_path)
|
|
|
|
return deb_output_path
|
|
|
|
def build_rpm(self) -> Path:
|
|
"""Sets up RPM build tree and executes rpmbuild if available."""
|
|
rpm_topdir = self.output_dir / "rpm"
|
|
specs_dir = rpm_topdir / "SPECS"
|
|
sources_dir = rpm_topdir / "SOURCES"
|
|
rpms_dir = rpm_topdir / "RPMS"
|
|
srpms_dir = rpm_topdir / "SRPMS"
|
|
build_dir = rpm_topdir / "BUILD"
|
|
|
|
for d in (specs_dir, sources_dir, rpms_dir, srpms_dir, build_dir):
|
|
d.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 1. Generate source tarball in SOURCES
|
|
tarball_path = sources_dir / f"linux-updater-{self.version}.tar.gz"
|
|
with tempfile.TemporaryDirectory() as tmp_str:
|
|
tmp_root = Path(tmp_str) / f"linux-updater-{self.version}"
|
|
tmp_root.mkdir(parents=True)
|
|
shutil.copytree(str(self.source_root / "linux_updater"), str(tmp_root / "linux_updater"))
|
|
bin_dir = tmp_root / "bin"
|
|
bin_dir.mkdir()
|
|
shutil.copy2(str(self.source_root / "bin" / "linux-updater"), str(bin_dir / "linux-updater"))
|
|
os.chmod(str(bin_dir / "linux-updater"), 0o755)
|
|
shutil.copy2(
|
|
str(self.source_root / "linux-updater.json.example"),
|
|
str(tmp_root / "linux-updater.json.example"),
|
|
)
|
|
if (self.source_root / "man").exists():
|
|
shutil.copytree(str(self.source_root / "man"), str(tmp_root / "man"))
|
|
with tarfile.open(str(tarball_path), "w:gz") as tar:
|
|
tar.add(str(tmp_root), arcname=f"linux-updater-{self.version}")
|
|
|
|
# 2. Copy spec file
|
|
spec_source = self.source_root / "packaging" / "rpm" / "linux-updater.spec"
|
|
target_spec = specs_dir / "linux-updater.spec"
|
|
shutil.copy2(str(spec_source), str(target_spec))
|
|
|
|
# 3. If rpmbuild is available, build the RPM
|
|
if shutil.which("rpmbuild"):
|
|
try:
|
|
subprocess.run(
|
|
[
|
|
"rpmbuild",
|
|
"-ba",
|
|
"--define",
|
|
f"_topdir {rpm_topdir}",
|
|
str(target_spec),
|
|
],
|
|
check=True,
|
|
)
|
|
# Find created .rpm
|
|
rpms = list(rpms_dir.glob("**/*.rpm"))
|
|
if rpms:
|
|
return rpms[0]
|
|
except Exception:
|
|
pass
|
|
|
|
# Return spec path or topdir
|
|
return target_spec
|
|
|
|
def build_all(self) -> Dict[str, Path]:
|
|
tar_p = self.build_tarball()
|
|
deb_p = self.build_deb()
|
|
rpm_p = self.build_rpm()
|
|
return {
|
|
"tarball": tar_p,
|
|
"deb": deb_p,
|
|
"rpm": rpm_p,
|
|
}
|
|
|
|
|
|
def main() -> int:
|
|
builder = PackageBuilder()
|
|
print("Building Linux System Updater distribution packages...")
|
|
results = builder.build_all()
|
|
for name, path in results.items():
|
|
print(f"[{name.upper()}] Artifact created at: {path}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|