import gzip
import io
import tarfile
from pathlib import Path
HERE = Path(__file__).parent
CARGO_TOML = '[package]\nname = "widget"\nversion = "0.1.0"\nedition = "2021"\n'
SOURCE = "pub fn add(a: i32, b: i32) -> i32 {\n a + b\n}\n"
INTEGRATION_TEST = "#[test]\nfn adds() {\n assert_eq!(widget::add(1, 2), 3);\n}\n"
def write_crate(path: Path, root: str, files: dict) -> None:
raw = io.BytesIO()
with tarfile.open(fileobj=raw, mode="w") as tar:
for name, content in sorted(files.items()):
data = content.encode()
info = tarfile.TarInfo(f"{root}/{name}")
info.size = len(data)
info.mtime = 0
tar.addfile(info, io.BytesIO(data))
with gzip.GzipFile(path, "wb", mtime=0) as gz:
gz.write(raw.getvalue())
common = {"Cargo.toml": CARGO_TOML, "src/lib.rs": SOURCE}
write_crate(HERE / "widget-0.1.0.crate", "widget-0.1.0", {**common, "tests/integration.rs": INTEGRATION_TEST})
write_crate(HERE / "clean-0.1.0.crate", "clean-0.1.0", common)
print("wrote widget-0.1.0.crate and clean-0.1.0.crate")