Skip to content

Commit a8da6cc

Browse files
committed
FIX: Add unlink() with missing_ok for Python 3.7
1 parent deb3df1 commit a8da6cc

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

niworkflows/interfaces/bids.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from templateflow.api import templates as _get_template_list
5151
from ..utils.bids import _init_layout, relative_to_root
5252
from ..utils.images import set_consumables, unsafe_write_nifti_header_and_data
53-
from ..utils.misc import splitext as _splitext, _copy_any
53+
from ..utils.misc import splitext as _splitext, _copy_any, unlink
5454

5555
regz = re.compile(r"\.gz$")
5656
_pybids_spec = loads(Path(_pkgres("niworkflows", "data/nipreps.json")).read_text())
@@ -669,7 +669,7 @@ def _run_interface(self, runtime):
669669
new_header.set_data_dtype(data_dtype)
670670
del nii
671671

672-
out_file.unlink(missing_ok=True)
672+
unlink(out_file, missing_ok=True)
673673
if new_data is new_header is None:
674674
_copy_any(orig_file, str(out_file))
675675
else:
@@ -710,11 +710,11 @@ def _run_interface(self, runtime):
710710
legacy_metadata[key] = self._metadata.pop(key)
711711
if legacy_metadata:
712712
sidecar = out_file.parent / f"{_splitext(str(out_file))[0]}.json"
713-
sidecar.unlink(missing_ok=True)
713+
unlink(sidecar, missing_ok=True)
714714
sidecar.write_text(dumps(legacy_metadata, sort_keys=True, indent=2))
715715
# The future: the extension is the first . and everything after
716716
sidecar = out_file.parent / f"{out_file.name.split('.', 1)[0]}.json"
717-
sidecar.unlink(missing_ok=True)
717+
unlink(sidecar, missing_ok=True)
718718
sidecar.write_text(dumps(self._metadata, sort_keys=True, indent=2))
719719
self._results["out_meta"] = str(sidecar)
720720
return runtime

niworkflows/utils/misc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# https://www.nipreps.org/community/licensing/
2222
#
2323
"""Miscellaneous utilities."""
24+
import os
2425

2526

2627
__all__ = [
@@ -333,5 +334,15 @@ def check_valid_fs_license():
333334
return proc.returncode == 0 and "ERROR:" not in proc.stdout.decode()
334335

335336

337+
def unlink(pathlike, missing_ok=False):
338+
"""Backport of Path.unlink from Python 3.8+ with missing_ok keyword"""
339+
# PY37 hack; drop when python_requires >= 3.8
340+
try:
341+
os.unlink(pathlike)
342+
except FileNotFoundError:
343+
if not missing_ok:
344+
raise
345+
346+
336347
if __name__ == "__main__":
337348
pass

0 commit comments

Comments
 (0)