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

Commitbd428b0

Browse files
committed
remove cached initdb dir by default
1 parentb903f44 commitbd428b0

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

‎testgres/testgres.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@
2727
Copyright (c) 2016, Postgres Professional
2828
"""
2929

30+
importatexit
31+
importlogging
3032
importos
31-
importsubprocess
3233
importpwd
34+
importselect
3335
importshutil
34-
importtime
3536
importsix
36-
importport_for
37-
38-
importthreading
39-
importlogging
40-
importselect
37+
importsubprocess
4138
importtempfile
39+
importthreading
40+
importtime
41+
42+
importport_for
4243

4344
fromenumimportEnum
4445
fromdistutils.versionimportLooseVersion
4546

47+
4648
# Try to use psycopg2 by default. If psycopg2 isn't available then use
4749
# pg8000 which is slower but much more portable because uses only
4850
# pure-Python code
@@ -60,9 +62,6 @@
6062
# threads for loggers
6163
util_threads= []
6264

63-
# chached initdb dir
64-
cached_data_dir=None
65-
6665
# rows returned by PG_CONFIG
6766
pg_config_data= {}
6867

@@ -80,9 +79,18 @@ class TestgresConfig:
8079
Global config (override default settings)
8180
"""
8281

82+
# shall we cache pg_config results?
8383
cache_pg_config=True
84+
85+
# shall we use cached initdb instance?
8486
cache_initdb=True
8587

88+
# shall we create a temp dir for cached initdb?
89+
cached_initdb_dir=None
90+
91+
# shall we remove EVERYTHING (including logs)?
92+
node_cleanup_full=False
93+
8694

8795
classTestgresException(Exception):
8896
"""
@@ -427,7 +435,7 @@ def __init__(self,
427435
self.port=portorreserve_port()
428436
self.should_free_port=portisNone
429437
self.base_dir=base_dirortempfile.mkdtemp()
430-
self.should_rm_base_dir=base_dirisNone
438+
self.should_rm_dirs=base_dirisNone
431439
self.use_logging=use_logging
432440
self.logger=None
433441

@@ -741,8 +749,6 @@ def reload(self, params=[]):
741749
_params= ["reload","-D",self.data_dir,"-w"]+params
742750
_execute_utility("pg_ctl",_params,self.utils_logname)
743751

744-
returnself
745-
746752
defpg_ctl(self,params):
747753
"""
748754
Invoke pg_ctl with params.
@@ -784,9 +790,16 @@ def cleanup(self, max_attempts=3):
784790

785791
attempts+=1
786792

787-
# remove data directory if necessary
788-
ifself.should_rm_base_dir:
789-
shutil.rmtree(self.data_dir,ignore_errors=True)
793+
# remove directory tree if necessary
794+
ifself.should_rm_dirs:
795+
796+
# choose directory to be removed
797+
ifTestgresConfig.node_cleanup_full:
798+
rm_dir=self.base_dir# everything
799+
else:
800+
rm_dir=self.data_dir# just data, save logs
801+
802+
shutil.rmtree(rm_dir,ignore_errors=True)
790803

791804
returnself
792805

@@ -1101,16 +1114,29 @@ def call_initdb(_data_dir):
11011114
call_initdb(data_dir)
11021115
# Else we can use cached dir
11031116
else:
1104-
globalcached_data_dir
1117+
# Set default temp dir for cached initdb
1118+
ifTestgresConfig.cached_initdb_dirisNone:
1119+
defrm_cached_data_dir(rm_dir):
1120+
shutil.rmtree(rm_dir,ignore_errors=True)
1121+
1122+
# Create default temp dir
1123+
TestgresConfig.cached_initdb_dir=tempfile.mkdtemp()
11051124

1106-
# Initialize cached initdb
1107-
ifcached_data_dirisNone:
1108-
cached_data_dir=tempfile.mkdtemp()
1109-
call_initdb(cached_data_dir)
1125+
# Schedule cleanup
1126+
atexit.register(rm_cached_data_dir,
1127+
TestgresConfig.cached_initdb_dir)
11101128

11111129
try:
1130+
# Fetch cached initdb dir
1131+
cached_data_dir=TestgresConfig.cached_initdb_dir
1132+
1133+
# Initialize cached initdb
1134+
ifnotos.listdir(cached_data_dir):
1135+
call_initdb(cached_data_dir)
1136+
11121137
# Copy cached initdb to current data dir
11131138
shutil.copytree(cached_data_dir,data_dir)
1139+
11141140
exceptExceptionase:
11151141
raiseInitNodeException(str(e))
11161142

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp