]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46951: Order contents of zipapps (GH-31713)
authorhfinucane <h.finucane+gh@gmail.com>
Fri, 27 May 2022 16:04:29 +0000 (09:04 -0700)
committerGitHub <noreply@github.com>
Fri, 27 May 2022 16:04:29 +0000 (19:04 +0300)
So that builds are more reproducible.

Lib/test/test_zipapp.py
Lib/zipapp.py
Misc/NEWS.d/next/Library/2022-03-08-04-46-44.bpo-46951.SWAz97.rst [new file with mode: 0644]

index 69f2e55d5638400f41c83e574a4b1dd19e319592..d135c68865812f83ed795b330e4f57fd744a02d1 100644 (file)
@@ -54,6 +54,22 @@ class ZipAppTest(unittest.TestCase):
             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.
index ce77632516c646ecc11e231a1d855e3a48670a71..d8ebfcb6c73b0c7bdec4c69a446d30e310526492 100644 (file)
@@ -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())
diff --git a/Misc/NEWS.d/next/Library/2022-03-08-04-46-44.bpo-46951.SWAz97.rst b/Misc/NEWS.d/next/Library/2022-03-08-04-46-44.bpo-46951.SWAz97.rst
new file mode 100644 (file)
index 0000000..cd7601a
--- /dev/null
@@ -0,0 +1 @@
+Order the contents of zipapp archives, to make builds more reproducible.