Skip to content

Commit

Permalink
fix reproducibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Aug 6, 2023
1 parent 84d9da8 commit 339a179
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pex/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,13 @@ def zip(
# If labels are provided, respect the given ordering, but still sort the files
# within each label to get deterministic output.
sorted(self.filesets.get(label, ()))
# NB: An iterable of labels with non-deterministic order is not reproducible!
for label in labels
)
)
else:
selected_files = OrderedSet(self.files())
# Otherwise, sort the files to get reproducible output by default.
selected_files = OrderedSet(sorted(self.files()))

compression = zipfile.ZIP_DEFLATED if compress else zipfile.ZIP_STORED
with open_zip(filename, mode, compression) as zf:
Expand Down
6 changes: 3 additions & 3 deletions pex/pex_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ def iter_bootstrap_script_labels(cls):
def iter_metadata_labels(cls):
# type: () -> Iterator[str]
"""``Chroot`` labels covering metadata files."""
# PEX-INFO: This is accessed after unpacking the zip.
# PEX-INFO
yield "manifest"

@classmethod
Expand All @@ -733,8 +733,8 @@ def iter_bootstrap_libs_labels(cls):
def iter_deps_libs_labels(cls, pex_info):
# type: (PexInfo) -> Iterator[str]
"""``Chroot`` labels covering the third-party code that was resolved into dists."""
# Subdirectories of .deps:
for dist_label in pex_info.distributions.keys():
# Subdirectories of .deps/: Keys need to be sorted for deterministic output.
for dist_label in sorted(pex_info.distributions.keys()):
yield dist_label

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/test_reproducible.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def explode_pex(path):
), "{} and {} have different content.".format(member1, member2)
# Check that the entire file is equal, including metadata.
assert filecmp.cmp(member1, member2, shallow=False)
# Check that the file list is identical.
with ZipFile(pex1) as zfp1, ZipFile(pex2) as zfp2:
assert zfp1.namelist() == zfp2.namelist()
# Finally, check that the .pex files are byte-for-byte identical.
assert filecmp.cmp(pex1, pex2, shallow=False)

Expand Down

0 comments on commit 339a179

Please sign in to comment.