Files

82 lines
4.3 KiB
Python

# Docs inputs for the archive page's Unix / package sections (tools/fixtures.py; tools/docs_check.py explains the
# inputs): a newc cpio by GNU cpio, a .deb by dpkg-deb, an .rpm by rpmbuild, a .tar.Z by ncompress, an ISO image by
# xorriso (-J -R, with an El Torito boot image) and an .lz4 by the lz4 tool, all of one small tree.
# tests/gen/docs_arch_unix.luau { files = { [name] = bytes }, vars = { cpioBytes = "tree.cpio", ... } }
import importlib.util, io, os, subprocess, tarfile, tempfile
INPUTS = ["tools/docs_check.py"]
MTIME = 1767225600 # 2026-01-01T00:00:00Z
SPEC = """Name: hello
Version: 1.0
Release: 1
Summary: a greeting
License: MIT
BuildArch: noarch
%description
prints a greeting
%install
mkdir -p %{buildroot}/usr/bin
printf '#!/bin/sh\\necho hello\\n' > %{buildroot}/usr/bin/hello
chmod 755 %{buildroot}/usr/bin/hello
%files
/usr/bin/hello
"""
def docs_check(ctx):
spec = importlib.util.spec_from_file_location("docs_check", os.path.join(ctx.root, "tools", "docs_check.py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def sh(pkgs, script, cwd, env=None):
argv = ["nix", "shell"] + ["nixpkgs#" + p for p in pkgs] + ["-c", "sh", "-c", script]
return subprocess.run(argv, cwd=cwd, capture_output=True, check=True, env=env).stdout
def generate(ctx):
dc = docs_check(ctx)
files = {}
env = dict(os.environ, SOURCE_DATE_EPOCH=str(MTIME))
with tempfile.TemporaryDirectory() as d:
tree = os.path.join(d, "tree")
os.makedirs(os.path.join(tree, "bin"))
with open(os.path.join(tree, "readme.txt"), "wb") as f:
f.write(b"rosetta unix archives\n")
with open(os.path.join(tree, "bin", "run.sh"), "wb") as f:
f.write(b"#!/bin/sh\necho hi\n")
os.chmod(os.path.join(tree, "bin", "run.sh"), 0o755)
os.symlink("bin/run.sh", os.path.join(tree, "run"))
with open(os.path.join(tree, "boot.img"), "wb") as f:
f.write(b"\xfa\xeb\xfe" + bytes(2045))
for dirpath, dirs, fs in os.walk(tree):
for n in dirs + fs:
os.utime(os.path.join(dirpath, n), (MTIME, MTIME), follow_symlinks=False)
files["tree.cpio"] = sh(["cpio", "findutils"], "find readme.txt bin run | LC_ALL=C sort | cpio -o -H newc --reproducible 2>/dev/null", tree)
files["disc.iso"] = sh(["xorriso"], "xorriso -as mkisofs -quiet -no-pad -J -R -V DOCS -b boot.img -no-emul-boot -boot-load-size 4 -o - .", tree)
b = io.BytesIO()
with tarfile.open(fileobj=b, mode="w", format=tarfile.PAX_FORMAT) as t:
for n in ("readme.txt", "bin/run.sh"):
t.add(os.path.join(tree, n), arcname=n)
files["src.tar.Z"] = subprocess.run(["nix", "shell", "nixpkgs#ncompress", "-c", "compress", "-c", "-f"], input=b.getvalue(),
capture_output=True, check=True).stdout
files["notes.txt.lz4"] = subprocess.run(["nix", "shell", "nixpkgs#lz4", "-c", "lz4", "-c"], input=b"rosetta unix archives\n" * 8,
capture_output=True, check=True).stdout
pkg = os.path.join(d, "pkg")
os.makedirs(os.path.join(pkg, "DEBIAN"))
os.makedirs(os.path.join(pkg, "usr", "bin"))
with open(os.path.join(pkg, "DEBIAN", "control"), "w") as f:
f.write("Package: hello\nVersion: 1.0-1\nArchitecture: all\nMaintainer: Ada <[email protected]>\nDescription: a greeting\n")
with open(os.path.join(pkg, "usr", "bin", "hello"), "wb") as f:
f.write(b"#!/bin/sh\necho hello\n")
os.chmod(os.path.join(pkg, "usr", "bin", "hello"), 0o755)
files["hello.deb"] = sh(["dpkg"], "dpkg-deb --root-owner-group -Zxz --build pkg out.deb >/dev/null && cat out.deb", d, env)
with open(os.path.join(d, "hello.spec"), "w") as f:
f.write(SPEC)
sh(["rpm"], "rpmbuild --define '_topdir %s/top' --define '__os_install_post %%{nil}' --define '_build_id_links none' -bb hello.spec >/dev/null 2>&1" % d, d, env)
files["hello.rpm"] = open(os.path.join(d, "top", "RPMS", "noarch", "hello-1.0-1.noarch.rpm"), "rb").read()
dc.emit_inputs(ctx, files, {"cpioBytes": "tree.cpio", "isoBytes": "disc.iso", "tarZBytes": "src.tar.Z", "lz4Bytes": "notes.txt.lz4",
"debBytes": "hello.deb", "rpmBytes": "hello.rpm"})