From cd482758c57757c4d9316e15890a2140ea5cc29e Mon Sep 17 00:00:00 2001 From: SiQube Date: Sun, 25 Aug 2024 00:09:44 +0200 Subject: [PATCH] serial extract_archive to prevent unnecessary extractions --- src/pymovements/utils/archives.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/pymovements/utils/archives.py b/src/pymovements/utils/archives.py index a8652a494..fae764dcc 100644 --- a/src/pymovements/utils/archives.py +++ b/src/pymovements/utils/archives.py @@ -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( @@ -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]] = {