|
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +# |
| 4 | +# Copyright The NiPreps Developers <[email protected]> |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# We support and encourage derived works from this project, please read |
| 19 | +# about our expectations at |
| 20 | +# |
| 21 | +# https://www.nipreps.org/community/licensing/ |
| 22 | +# |
| 23 | +"""Utilities and mocks for testing and documentation building.""" |
| 24 | + |
| 25 | +import os |
| 26 | +import shutil |
| 27 | +from contextlib import contextmanager |
| 28 | +from pathlib import Path |
| 29 | +from tempfile import mkdtemp |
| 30 | + |
| 31 | +from toml import loads |
| 32 | + |
| 33 | +from nibabies import data |
| 34 | +from nibabies.workflows.base import init_execution_spaces |
| 35 | + |
| 36 | + |
| 37 | +@contextmanager |
| 38 | +def mock_config(bids_dir=None): |
| 39 | + """Create a mock config for documentation and testing purposes.""" |
| 40 | + from ... import config |
| 41 | + |
| 42 | + _old_fs = os.getenv('FREESURFER_HOME') |
| 43 | + if not _old_fs: |
| 44 | + os.environ['FREESURFER_HOME'] = mkdtemp() |
| 45 | + |
| 46 | + settings = loads(data.load.readable('tests/config.toml').read_text()) |
| 47 | + for sectionname, configs in settings.items(): |
| 48 | + if sectionname != 'environment': |
| 49 | + section = getattr(config, sectionname) |
| 50 | + section.load(configs, init=False) |
| 51 | + config.nipype.omp_nthreads = 1 |
| 52 | + config.nipype.init() |
| 53 | + config.loggers.init() |
| 54 | + init_execution_spaces() |
| 55 | + |
| 56 | + bids_dir = bids_dir or data.load('tests/ds000005').absolute() |
| 57 | + |
| 58 | + config.execution.work_dir = Path(mkdtemp()) |
| 59 | + config.execution.bids_dir = bids_dir |
| 60 | + config.execution.nibabies_dir = Path(mkdtemp()) |
| 61 | + config.execution.bids_database_dir = None |
| 62 | + config.execution._layout = None |
| 63 | + config.execution.init() |
| 64 | + |
| 65 | + yield |
| 66 | + |
| 67 | + shutil.rmtree(config.execution.work_dir) |
| 68 | + shutil.rmtree(config.execution.nibabies_dir) |
| 69 | + |
| 70 | + if not _old_fs: |
| 71 | + del os.environ['FREESURFER_HOME'] |
0 commit comments