Skip to content

Commit

Permalink
Fix zip distribution does not include leading directory (opensearch-p…
Browse files Browse the repository at this point in the history
…roject#2672)

* Fix zip distribution does not include leading directory

Signed-off-by: Peter Zhu <[email protected]>

* Resolve the path issues failed on Windows

Signed-off-by: Peter Zhu <[email protected]>

Signed-off-by: Peter Zhu <[email protected]>
  • Loading branch information
peterzhuamazon committed Sep 29, 2022
1 parent 4f24a27 commit c1f517a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/assemble_workflow/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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-<version+qualifier>
# leadingdir : opensearch-<version+qualifier>
# root no leading dir: /tmp/tmp********/
# This is to preserve the leading directory `opensearch-<version+qualifier>` 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):
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_assemble_workflow/test_bundle_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c1f517a

Please sign in to comment.