ws-arch2: archive/uu (uuencode / xxencode / base64 blocks, read + write), core/Crc16 (ccitt / arc), Archive.files helper, shared fixture helpers (_arch2.py / _arch2_ours.luau) and tests/archive/_entries.luau
core/Crc16 is new (CRC-16/XMODEM for BinHex / MacBinary, CRC-16/ARC for LHA / Zoo). containers/Archive gains Archive.files(value, name?) (additive).
This commit is contained in:
1 parent
51117f9732
commit
8cdb65367a
10 files changed
+679
No files matched your search
@@ -0,0 +1,62 @@
|
||||
# Shared helpers for the ws-arch2 fixture generators (archives, encodings, disk images): reference tools through nix,
|
||||
# our writers' output through tools/fixtures/_arch2_ours.luau, and the standard file set those writers produce.
|
||||
import os, shutil, subprocess, tempfile, zlib
|
||||
|
||||
|
||||
def content(n, seed):
|
||||
"""the bytes of _arch2_ours.luau's content(n, seed)"""
|
||||
out = bytearray(n)
|
||||
s = seed
|
||||
for i in range(n):
|
||||
s = (s * 75 + 74) % 65537
|
||||
out[i] = (i // 7) % 256 if (i // 64) % 3 == 0 else s % 256
|
||||
return bytes(out)
|
||||
|
||||
|
||||
FILES = [("hello.txt", b"hello, archive\n"), ("data.bin", content(5000, 7)), ("empty.txt", b"")]
|
||||
MTIME = 1700000000
|
||||
|
||||
|
||||
def tool(pkgs, argv, cwd=None, input=None, check=True):
|
||||
"""run argv with the nix packages pkgs on PATH (the host's tool when argv[0] is installed and pkgs is empty)"""
|
||||
if isinstance(pkgs, str):
|
||||
pkgs = [pkgs]
|
||||
cmd = (["nix", "shell"] + ["nixpkgs#" + p for p in pkgs] + ["-c"] if pkgs else []) + list(argv)
|
||||
r = subprocess.run(cmd, cwd=cwd, input=input, capture_output=True)
|
||||
if check and r.returncode != 0:
|
||||
raise RuntimeError("%s failed (%d): %s" % (" ".join(argv), r.returncode, r.stderr.decode(errors="replace")))
|
||||
return r.stdout
|
||||
|
||||
|
||||
def ours(ctx, fmt, opts=""):
|
||||
"""our writer's output for the standard file set (bytes)"""
|
||||
script = os.path.join(ctx.root, "tools", "fixtures", "_arch2_ours.luau")
|
||||
exe = shutil.which("luau")
|
||||
argv = ([exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"]) + [script, "-a", fmt, opts]
|
||||
out = subprocess.run(argv, check=True, capture_output=True, cwd=ctx.root).stdout
|
||||
return bytes.fromhex(out.decode().strip())
|
||||
|
||||
|
||||
def tree(files, root):
|
||||
"""write [(name, bytes)] under root (with MTIME)"""
|
||||
for name, data in files:
|
||||
p = os.path.join(root, name)
|
||||
os.makedirs(os.path.dirname(p), exist_ok=True)
|
||||
with open(p, "wb") as f:
|
||||
f.write(data)
|
||||
os.utime(p, (MTIME, MTIME))
|
||||
|
||||
|
||||
def read_tree(root):
|
||||
"""{relative name: crc32} of the files under root"""
|
||||
out = {}
|
||||
for d, _, fs in os.walk(root):
|
||||
for f in fs:
|
||||
p = os.path.join(d, f)
|
||||
if os.path.isfile(p) and not os.path.islink(p):
|
||||
out[os.path.relpath(p, root)] = zlib.crc32(open(p, "rb").read())
|
||||
return out
|
||||
|
||||
|
||||
def tmpdir():
|
||||
return tempfile.mkdtemp(prefix="arch2-")
|
||||
@@ -0,0 +1,43 @@
|
||||
-- tools/fixtures/_arch2_ours.luau: our archive writers' output for the reference tools to read (run by the ws-arch2
|
||||
-- fixture generators through `luau tools/fixtures/_arch2_ours.luau -a <format> [<options as k=v,k=v>]`). Writes the
|
||||
-- standard file set (the same names and bytes as _arch2.FILES) with the format's encoder and prints the file as hex.
|
||||
local F = require("@formats")
|
||||
|
||||
local which, optstr = ...
|
||||
local function content(n: number, seed: number): buffer
|
||||
local b = buffer.create(n)
|
||||
local s = seed
|
||||
for i = 0, n - 1 do
|
||||
s = (s * 75 + 74) % 65537
|
||||
-- runs and noise, so compressors have something to find
|
||||
buffer.writeu8(b, i, if (i // 64) % 3 == 0 then (i // 7) % 256 else s % 256)
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
local opts: { [string]: any } = {}
|
||||
for k, v in string.gmatch(optstr or "", "([%w_]+)=([^,]*)") do
|
||||
opts[k] = tonumber(v) or (if v == "true" then true elseif v == "false" then false else v)
|
||||
end
|
||||
local mod = F.format(which) :: any
|
||||
local a = if mod.new then mod.new() else nil
|
||||
local files = {
|
||||
{ "hello.txt", buffer.fromstring("hello, archive\n") },
|
||||
{ "data.bin", content(5000, 7) },
|
||||
{ "empty.txt", buffer.create(0) },
|
||||
}
|
||||
if opts.single then
|
||||
files = { files[2] }
|
||||
end
|
||||
for _, f in files do
|
||||
a:add(f[1], f[2], { mtime = 1700000000, mode = 0x1A4 })
|
||||
end
|
||||
local out, err = a:write(opts)
|
||||
if not out then
|
||||
error(tostring(err))
|
||||
end
|
||||
local t = table.create(buffer.len(out))
|
||||
for i = 0, buffer.len(out) - 1 do
|
||||
t[i + 1] = string.format("%02x", buffer.readu8(out, i))
|
||||
end
|
||||
print(table.concat(t))
|
||||
@@ -0,0 +1,61 @@
|
||||
# uuencode / xxencode / base64 fixtures (tools/fixtures.py): blocks made by GNU sharutils `uuencode` (classic and -m)
|
||||
# and uudeview's `uuenview` (-u, -x), alone and several in one text with mail-like text around them; our writer's uu /
|
||||
# xx / base64 output (tools/fixtures/_arch2_ours.luau) decoded by `uudecode` / `uudeview`.
|
||||
# tests/gen/uu.luau { cases = { { name, data, entries = { { name, size, crc, mode } } } }, gnu = { { name, data } },
|
||||
# ours = { { variant, ok } } }
|
||||
import importlib.util, os, shutil, zlib
|
||||
|
||||
_s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_arch2.py"))
|
||||
A = importlib.util.module_from_spec(_s)
|
||||
_s.loader.exec_module(A)
|
||||
|
||||
INPUTS = ["tools/fixtures/_arch2.py", "tools/fixtures/_arch2_ours.luau", "src/Formats/archive/uu"]
|
||||
PKGS = ["sharutils", "uudeview"]
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
tmp = A.tmpdir()
|
||||
try:
|
||||
A.tree(A.FILES, tmp)
|
||||
for name, _ in A.FILES:
|
||||
os.chmod(os.path.join(tmp, name), 0o644)
|
||||
ents = {n: {"name": n, "size": len(d), "crc": zlib.crc32(d), "mode": 0o644} for n, d in A.FILES}
|
||||
cases, gnu = [], []
|
||||
for name, _ in A.FILES:
|
||||
uu = A.tool(PKGS, ["uuencode", name, name], cwd=tmp)
|
||||
b64 = A.tool(PKGS, ["uuencode", "-m", name, name], cwd=tmp)
|
||||
gnu.append({"name": name, "uu": uu, "base64": b64})
|
||||
cases.append({"name": "uuencode " + name, "data": uu, "entries": [ents[name]]})
|
||||
cases.append({"name": "uuencode -m " + name, "data": b64, "entries": [ents[name]]})
|
||||
for flag in ("-u", "-x"):
|
||||
out = A.tool(PKGS, ["uuenview", flag, name], cwd=tmp)
|
||||
cases.append({"name": "uuenview %s %s" % (flag, name), "data": out, "entries": [{"name": name, "size": ents[name]["size"], "crc": ents[name]["crc"]}]})
|
||||
# several blocks in one mail-like text
|
||||
text = b"From: someone\nSubject: files\n\nHere they are:\n\n" + gnu[0]["uu"] + b"\nand the other one\n" + gnu[1]["base64"] + b"\n-- \nsig\n"
|
||||
cases.append({"name": "mail with two blocks", "data": text, "entries": [ents["hello.txt"], ents["data.bin"]]})
|
||||
crlf = gnu[1]["uu"].replace(b"\n", b"\r\n")
|
||||
cases.append({"name": "crlf lines", "data": crlf, "entries": [ents["data.bin"]]})
|
||||
ours = []
|
||||
for variant in ("uu", "xx", "base64"):
|
||||
data = A.ours(ctx, "uu", "variant=" + variant)
|
||||
d = A.tmpdir()
|
||||
try:
|
||||
# one block per file: uudecode reads the first block only
|
||||
end = b"====\n" if variant == "base64" else b"end\n"
|
||||
for k, block in enumerate(data.split(end)[:-1]):
|
||||
p = os.path.join(d, "in%d.txt" % k)
|
||||
open(p, "wb").write(block + end)
|
||||
if variant != "xx":
|
||||
A.tool(PKGS, ["uudecode", p], cwd=d)
|
||||
if variant == "xx":
|
||||
p = os.path.join(d, "all.txt")
|
||||
open(p, "wb").write(data)
|
||||
A.tool(PKGS, ["uudeview", "-i", "-p", d + "/", p], cwd=d)
|
||||
got = A.read_tree(d)
|
||||
ok = all(got.get(n) == zlib.crc32(b) for n, b in A.FILES)
|
||||
ours.append({"variant": variant, "ok": ok})
|
||||
finally:
|
||||
shutil.rmtree(d)
|
||||
ctx.emit("uu", {"cases": cases, "gnu": gnu, "ours": ours})
|
||||
finally:
|
||||
shutil.rmtree(tmp)
|
||||
Reference in new issue
Block a user