Skip to content

Commit a1c780b

Browse files
committed
cache cwd in config object, handle os.getcwd errors
1 parent 77e5257 commit a1c780b

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

nipype/utils/config.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import errno
1616
import atexit
1717
from warnings import warn
18-
from io import StringIO
1918
from distutils.version import LooseVersion
2019
import configparser
2120
import numpy as np
@@ -37,21 +36,19 @@
3736

3837
NUMPY_MMAP = LooseVersion(np.__version__) >= LooseVersion('1.12.0')
3938

40-
# Get home directory in platform-agnostic way
41-
homedir = os.path.expanduser('~')
42-
default_cfg = """
39+
DEFAULT_CONFIG_TPL = """\
4340
[logging]
4441
workflow_level = INFO
4542
utils_level = INFO
4643
interface_level = INFO
4744
log_to_file = false
48-
log_directory = %s
45+
log_directory = {log_dir}
4946
log_size = 16384000
5047
log_rotate = 4
5148
5249
[execution]
5350
create_report = true
54-
crashdump_dir = %s
51+
crashdump_dir = {crashdump_dir}
5552
hash_method = timestamp
5653
job_finished_timeout = 5
5754
keep_inputs = false
@@ -79,7 +76,7 @@
7976
8077
[check]
8178
interval = 1209600
82-
""" % (homedir, os.getcwd())
79+
""".format
8380

8481

8582
def mkdir_p(path):
@@ -97,15 +94,17 @@ class NipypeConfig(object):
9794

9895
def __init__(self, *args, **kwargs):
9996
self._config = configparser.ConfigParser()
97+
self._cwd = None
98+
10099
config_dir = os.path.expanduser('~/.nipype')
101-
config_file = os.path.join(config_dir, 'nipype.cfg')
102100
self.data_file = os.path.join(config_dir, 'nipype.json')
103-
self._config.readfp(StringIO(default_cfg))
101+
102+
self.set_default_config()
104103
self._display = None
105104
self._resource_monitor = None
106105

107106
if os.path.exists(config_dir):
108-
self._config.read([config_file, 'nipype.cfg'])
107+
self._config.read([os.path.join(config_dir, 'nipype.cfg'), 'nipype.cfg'])
109108

110109
for option in CONFIG_DEPRECATIONS:
111110
for section in ['execution', 'logging', 'monitoring']:
@@ -115,8 +114,32 @@ def __init__(self, *args, **kwargs):
115114
# Warn implicit in get
116115
self.set(new_section, new_option, self.get(section, option))
117116

117+
@property
118+
def cwd(self):
119+
"""Cache current working directory ASAP"""
120+
# Run getcwd only once, preventing multiproc to finish
121+
# with error having changed to the wrong path
122+
if self._cwd is None:
123+
try:
124+
self._cwd = os.getcwd()
125+
except OSError:
126+
warn('Trying to run Nipype from a nonexistent directory "%s".',
127+
os.getenv('PWD', 'unknown'))
128+
raise
129+
return self._cwd
130+
118131
def set_default_config(self):
119-
self._config.readfp(StringIO(default_cfg))
132+
"""Read default settings template and set into config object"""
133+
default_cfg = DEFAULT_CONFIG_TPL(
134+
log_dir=os.path.expanduser('~'), # Get $HOME in a platform-agnostic way
135+
crashdump_dir=self.cwd # Read cached cwd
136+
)
137+
138+
try:
139+
self._config.read_string(default_cfg) # Python >= 3.2
140+
except AttributeError:
141+
from io import StringIO
142+
self._config.readfp(StringIO(default_cfg))
120143

121144
def enable_debug_mode(self):
122145
"""Enables debug configuration"""

0 commit comments

Comments
 (0)