Files
rosetta/tools/fixtures/7z.py
T

104 lines
4.7 KiB
Python

# 7z fixtures (tools/fixtures.py): a small tree (text, x86 machine code, runs, an empty file, empty and nested
# directories, UTF-16 names with accents and a surrogate pair) archived by 7-Zip 2x (nix _7zz) with many method chains:
# LZMA / LZMA2 / copy / Deflate, solid and non-solid, BCJ / ARM64 / delta filters, plain and compressed headers; plus
# BZip2 archives (codecs/Bzip2), archives whose methods are not supported (PPMd, BCJ2, AES) and an empty archive.
# tests/gen/7z.luau { files = { { name, dir, data?, mtime } }, cases = { { name, data, unsupported? } } }
import os, random, subprocess, sys, tempfile
TOOL = "_7zz"
def tree(rng):
with open(os.path.realpath(sys.executable), "rb") as f:
code = f.read(60000)
base = os.path.dirname(os.__file__)
text = b""
for name in sorted(os.listdir(base)):
if name.endswith(".py"):
with open(os.path.join(base, name), "rb") as f:
text += f.read()
if len(text) > 50000:
break
return [
("readme.txt", text[:50000]),
("bin/code.bin", code),
("bin/runs.dat", b"ab" * 5000 + b"\0" * 9000 + bytes(range(256)) * 8),
("bin/random.dat", rng.randbytes(3000)),
("empty.txt", b""),
("emptydir/", None),
("a/b/c/deep.txt", b"deep\n" * 10),
("héllo wörld ☃.txt", "unicode ☃\n".encode()),
("\U0001d11e clef.txt", b"g clef\n"),
]
def generate(ctx):
rng = random.Random(77)
files = tree(rng)
mtime = 1600000000
out_files = []
cases = []
with tempfile.TemporaryDirectory() as d:
src = os.path.join(d, "src")
for name, data in files:
p = os.path.join(src, name)
if data is None:
os.makedirs(p, exist_ok=True)
os.utime(p, (mtime, mtime))
out_files.append({"name": name.rstrip("/"), "dir": True, "mtime": mtime})
else:
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(p, "wb") as f:
f.write(data)
os.utime(p, (mtime + 60, mtime + 60))
out_files.append({"name": name, "dir": False, "data": data, "mtime": mtime + 60})
for sub in ("bin", "a", "a/b", "a/b/c"):
os.utime(os.path.join(src, sub), (mtime, mtime))
out_files.append({"name": sub, "dir": True, "mtime": mtime})
def make(name, args, unsupported=None):
arc = os.path.join(d, name + ".7z")
if os.path.exists(arc):
os.remove(arc)
subprocess.run(["nix", "shell", "nixpkgs#" + TOOL, "-c", "7zz", "a", "-bd", "-y", arc] + args + ["."],
cwd=src, capture_output=True, check=True)
with open(arc, "rb") as f:
c = {"name": name, "data": f.read()}
if unsupported:
c["unsupported"] = unsupported
cases.append(c)
make("lzma-nonsolid", ["-m0=LZMA", "-ms=off", "-mhc=off"])
make("lzma-solid", ["-m0=LZMA:d=1m", "-ms=on"])
make("lzma2-solid", ["-m0=LZMA2", "-ms=on"])
make("lzma2-mt-blocks", ["-m0=LZMA2:d=64k:c=20k", "-ms=on", "-mmt=4"])
make("lzma2-hc-off", ["-m0=LZMA2", "-mhc=off"])
make("copy", ["-m0=Copy", "-mhc=off"])
make("copy-hc", ["-m0=Copy"])
make("deflate", ["-m0=Deflate", "-ms=on"])
make("bcj-lzma", ["-m0=BCJ", "-m1=LZMA", "-ms=on"])
make("bcj-lzma2", ["-m0=BCJ", "-m1=LZMA2", "-ms=off"])
make("arm64-lzma2", ["-m0=ARM64", "-m1=LZMA2"])
make("arm-lzma2", ["-m0=ARM", "-m1=LZMA2"])
make("delta-lzma", ["-m0=Delta:4", "-m1=LZMA"])
make("ppc-sparc", ["-m0=PPC", "-m1=SPARC", "-m2=LZMA2"])
make("lzma-lc-lp", ["-m0=LZMA:lc=0:lp=2:pb=0", "-ms=e"])
make("auto", ["-mx=9"])
make("fast", ["-mx=1"])
make("bzip2", ["-m0=BZip2", "-mhc=off"])
make("bzip2-solid-bcj", ["-m0=BCJ", "-m1=BZip2", "-ms=on", "-mhc=on"])
make("ppmd", ["-m0=PPMd", "-mhc=off"], unsupported="ppmd")
make("bcj2", ["-m0=BCJ2", "-m1=LZMA", "-m2=LZMA", "-m3=LZMA", "-mb0s0:1", "-mb0s1:2", "-mb0s2:3", "-mhc=off"],
unsupported="bcj2")
make("aes", ["-m0=LZMA2", "-mhc=off", "-pSecret"], unsupported="aes")
# an empty archive
arc = os.path.join(d, "empty.7z")
empty = os.path.join(d, "emptysrc")
os.makedirs(empty)
subprocess.run(["nix", "shell", "nixpkgs#" + TOOL, "-c", "7zz", "a", "-bd", "-y", arc, "-x!*", "."],
cwd=empty, capture_output=True)
if os.path.exists(arc):
with open(arc, "rb") as f:
cases.append({"name": "empty", "data": f.read(), "empty": True})
ctx.emit("7z", {"files": out_files, "cases": cases})