🗜️ archive#

Zips the generated datapack and resource pack.

TEXT_EXTENSIONS: frozenset[str] = frozenset({'.fsh', '.glsl', '.json', '.lang', '.map', '.mcfunction', '.mcmeta', '.md', '.properties', '.snbt', '.txt', '.vsh', '.yaml', '.yml'})[source]#

Extensions whose entries hold text, so their line endings are normalized on the way into the archive. Everything else is archived byte for byte, since a CRLF inside a .png or an .ogg is data rather than a line ending.

BEFORE_NEWLINE: Pattern[bytes] = re.compile(b'\\r+\\n')[source]#

Carriage returns opening a line ending, however many of them there are. A file welded out of a library archive already holding CRLF reaches this as CRCRLF. TextIOWrapper translates the newline it finds behind the carriage return that was already there.

get_consistent_timestamp(
ctx: Context,
) → tuple[int, int, int, int, int, int][source]#

Date written on every archive entry: the last commit of the beet config, in UTC, identical in every clone.

A file modification time is not kept by git, so it is only the fallback: the beet cache one, then 2025-01-01.

class ConstantTimeZipFile(
file: Any,
mode: Literal['r', 'w', 'x', 'a'] = 'r',
*args: Any,
date_time: tuple[int, int, int, int, int, int],
skip_names: tuple[str, ...] = (),
**kwargs: Any,
)[source]#

Bases: ZipFile

ZipFile that forces a constant timestamp and normalized metadata on every entry.

Replaces the old two-pass approach (dump to zip, read everything back, re-deflate with fixed timestamps): every write path used by pack.dump() (open(mode="w"), writestr and write) goes through a fresh ZipInfo carrying the constant date_time, so a single compression pass produces the exact same bytes.

Entries whose name is in skip_names are silently dropped (used to replace pack.mcmeta/pack.png with fixed content afterwards via force_writestr()).

A text entry is written with Unix line endings whatever the host would have used, see unix_newlines().

When the archive replaces one written before, an entry holding the same bytes as there is copied still compressed instead of being deflated again, see store().

previous: ZipFile | None[source]#

The archive this one replaces, read before it was truncated, to reuse its unchanged entries.

force_writestr(
name: str,
data: bytes,
) → None[source]#

Write an entry with the constant timestamp, bypassing skip_names.

Drops name from skip_names first: ZipFile.writestr internally goes through self.open which would otherwise discard the entry again.

store(name: str, data: bytes) → None[source]#

Write one entry, with Unix line endings for text, reusing its compressed bytes from the previous archive when unchanged.

Deflate gives the same bytes for the same input, so the copy is what compressing again would have written. The previous entry is decompressed and compared whole: a matching checksum alone is not trusted.

previous_entry(
name: str,
) → bytes | None[source]#

The content of an entry of the archive this one replaces, None when there is no such entry.

previous_raw(
name: str,
data: bytes,
) → bytes | None[source]#

The still compressed bytes of the previous archive’s entry when it holds exactly data, else None.

class EntryWriter(
archive: ConstantTimeZipFile,
name: str,
)[source]#

Bases: BytesIO

Collects what is written to one entry, and stores it in the archive once closed.

>>> archive = ConstantTimeZipFile(io.BytesIO(), "w", date_time=(2025, 1, 1, 0, 0, 0))
>>> with archive.open("say.mcfunction", "w") as entry:
...     _ = entry.write(b"say a\r\nsay b\r\r\n")
>>> archive.read("say.mcfunction")
b'say a\nsay b\n'
is_text_entry(name: str) → bool[source]#

Whether an archive entry holds text, so its line endings are ours to normalize.

>>> is_text_entry("data/ns/function/tick.mcfunction"), is_text_entry("pack.png")
(True, False)
unix_lines(data: bytes) → bytes[source]#

The same bytes with every line ending turned into a LF.

bytes.replace runs eleven times faster than the pattern. It covers the CRLF a Windows TextIOWrapper writes, which is all but every entry of an archive built here. The pattern is only there for a run of carriage returns, which nothing but a welded library archive produces.

>>> unix_lines(b"say a\r\nsay b\r\r\n")
b'say a\nsay b\n'
unix_newlines(name: str, data: str | bytes) → str | bytes[source]#

The same data with every line ending turned into a LF, for a text entry only.

>>> unix_newlines("tick.mcfunction", b"say a\r\nsay b\r\r\n"), unix_newlines("icon.png", b"\r\n")
(b'say a\nsay b\n', b'\r\n')
beet_default(ctx: Context) → None[source]#

Archive plugin for StewBeet. Creates zip archives of the generated datapack and resource pack using pack.dump() to avoid interfering with existing pack directories.

Parameters:

ctx – The beet context.

silent#