Skip to content

Commit

Permalink
serial extract_archive to prevent unnecessary extractions
Browse files Browse the repository at this point in the history
  • Loading branch information
SiQube committed Sep 26, 2024
1 parent 956cabb commit cd48275
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/pymovements/utils/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,13 @@ def _extract_tar(
Compression filename suffix.
"""
with tarfile.open(source_path, f'r:{compression[1:]}' if compression else 'r') as archive:
if sys.version_info < (3, 12): # pragma: <3.12 cover
archive.extractall(destination_path)
else: # pragma: >=3.12 cover
archive.extractall(destination_path, filter='tar')
for member in archive.getnames():
if os.path.exists(os.path.join(destination_path, member)):
continue
if sys.version_info < (3, 12): # pragma: <3.12 cover
archive.extract(member, destination_path)
else: # pragma: >=3.12 cover
archive.extract(member, destination_path, filter='tar')


def _extract_zip(
Expand All @@ -175,7 +178,10 @@ def _extract_zip(
"""
compression_id = _ZIP_COMPRESSION_MAP[compression] if compression else zipfile.ZIP_STORED
with zipfile.ZipFile(source_path, 'r', compression=compression_id) as archive:
archive.extractall(destination_path)
for member in archive.namelist():
if os.path.exists(os.path.join(destination_path, member)):
continue
archive.extract(member, destination_path)


_ARCHIVE_EXTRACTORS: dict[str, Callable[[Path, Path, str | None], None]] = {
Expand Down

0 comments on commit cd48275

Please sign in to comment.