Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/maint/1.4.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Mar 17, 2022
2 parents 47d6034 + 03d6328 commit 3639077
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions niworkflows/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from templateflow.api import templates as _get_template_list
from ..utils.bids import _init_layout, relative_to_root
from ..utils.images import set_consumables, unsafe_write_nifti_header_and_data
from ..utils.misc import splitext as _splitext, _copy_any
from ..utils.misc import splitext as _splitext, _copy_any, unlink

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

out_file.unlink(missing_ok=True)
unlink(out_file, missing_ok=True)
if new_data is new_header is None:
_copy_any(orig_file, str(out_file))
else:
Expand Down Expand Up @@ -710,11 +710,11 @@ def _run_interface(self, runtime):
legacy_metadata[key] = self._metadata.pop(key)
if legacy_metadata:
sidecar = out_file.parent / f"{_splitext(str(out_file))[0]}.json"
sidecar.unlink(missing_ok=True)
unlink(sidecar, missing_ok=True)
sidecar.write_text(dumps(legacy_metadata, sort_keys=True, indent=2))
# The future: the extension is the first . and everything after
sidecar = out_file.parent / f"{out_file.name.split('.', 1)[0]}.json"
sidecar.unlink(missing_ok=True)
unlink(sidecar, missing_ok=True)
sidecar.write_text(dumps(self._metadata, sort_keys=True, indent=2))
self._results["out_meta"] = str(sidecar)
return runtime
Expand Down
11 changes: 11 additions & 0 deletions niworkflows/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# https://www.nipreps.org/community/licensing/
#
"""Miscellaneous utilities."""
import os


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


def unlink(pathlike, missing_ok=False):
"""Backport of Path.unlink from Python 3.8+ with missing_ok keyword"""
# PY37 hack; drop when python_requires >= 3.8
try:
os.unlink(pathlike)
except FileNotFoundError:
if not missing_ok:
raise


if __name__ == "__main__":
pass

0 comments on commit 3639077

Please sign in to comment.