Skip to content

Commit

Permalink
Replace the tarfile and zipfile dependency with `shutil.unpack_ar…
Browse files Browse the repository at this point in the history
…chive`
  • Loading branch information
marcelzwiers committed Jul 6, 2023
1 parent 6ca7012 commit 29af330
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
13 changes: 7 additions & 6 deletions bidscoin/bcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import shutil
import subprocess
import sys
import tarfile
import textwrap
import urllib.request
from functools import lru_cache
Expand Down Expand Up @@ -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():
Expand Down
14 changes: 5 additions & 9 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 29af330

Please sign in to comment.