1515import errno
1616import atexit
1717from warnings import warn
18- from io import StringIO
1918from distutils .version import LooseVersion
2019import configparser
2120import numpy as np
3736
3837NUMPY_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]
4441workflow_level = INFO
4542utils_level = INFO
4643interface_level = INFO
4744log_to_file = false
48- log_directory = %s
45+ log_directory = {log_dir}
4946log_size = 16384000
5047log_rotate = 4
5148
5249[execution]
5350create_report = true
54- crashdump_dir = %s
51+ crashdump_dir = {crashdump_dir}
5552hash_method = timestamp
5653job_finished_timeout = 5
5754keep_inputs = false
7976
8077[check]
8178interval = 1209600
82- """ % ( homedir , os . getcwd ())
79+ """ . format
8380
8481
8582def 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