From 29af330234fb9b88d978a2b8a3afa6a9221165e8 Mon Sep 17 00:00:00 2001 From: MarsMellow Date: Thu, 6 Jul 2023 11:14:18 +0200 Subject: [PATCH] Replace the `tarfile` and `zipfile` dependency with `shutil.unpack_archive` --- bidscoin/bcoin.py | 13 +++++++------ bidscoin/bids.py | 14 +++++--------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/bidscoin/bcoin.py b/bidscoin/bcoin.py index 961409b6..1be47fd4 100755 --- a/bidscoin/bcoin.py +++ b/bidscoin/bcoin.py @@ -21,7 +21,6 @@ import shutil import subprocess import sys -import tarfile import textwrap import urllib.request from functools import lru_cache @@ -635,12 +634,14 @@ def pulltutorialdata(tutorialfolder: str) -> None: urllib.request.urlretrieve(tutorialurl, tutorialtargz, reporthook=t.update_to) # NB: Much faster than requests.get(url, stream=True). In case of ssl certificate issues use: with urllib.request.urlopen(tutorialurl, context=ssl.SSLContext()) as data, open(tutorialtargz, 'wb') as targz_fid: shutil.copyfileobj(data, targz_fid) # Unzip the data in the target folder - LOGGER.info(f"Unzipping the downloaded data in: {tutorialfolder}") - with tarfile.open(tutorialtargz, 'r') as targz_fid: - for member in tqdm(iterable=targz_fid.getmembers(), total=len(targz_fid.getmembers()), leave=False): - targz_fid.extract(member, tutorialfolder) + LOGGER.info(f"Unpacking the downloaded data in: {tutorialfolder}") + try: + shutil.unpack_archive(tutorialtargz, tutorialfolder) + LOGGER.success(f"Done") + except Exception as unpackerror: + LOGGER.error(f"Could not unpack: {tutorialtargz}\n{unpackerror}") + tutorialtargz.unlink() - LOGGER.success(f"Done") def main(): diff --git a/bidscoin/bids.py b/bidscoin/bids.py index c785a9dc..72dee62f 100644 --- a/bidscoin/bids.py +++ b/bidscoin/bids.py @@ -13,10 +13,8 @@ import logging import re import shutil -import tarfile import tempfile import warnings -import zipfile import fnmatch from functools import lru_cache from pathlib import Path @@ -355,13 +353,11 @@ def unpack(sourcefolder: Path, wildcard: str='', workfolder: Path='') -> Tuple[L recursive = False for tarzipfile in [worksubses/tarzipfile.name for tarzipfile in tarzipfiles]: LOGGER.info(f"Unpacking: {tarzipfile.name} -> {worksubses}") - ext = tarzipfile.suffixes - if ext and ext[-1] == '.zip': - with zipfile.ZipFile(tarzipfile, 'r') as zip_fid: - zip_fid.extractall(worksubses) - elif '.tar' in ext: - with tarfile.open(tarzipfile, 'r') as tar_fid: - tar_fid.extractall(worksubses) + try: + shutil.unpack_archive(tarzipfile, worksubses) + except Exception as unpackerror: + LOGGER.warning(f"Could not unpack: {tarzipfile}\n{unpackerror}") + continue # Sort the DICOM files in the worksubses rootfolder immediately (to avoid name collisions) if not (worksubses/'DICOMDIR').is_file():