Skip to content

Commit

Permalink
Try_except with warning
Browse files Browse the repository at this point in the history
  • Loading branch information
frheault committed Oct 9, 2023
1 parent 568730b commit 2bb608e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
9 changes: 7 additions & 2 deletions trx/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import logging
import tempfile
import sys

try:
import dipy
Expand All @@ -23,7 +24,11 @@ def get_trx_tmpdir():
else:
trx_tmp_dir = tempfile.gettempdir()

return tempfile.TemporaryDirectory(dir=trx_tmp_dir, prefix='trx_')
if sys.version_info[1] >= 10:
return tempfile.TemporaryDirectory(dir=trx_tmp_dir, prefix='trx_',
ignore_cleanup_errors=True)
else:
return tempfile.TemporaryDirectory(dir=trx_tmp_dir, prefix='trx_')


def load_sft_with_reference(filepath, reference=None,
Expand Down Expand Up @@ -72,7 +77,7 @@ def load(tractogram_filename, reference):
def save(tractogram_obj, tractogram_filename, bbox_valid_check=False):
if not dipy_available:
logging.error('Dipy library is missing, cannot use functions related '
'to the StatefulTractogram.')
'to the StatefulTractogram.')
return None
from dipy.io.stateful_tractogram import StatefulTractogram
from dipy.io.streamline import save_tractogram
Expand Down
24 changes: 5 additions & 19 deletions trx/trx_file_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1736,24 +1736,10 @@ def to_sft(self, resize=False):
def close(self) -> None:
"""Cleanup on-disk temporary folder and initialize an empty TrxFile"""
if self._uncompressed_folder_handle is not None:
# Problem on Windows that is solving only in Python above 3.10
# Step 1: Traverse the directory tree
for dirpath, dirnames, filenames in \
os.walk(self._uncompressed_folder_handle.name, topdown=False):

# Step 2 and 3: Identify and remove symlinks (for files)
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if os.path.islink(filepath):
os.unlink(filepath)

# Step 2 and 3: Identify and remove symlinks (for directories)
for dirname in dirnames:
dir_full_path = os.path.join(dirpath, dirname)
if os.path.islink(dir_full_path):
os.unlink(dir_full_path)

# Step 4: Remove the temporary directory
shutil.rmtree(self._uncompressed_folder_handle.name)
try:
self._uncompressed_folder_handle.cleanup()
except PermissionError:
logging.error("Windows PermissionError, temporary directory {}" +
"was not deleted!".format(self._uncompressed_folder_handle.name))
self.__init__()
logging.debug("Deleted memmaps and intialized empty TrxFile.")

0 comments on commit 2bb608e

Please sign in to comment.