Package and Document a Reproducible CLI
Core idea
For this dependency-free tool, distribute a Python zip application containing main.py and the package source. It requires an installed compatible Python interpreter; it is not a standalone native executable or an installed wheel. A reproducible artifact needs deterministic file order, explicit ZIP metadata, fixed source bytes and a documented build process. The supplied builder uses ZIP_STORED and fixed timestamps and permissions, includes only the declared source files, and records SHA-256. Two builds on the same runtime are compared byte for byte. The digest helps detect changes, not establish publisher authenticity.
from io import BytesIOfrom zipfile import ZipFile, ZipInfodef build(): data = BytesIO() with ZipFile(data, 'w') as archive: info = ZipInfo('__main__.py', (1980, 1, 1, 0, 0, 0)) info.create_system = 3 info.external_attr = 0o100644 << 16 archive.writestr(info, b'print(7)\n') return data.getvalue()print(build() == build())Both archives use the same bytes and explicit metadata on this runtime. The complete builder also fixes order and included files.
Expected output:
TrueCheck a digest against expected bytes
from hashlib import sha256original = b'report-v1'expected = sha256(original).hexdigest()print(sha256(original).hexdigest() == expected)print(sha256(b'report-v2').hexdigest() == expected)A trusted expected digest detects this change. Generating a digest from an untrusted artifact alone does not authenticate it.
Expected output:
TrueFalseCommon mistake
Do not call a .pyz a universal binary or claim bit reproducibility across all toolchains from two local builds. Include runtime requirements, exact command, input example, error codes, test command, scope and known limits in the README.
Practice
Build the supplied app twice using build.py and compare digests. Execute both archives with sample.json, then with an invalid input. Record outputs and exit codes in your release notes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Packaging and documentation
Check your understanding of packaging and documentation.
What is printed?
from zipfile import ZipInfoprint(ZipInfo('x').date_time[0])What is printed?
from hashlib import sha256print(len(sha256(b'hello').hexdigest()))What does a .pyz still require?
What does two byte-identical local builds prove?