diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ecb6f6..6ba95b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch - Update session manager to pass toolchain role region to sts ### Fixes +- correct archive extraction when there is only one nested path (issue 749) ## v5.0.0 (2024-08-16) diff --git a/seedfarmer/mgmt/archive_support.py b/seedfarmer/mgmt/archive_support.py index a36a966..a48e288 100644 --- a/seedfarmer/mgmt/archive_support.py +++ b/seedfarmer/mgmt/archive_support.py @@ -72,12 +72,26 @@ def _download_archive(archive_url: str, secret_name: Optional[str]) -> Response: def _extract_archive(archive_name: str, extracted_dir_path: str) -> str: if archive_name.endswith(".tar.gz"): with tarfile.open(archive_name, "r:gz") as tar_file: - embedded_dir = os.path.commonprefix(tar_file.getnames()) - tar_file.extractall(extracted_dir_path) + all_members = tar_file.getmembers() + top_level_dirs = set(member.name.split("/")[0] for member in all_members) + if len(top_level_dirs) == 1: + embedded_dir = top_level_dirs.pop() + else: + embedded_dir = "" + members_to_extract = [m for m in all_members] + tar_file.extractall(extracted_dir_path, members=members_to_extract) else: with ZipFile(archive_name, "r") as zip_file: - embedded_dir = os.path.commonprefix(zip_file.namelist()) - zip_file.extractall(extracted_dir_path) + all_files = zip_file.namelist() + top_level_dirs = set(name.split("/")[0] for name in all_files if name.endswith("/")) + if len(top_level_dirs) == 1: + embedded_dir = top_level_dirs.pop() + else: + embedded_dir = "" + files_to_extract = [f for f in all_files] # ] + + for file in files_to_extract: + zip_file.extract(file, path=extracted_dir_path) return embedded_dir