From c1f517ae6e386beace466363a1df763af09165f1 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Thu, 29 Sep 2022 17:01:52 -0400 Subject: [PATCH] Fix zip distribution does not include leading directory (#2672) * Fix zip distribution does not include leading directory Signed-off-by: Peter Zhu * Resolve the path issues failed on Windows Signed-off-by: Peter Zhu Signed-off-by: Peter Zhu --- src/assemble_workflow/dist.py | 10 ++++++++-- .../tests_assemble_workflow/test_bundle_opensearch.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/assemble_workflow/dist.py b/src/assemble_workflow/dist.py index da20186920..1e157a1525 100644 --- a/src/assemble_workflow/dist.py +++ b/src/assemble_workflow/dist.py @@ -102,11 +102,17 @@ def __extract__(self, dest: str) -> None: def __build__(self, name: str, dest: str) -> None: with ZipFile(name, "w", zipfile.ZIP_DEFLATED) as zip: - rootlen = len(self.archive_path) + 1 + # root : /tmp/tmp********/opensearch- + # leadingdir : opensearch- + # root no leading dir: /tmp/tmp********/ + # This is to preserve the leading directory `opensearch-` in zip + rootlen = len(self.archive_path) + leadingdirlen = len(os.path.basename(self.archive_path)) + noleadingdirlen = rootlen - leadingdirlen for base, _, files in os.walk(self.archive_path): for file in files: fn = os.path.join(base, file) - zip.write(fn, fn[rootlen:]) + zip.write(fn, fn[noleadingdirlen:]) class DistRpm(Dist): diff --git a/tests/tests_assemble_workflow/test_bundle_opensearch.py b/tests/tests_assemble_workflow/test_bundle_opensearch.py index 814bce4d2b..8193dbe1c3 100644 --- a/tests/tests_assemble_workflow/test_bundle_opensearch.py +++ b/tests/tests_assemble_workflow/test_bundle_opensearch.py @@ -250,5 +250,5 @@ def test_bundle_package_zip(self) -> None: with patch("shutil.copyfile") as mock_copyfile: bundle.package(os.path.dirname(__file__)) mock_zipfile_open.assert_called_with("opensearch.zip", "w", zipfile.ZIP_DEFLATED) - mock_zipfile_write.assert_called_with(os.path.join(bundle.tmp_dir.name, "opensearch-1.3.0", "opensearch.txt"), "opensearch.txt") + mock_zipfile_write.assert_called_with(os.path.join(bundle.tmp_dir.name, "opensearch-1.3.0", "opensearch.txt"), os.path.join("opensearch-1.3.0", "opensearch.txt")) self.assertEqual(mock_copyfile.call_count, 1)