15
15
import errno
16
16
import atexit
17
17
from warnings import warn
18
- from io import StringIO
19
18
from distutils .version import LooseVersion
20
19
import configparser
21
20
import numpy as np
37
36
38
37
NUMPY_MMAP = LooseVersion (np .__version__ ) >= LooseVersion ('1.12.0' )
39
38
40
- # Get home directory in platform-agnostic way
41
- homedir = os .path .expanduser ('~' )
42
- default_cfg = """
39
+ DEFAULT_CONFIG_TPL = """\
43
40
[logging]
44
41
workflow_level = INFO
45
42
utils_level = INFO
46
43
interface_level = INFO
47
44
log_to_file = false
48
- log_directory = %s
45
+ log_directory = {log_dir}
49
46
log_size = 16384000
50
47
log_rotate = 4
51
48
52
49
[execution]
53
50
create_report = true
54
- crashdump_dir = %s
51
+ crashdump_dir = {crashdump_dir}
55
52
hash_method = timestamp
56
53
job_finished_timeout = 5
57
54
keep_inputs = false
79
76
80
77
[check]
81
78
interval = 1209600
82
- """ % ( homedir , os . getcwd ())
79
+ """ . format
83
80
84
81
85
82
def mkdir_p (path ):
@@ -97,15 +94,17 @@ class NipypeConfig(object):
97
94
98
95
def __init__ (self , * args , ** kwargs ):
99
96
self ._config = configparser .ConfigParser ()
97
+ self ._cwd = None
98
+
100
99
config_dir = os .path .expanduser ('~/.nipype' )
101
- config_file = os .path .join (config_dir , 'nipype.cfg' )
102
100
self .data_file = os .path .join (config_dir , 'nipype.json' )
103
- self ._config .readfp (StringIO (default_cfg ))
101
+
102
+ self .set_default_config ()
104
103
self ._display = None
105
104
self ._resource_monitor = None
106
105
107
106
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' ])
109
108
110
109
for option in CONFIG_DEPRECATIONS :
111
110
for section in ['execution' , 'logging' , 'monitoring' ]:
@@ -115,8 +114,32 @@ def __init__(self, *args, **kwargs):
115
114
# Warn implicit in get
116
115
self .set (new_section , new_option , self .get (section , option ))
117
116
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
+
118
131
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 ))
120
143
121
144
def enable_debug_mode (self ):
122
145
"""Enables debug configuration"""
0 commit comments