102 lines
5.2 KiB
Python
102 lines
5.2 KiB
Python
# rpm fixtures (tools/fixtures.py): one package built by rpmbuild (nixpkgs#rpm) with each payload compressor (gzip,
|
|
# bzip2, xz, lzma, zstd, none), holding files, a symlink, a hard link pair, a directory and a %ghost file. The expected
|
|
# header fields and file list come from `rpm -qp --qf`, the file data from rpm2cpio's cpio stream (parsed here).
|
|
# tests/gen/rpm.luau { rpms = { { name, data, compressor, fields = { name, version, ... }, files = { { name, size,
|
|
# mode, mtime, link?, crc? } }, cpioCrc } } }
|
|
import os, shutil, subprocess, tempfile, zlib
|
|
|
|
SPEC = r"""
|
|
Name: hello
|
|
Version: 1.0
|
|
Release: 3
|
|
Epoch: 2
|
|
Summary: a greeting
|
|
License: MIT
|
|
URL: https://example.org/hello
|
|
Group: Applications/Text
|
|
BuildArch: noarch
|
|
%description
|
|
prints a greeting
|
|
and exits
|
|
%install
|
|
mkdir -p %{buildroot}/usr/bin %{buildroot}/usr/share/doc/hello %{buildroot}/var/log
|
|
printf '#!/bin/sh\necho hello\n' > %{buildroot}/usr/bin/hello
|
|
chmod 755 %{buildroot}/usr/bin/hello
|
|
python3 -c "import random; open('%{buildroot}/usr/share/doc/hello/README', 'wb').write(random.Random(3).randbytes(20000))"
|
|
printf 'a long name' > "%{buildroot}/usr/share/doc/hello/a file name with spaces and more than one hundred characters, to pass every old limit of the cpio or tar headers.txt"
|
|
ln -s hello %{buildroot}/usr/bin/hi
|
|
ln %{buildroot}/usr/bin/hello %{buildroot}/usr/bin/hello2
|
|
touch %{buildroot}/var/log/hello.log
|
|
%files
|
|
/usr/bin/hello
|
|
/usr/bin/hello2
|
|
/usr/bin/hi
|
|
%dir /usr/share/doc/hello
|
|
/usr/share/doc/hello/README
|
|
"/usr/share/doc/hello/a file name with spaces and more than one hundred characters, to pass every old limit of the cpio or tar headers.txt"
|
|
%ghost /var/log/hello.log
|
|
"""
|
|
|
|
QF = "%{NAME}\\n%{VERSION}\\n%{RELEASE}\\n%{EPOCH}\\n%{ARCH}\\n%{OS}\\n%{LICENSE}\\n%{URL}\\n%{PAYLOADCOMPRESSOR}\\n%{PAYLOADFORMAT}\\n%{SUMMARY}\\n"
|
|
FQF = "[%{FILENAMES}\\t%{FILESIZES}\\t%{FILEMODES}\\t%{FILEMTIMES}\\t%{FILELINKTOS}\\t%{FILEFLAGS}\\n]"
|
|
|
|
|
|
def cpio_files(d):
|
|
out, ino, p = {}, {}, 0
|
|
while p < len(d):
|
|
h = d[p:p + 110]
|
|
f = [int(h[6 + 8 * i:14 + 8 * i], 16) for i in range(13)]
|
|
ns, fs = f[11], f[6]
|
|
name = d[p + 110:p + 110 + ns - 1].decode()
|
|
p = (p + 110 + ns + 3) & ~3
|
|
if name == "TRAILER!!!":
|
|
out[None] = p # the archive's end (rpm2cpio pads its output with zeros)
|
|
break
|
|
out[name[2:] if name.startswith("./") else name] = f[0]
|
|
if fs:
|
|
ino[f[0]] = d[p:p + fs]
|
|
p = (p + fs + 3) & ~3
|
|
return {n: (i if n is None else ino.get(i, b"")) for n, i in out.items()} # hard links: the data of their inode
|
|
|
|
|
|
def generate(ctx):
|
|
tmp = tempfile.mkdtemp()
|
|
rpms = []
|
|
try:
|
|
with open(os.path.join(tmp, "hello.spec"), "w") as f:
|
|
f.write(SPEC)
|
|
env = dict(os.environ, SOURCE_DATE_EPOCH="1700000000")
|
|
for comp, io in (("gzip", "w9.gzdio"), ("bzip2", "w9.bzdio"), ("xz", "w6.xzdio"), ("lzma", "w6.lzdio"), ("zstd", "w19.zstdio"), ("none", "w0.ufdio")):
|
|
top = os.path.join(tmp, "top-" + comp)
|
|
subprocess.run(["nix", "shell", "nixpkgs#rpm", "nixpkgs#python3", "-c", "rpmbuild", "--define", "_topdir " + top,
|
|
"--define", "_binary_payload " + io, "--define", "__os_install_post %{nil}",
|
|
"--define", "_build_id_links none", "--define", "use_source_date_epoch_as_buildtime 1",
|
|
"--define", "clamp_mtime_to_source_date_epoch 1", "-bb", "hello.spec"],
|
|
cwd=tmp, capture_output=True, check=True, env=env)
|
|
path = os.path.join(top, "RPMS", "noarch", "hello-1.0-3.noarch.rpm")
|
|
data = open(path, "rb").read()
|
|
q = lambda fmt: subprocess.run(["nix", "shell", "nixpkgs#rpm", "-c", "rpm", "--dbpath", os.path.join(tmp, "db"), "-qp", "--qf", fmt, path],
|
|
capture_output=True, check=True).stdout.decode()
|
|
v = q(QF).split("\n")
|
|
fields = dict(zip(["name", "version", "release", "epoch", "arch", "os", "license", "url", "payloadCompressor", "payloadFormat", "summary"], v))
|
|
fields["epoch"] = int(fields["epoch"])
|
|
if comp == "none":
|
|
del fields["payloadCompressor"] # whatever rpm stores for "no compression" is checked by the test itself
|
|
raw = subprocess.run(["nix", "shell", "nixpkgs#rpm", "-c", "rpm2cpio", path], capture_output=True, check=True).stdout
|
|
content = cpio_files(raw)
|
|
files = []
|
|
for line in q(FQF).split("\n")[:-1]:
|
|
name, size, mode, mtime, link, flags = line.split("\t")
|
|
e = {"name": name.lstrip("/"), "size": int(size), "mode": int(mode) & 0xFFF, "type": int(mode) & 0xF000,
|
|
"mtime": int(mtime), "flags": int(flags)}
|
|
if link:
|
|
e["link"] = link
|
|
if e["name"] in content and e["type"] == 0x8000:
|
|
e["crc"] = zlib.crc32(content[e["name"]])
|
|
files.append(e)
|
|
rpms.append({"name": "rpmbuild-" + comp, "data": data, "compressor": comp, "fields": fields, "files": files,
|
|
"cpioCrc": zlib.crc32(raw[:content[None]]), "cpioSize": content[None]})
|
|
finally:
|
|
shutil.rmtree(tmp)
|
|
ctx.emit("rpm", {"rpms": rpms})
|