Skip to content

Commit

Permalink
limit extraction dir to one on archives
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraeber committed Dec 4, 2024
1 parent 62214de commit a304c34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 18 additions & 4 deletions seedfarmer/mgmt/archive_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a304c34

Please sign in to comment.