Viewing files at commit e70815b9044aCommit detailsView latest

scripts/oakbench/fixtures.py 32 lines · 1.1 KB · python
1
"""Deterministic fixture helpers.
2
3
Fixture bytes are identity: the same (prefix, seed, size) always produces the
4
same content, and changing any of them is a fixture_version bump (ADR-0005).
5
"""
6
7
from __future__ import annotations
8
9
import shutil
10
from pathlib import Path
11
12
13
def write_pattern_file(path: Path, size: int, seed: str, binary: bool, prefix: str = "oakbench") -> None:
14
    path.parent.mkdir(parents=True, exist_ok=True)
15
    if binary:
16
        unit = (f"{prefix}:{seed}:".encode("utf-8") * 4096)[: 64 * 1024]
17
    else:
18
        line = f"{prefix} {seed} " + ("x" * 72) + "\n"
19
        unit = line.encode("utf-8") * max(1, (64 * 1024 // len(line)))
20
    remaining = size
21
    with path.open("wb") as fh:
22
        while remaining > 0:
23
            chunk = unit[: min(len(unit), remaining)]
24
            fh.write(chunk)
25
            remaining -= len(chunk)
26
27
28
def copy_fixture(src: Path, dest: Path, ignore_marker: bool = False) -> None:
29
    if dest.exists():
30
        shutil.rmtree(dest)
31
    ignore = shutil.ignore_patterns(".fixture-ready") if ignore_marker else None
32
    shutil.copytree(src, dest, ignore=ignore)