Skip to content

Commit 19c52db

Browse files
committed
fix: make file ordering deterministic
1 parent d8303a6 commit 19c52db

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

package_python_function/packager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def zip_all_dependencies(self, target_path: Path) -> None:
4646

4747
with ZipFile(target_path, "w", ZIP_DEFLATED) as zip_file:
4848
def zip_dir(path: Path) -> None:
49-
for item in path.iterdir():
49+
# use sorted to make sure files are always written in a deterministic order
50+
for item in sorted(path.iterdir(), key=lambda i: i.name):
5051
if item.is_dir():
5152
if item.name not in self.DIRS_TO_EXCLUDE:
5253
zip_dir(item)

tests/test_determinism.py

Whitespace-only changes.

tests/test_package_python_function.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ def test_package_python_function(
7272
else:
7373
assert (verify_dir / file.path).exists()
7474

75+
def test_python_package_deterministic_file_ordering(
76+
test_data: Data,
77+
tmp_path: Path,
78+
):
79+
zip_contents: dict[str, list[str]] = {}
80+
for id in ["a", "b"]:
81+
output = tmp_path / f"output_{id}"
82+
output.mkdir()
83+
sys.argv = [
84+
"test_package_python_function",
85+
str(test_data.venv_dir),
86+
"--project",
87+
str(test_data.pyproject.path),
88+
"--output-dir",
89+
str(output),
90+
]
91+
main()
92+
zip_file = output / f"{test_data.pyproject.name.replace('-', '_')}.zip"
93+
with zipfile.ZipFile(zip_file, "r") as zip_file:
94+
zip_contents[id] = list(map(lambda zi: zi.filename, zip_file.infolist()))
95+
96+
assert zip_contents["a"] == zip_contents["b"], "File ordering in the zip file is not deterministic across runs."
97+
7598
@pytest.mark.parametrize(
7699
"src_epoch, expected_exception, expected_date_time",
77100
[

0 commit comments

Comments
 (0)