Skip to content

Commit

Permalink
bpo-46951: Order contents of zipapps (GH-31713)
Browse files Browse the repository at this point in the history
So that builds are more reproducible.
  • Loading branch information
hfinucane authored May 27, 2022
1 parent bbcf424 commit 47e68d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Lib/test/test_zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def test_create_archive_with_subdirs(self):
self.assertIn('foo/', z.namelist())
self.assertIn('bar/', z.namelist())

def test_create_sorted_archive(self):
# Test that zipapps order their files by name
source = self.tmpdir / 'source'
source.mkdir()
(source / 'zed.py').touch()
(source / 'bin').mkdir()
(source / 'bin' / 'qux').touch()
(source / 'bin' / 'baz').touch()
(source / '__main__.py').touch()
target = io.BytesIO()
zipapp.create_archive(str(source), target)
target.seek(0)
with zipfile.ZipFile(target, 'r') as zf:
self.assertEqual(zf.namelist(),
["__main__.py", "bin/", "bin/baz", "bin/qux", "zed.py"])

def test_create_archive_with_filter(self):
# Test packing a directory and using filter to specify
# which files to include.
Expand Down
2 changes: 1 addition & 1 deletion Lib/zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def create_archive(source, target=None, interpreter=None, main=None,
compression = (zipfile.ZIP_DEFLATED if compressed else
zipfile.ZIP_STORED)
with zipfile.ZipFile(fd, 'w', compression=compression) as z:
for child in source.rglob('*'):
for child in sorted(source.rglob('*')):
arcname = child.relative_to(source)
if filter is None or filter(arcname):
z.write(child, arcname.as_posix())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Order the contents of zipapp archives, to make builds more reproducible.

0 comments on commit 47e68d4

Please sign in to comment.