Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5d5b471

Browse files
committed
improve error handling and logging
1 parent71d0af0 commit5d5b471

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=portorreserve_port()
443+
self.base_dir=base_dir
443444
self.should_free_port=portisNone
444-
self.base_dir=base_dirortempfile.mkdtemp()
445445
self.should_rm_dirs=base_dirisNone
446446
self.use_logging=use_logging
447447
self.logger=None
448448

449-
# create directory if needed
450-
ifnotos.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
returnself
@@ -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+
ifnotself.base_dirornotos.path.exists(self.base_dir):
490+
self.base_dir=tempfile.mkdtemp()
491+
492+
ifnotos.path.exists(self.logs_dir):
493+
os.makedirs(self.logs_dir)
494+
489495
definit(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-
ifos.path.isfile(postgres_conf):
506-
raiseInitNodeException('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-
exceptExceptionase:
1129+
exceptExecUtilExceptionase:
11271130
raiseInitNodeException(_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-
ifnotos.listdir(cached_data_dir):
1152-
call_initdb(cached_data_dir)
1152+
# Initialize cached initdb
1153+
ifnotos.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
exceptExceptionase:
11581160
raiseInitNodeException(_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
withopen(logfile,"a")asfile_out:
1192-
# write util name
1193-
file_out.write(util)
1194-
# write args
1195-
forarginargs:
1196-
file_out.write(arg)
1197-
withopen(logfile,"ab")asfile_out:
1198-
# write output
1199-
ifout:
1194+
# write util name and args
1195+
file_out.write(' '.join([util]+args))
1196+
file_out.write('\n')
1197+
ifout:
1198+
withopen(logfile,"ab")asfile_out:
1199+
# write output
12001200
file_out.write(out)
12011201
exceptIOError:
12021202
pass

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp