Make Python Gzip Artifacts Reproducible with Fixed Headers
A build compresses the same asset manifest twice, but its checksum changes. Before investigating the manifest generator, check the gzip wrapper: metadata can differ even when decompression produces identical bytes. This creates unnecessary uploads when a deployment tool uses compressed-file hashes to detect changes.
These examples use Python 3.10 or later and the standard library. They target repeatable output within a pinned Python and compression-library environment. The input is an already prepared byte sequence; JSON serialization and source-file ordering belong to the preceding build step.
Reproduce a Metadata-Only Difference
Run this standalone example. Explicit timestamps make the demonstration reliable without waiting between writes:
| |
The changed bytes do not indicate changed manifest content. Comparing only the decompressed result would miss the reason a compressed-artifact cache keeps invalidating.
Fix the Timestamp and Omit the Filename
Save this helper as stable_gzip.py:
| |
The
Python GzipFile reference
documents both header inputs: an omitted modification time uses the current time, and a file object’s name can supply an original filename. Setting mtime=0 fixes the timestamp; filename="" omits the filename. An explicit compression level also makes the build’s choice visible.
Read the buffer after the context exits, so the gzip stream has its trailer. Closing GzipFile leaves the supplied buffer open. The helper requires bytes to keep text encoding outside the compression step.
Separate the Artifact Name from Its Contents
Save this demonstration beside the helper and run it in a scratch directory:
| |
The destination paths describe where the artifacts are stored. They never reach the gzip writer, so renaming an output cannot change its embedded metadata. This example overwrites those two demonstration files; choose fresh paths when trying it with other assets.
For large assets, use a streaming writer with the same explicit header settings rather than collecting both source and compressed bytes in memory. Keep source reading, gzip writing, and output closing in a defined order. Writing directly to a final destination can leave a partial artifact after an error; staging and publication are separate concerns.
Test Equality and the Round Trip
Save this as test_stable_gzip.py beside the helper and run python test_stable_gzip.py:
| |
Equality catches variation, while decompression checks that the artifact still represents the intended payload. The header assertions identify the two metadata rules directly. The samples cover an empty stream, binary data, and repetitive content without depending on one exact compressed byte string.
Define the Reproducibility Boundary
Fixed headers do not promise identical compressed bytes across Python or zlib upgrades. Pin the build environment when compressed hashes form part of a release contract, and review artifact changes when upgrading it. The
gzip.compress version notes
also describe a header OS-byte difference in Python 3.11 and 3.12 when mtime=0 delegates to zlib; API and runtime choices matter.
Make the uncompressed input stable first: choose a text encoding, a newline convention, and a serialization order before compression. If two payloads already differ, changing gzip metadata cannot make their artifacts equivalent. When a checksum changes, compare the input bytes, header policy, compression settings, and environment in that order.