Skip to content

Commit 5d5b471

Browse files
committed
improve error handling and logging
1 parent 71d0af0 commit 5d5b471

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

testgres/testgres.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,14 @@ def __init__(self,
440440
self.name = name
441441
self.host = '127.0.0.1'
442442
self.port = port or reserve_port()
443+
self.base_dir = base_dir
443444
self.should_free_port = port is None
444-
self.base_dir = base_dir or tempfile.mkdtemp()
445445
self.should_rm_dirs = base_dir is None
446446
self.use_logging = use_logging
447447
self.logger = None
448448

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()
452451

453452
def __enter__(self):
454453
return self
@@ -486,6 +485,13 @@ def _create_recovery_conf(self, root_node):
486485

487486
self.append_conf("recovery.conf", line)
488487

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+
489495
def init(self, allow_streaming=False, fsync=False, initdb_params=[]):
490496
"""
491497
Perform initdb for this node.
@@ -499,11 +505,8 @@ def init(self, allow_streaming=False, fsync=False, initdb_params=[]):
499505
This instance of PostgresNode.
500506
"""
501507

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()
507510

508511
# initialize this PostgreSQL node
509512
initdb_log = os.path.join(self.logs_dir, "initdb.log")
@@ -1123,7 +1126,7 @@ def call_initdb(_data_dir):
11231126
try:
11241127
_params = [_data_dir, "-N"] + initdb_params
11251128
_execute_utility("initdb", _params, initdb_logfile)
1126-
except Exception as e:
1129+
except ExecUtilException as e:
11271130
raise InitNodeException(_explain_exception(e))
11281131

11291132
# Call initdb if we have custom params
@@ -1143,17 +1146,16 @@ def rm_cached_data_dir(rm_dir):
11431146
atexit.register(rm_cached_data_dir,
11441147
TestgresConfig.cached_initdb_dir)
11451148

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
11491151

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)
11531155

1156+
try:
11541157
# Copy cached initdb to current data dir
11551158
shutil.copytree(cached_data_dir, data_dir)
1156-
11571159
except Exception as e:
11581160
raise InitNodeException(_explain_exception(e))
11591161

@@ -1189,14 +1191,12 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
11891191
# write new log entry if possible
11901192
try:
11911193
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
12001200
file_out.write(out)
12011201
except IOError:
12021202
pass

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy