@@ -440,15 +440,14 @@ def __init__(self,
440
440
self .name = name
441
441
self .host = '127.0.0.1'
442
442
self .port = port or reserve_port ()
443
+ self .base_dir = base_dir
443
444
self .should_free_port = port is None
444
- self .base_dir = base_dir or tempfile .mkdtemp ()
445
445
self .should_rm_dirs = base_dir is None
446
446
self .use_logging = use_logging
447
447
self .logger = None
448
448
449
- # create directory if needed
450
- if not os .path .exists (self .logs_dir ):
451
- os .makedirs (self .logs_dir )
449
+ # create directories if needed
450
+ self ._prepare_dirs ()
452
451
453
452
def __enter__ (self ):
454
453
return self
@@ -486,6 +485,13 @@ def _create_recovery_conf(self, root_node):
486
485
487
486
self .append_conf ("recovery.conf" , line )
488
487
488
+ def _prepare_dirs (self ):
489
+ if not self .base_dir or not os .path .exists (self .base_dir ):
490
+ self .base_dir = tempfile .mkdtemp ()
491
+
492
+ if not os .path .exists (self .logs_dir ):
493
+ os .makedirs (self .logs_dir )
494
+
489
495
def init (self , allow_streaming = False , fsync = False , initdb_params = []):
490
496
"""
491
497
Perform initdb for this node.
@@ -499,11 +505,8 @@ def init(self, allow_streaming=False, fsync=False, initdb_params=[]):
499
505
This instance of PostgresNode.
500
506
"""
501
507
502
- postgres_conf = os .path .join (self .data_dir , "postgresql.conf" )
503
-
504
- # We don't have to reinit it if data directory exists
505
- if os .path .isfile (postgres_conf ):
506
- raise InitNodeException ('Node is already intialized' )
508
+ # create directories if needed
509
+ self ._prepare_dirs ()
507
510
508
511
# initialize this PostgreSQL node
509
512
initdb_log = os .path .join (self .logs_dir , "initdb.log" )
@@ -1123,7 +1126,7 @@ def call_initdb(_data_dir):
1123
1126
try :
1124
1127
_params = [_data_dir , "-N" ] + initdb_params
1125
1128
_execute_utility ("initdb" , _params , initdb_logfile )
1126
- except Exception as e :
1129
+ except ExecUtilException as e :
1127
1130
raise InitNodeException (_explain_exception (e ))
1128
1131
1129
1132
# Call initdb if we have custom params
@@ -1143,17 +1146,16 @@ def rm_cached_data_dir(rm_dir):
1143
1146
atexit .register (rm_cached_data_dir ,
1144
1147
TestgresConfig .cached_initdb_dir )
1145
1148
1146
- try :
1147
- # Fetch cached initdb dir
1148
- cached_data_dir = TestgresConfig .cached_initdb_dir
1149
+ # Fetch cached initdb dir
1150
+ cached_data_dir = TestgresConfig .cached_initdb_dir
1149
1151
1150
- # Initialize cached initdb
1151
- if not os .listdir (cached_data_dir ):
1152
- call_initdb (cached_data_dir )
1152
+ # Initialize cached initdb
1153
+ if not os .listdir (cached_data_dir ):
1154
+ call_initdb (cached_data_dir )
1153
1155
1156
+ try :
1154
1157
# Copy cached initdb to current data dir
1155
1158
shutil .copytree (cached_data_dir , data_dir )
1156
-
1157
1159
except Exception as e :
1158
1160
raise InitNodeException (_explain_exception (e ))
1159
1161
@@ -1189,14 +1191,12 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
1189
1191
# write new log entry if possible
1190
1192
try :
1191
1193
with open (logfile , "a" ) as file_out :
1192
- # write util name
1193
- file_out .write (util )
1194
- # write args
1195
- for arg in args :
1196
- file_out .write (arg )
1197
- with open (logfile , "ab" ) as file_out :
1198
- # write output
1199
- if out :
1194
+ # write util name and args
1195
+ file_out .write (' ' .join ([util ] + args ))
1196
+ file_out .write ('\n ' )
1197
+ if out :
1198
+ with open (logfile , "ab" ) as file_out :
1199
+ # write output
1200
1200
file_out .write (out )
1201
1201
except IOError :
1202
1202
pass
0 commit comments