From a801fa37640c8b0005609a82bd7d67a5fe92eaef Mon Sep 17 00:00:00 2001 From: henry Date: Thu, 3 Mar 2022 06:41:59 -0800 Subject: [PATCH 1/3] bpo-46951: Order contents of zipapps So that builds are more reproducible --- Lib/zipapp.py | 2 +- .../next/Library/2022-03-08-04-46-44.bpo-46951.SWAz97.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-03-08-04-46-44.bpo-46951.SWAz97.rst diff --git a/Lib/zipapp.py b/Lib/zipapp.py index ce77632516c646e..d8ebfcb6c73b0c7 100644 --- a/Lib/zipapp.py +++ b/Lib/zipapp.py @@ -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 index 000000000000000..cd7601aa8c4de75 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-08-04-46-44.bpo-46951.SWAz97.rst @@ -0,0 +1 @@ +Order the contents of zipapp archives, to make builds more reproducible. From e509a45de0af754e9a44559102db7132474ab2f0 Mon Sep 17 00:00:00 2001 From: Henry Finucane Date: Thu, 10 Mar 2022 21:44:19 -0800 Subject: [PATCH 2/3] bpo-46951: Add test to check that the contents of zipapps are ordered --- Lib/test/test_zipapp.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_zipapp.py b/Lib/test/test_zipapp.py index 69f2e55d5638400..eab41fd739c89de 100644 --- a/Lib/test/test_zipapp.py +++ b/Lib/test/test_zipapp.py @@ -54,6 +54,20 @@ 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) + self.assertEqual(zipfile.ZipFile(target, 'r').namelist(), sorted(["__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. From 298d2d20625686dd3977b3da8a906525b58f7f97 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 16 Mar 2022 19:08:14 +0200 Subject: [PATCH 3/3] Update Lib/test/test_zipapp.py --- Lib/test/test_zipapp.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zipapp.py b/Lib/test/test_zipapp.py index eab41fd739c89de..d135c68865812f8 100644 --- a/Lib/test/test_zipapp.py +++ b/Lib/test/test_zipapp.py @@ -66,7 +66,9 @@ def test_create_sorted_archive(self): target = io.BytesIO() zipapp.create_archive(str(source), target) target.seek(0) - self.assertEqual(zipfile.ZipFile(target, 'r').namelist(), sorted(["__main__.py", "bin/", "bin/baz", "bin/qux", "zed.py"])) + 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