27
27
Copyright (c) 2016, Postgres Professional
28
28
"""
29
29
30
+ import atexit
31
+ import logging
30
32
import os
31
- import subprocess
32
33
import pwd
34
+ import select
33
35
import shutil
34
- import time
35
36
import six
36
- import port_for
37
-
38
- import threading
39
- import logging
40
- import select
37
+ import subprocess
41
38
import tempfile
39
+ import threading
40
+ import time
41
+
42
+ import port_for
42
43
43
44
from enum import Enum
44
45
from distutils .version import LooseVersion
45
46
47
+
46
48
# Try to use psycopg2 by default. If psycopg2 isn't available then use
47
49
# pg8000 which is slower but much more portable because uses only
48
50
# pure-Python code
60
62
# threads for loggers
61
63
util_threads = []
62
64
63
- # chached initdb dir
64
- cached_data_dir = None
65
-
66
65
# rows returned by PG_CONFIG
67
66
pg_config_data = {}
68
67
@@ -80,9 +79,18 @@ class TestgresConfig:
80
79
Global config (override default settings)
81
80
"""
82
81
82
+ # shall we cache pg_config results?
83
83
cache_pg_config = True
84
+
85
+ # shall we use cached initdb instance?
84
86
cache_initdb = True
85
87
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
+
86
94
87
95
class TestgresException (Exception ):
88
96
"""
@@ -427,7 +435,7 @@ def __init__(self,
427
435
self .port = port or reserve_port ()
428
436
self .should_free_port = port is None
429
437
self .base_dir = base_dir or tempfile .mkdtemp ()
430
- self .should_rm_base_dir = base_dir is None
438
+ self .should_rm_dirs = base_dir is None
431
439
self .use_logging = use_logging
432
440
self .logger = None
433
441
@@ -741,8 +749,6 @@ def reload(self, params=[]):
741
749
_params = ["reload" , "-D" , self .data_dir , "-w" ] + params
742
750
_execute_utility ("pg_ctl" , _params , self .utils_logname )
743
751
744
- return self
745
-
746
752
def pg_ctl (self , params ):
747
753
"""
748
754
Invoke pg_ctl with params.
@@ -784,9 +790,16 @@ def cleanup(self, max_attempts=3):
784
790
785
791
attempts += 1
786
792
787
- # remove data directory if necessary
788
- if self .should_rm_base_dir :
789
- shutil .rmtree (self .data_dir , ignore_errors = True )
793
+ # remove directory tree if necessary
794
+ if self .should_rm_dirs :
795
+
796
+ # choose directory to be removed
797
+ if TestgresConfig .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 )
790
803
791
804
return self
792
805
@@ -1101,16 +1114,29 @@ def call_initdb(_data_dir):
1101
1114
call_initdb (data_dir )
1102
1115
# Else we can use cached dir
1103
1116
else :
1104
- global cached_data_dir
1117
+ # Set default temp dir for cached initdb
1118
+ if TestgresConfig .cached_initdb_dir is None :
1119
+ def rm_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 ()
1105
1124
1106
- # Initialize cached initdb
1107
- if cached_data_dir is None :
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 )
1110
1128
1111
1129
try :
1130
+ # Fetch cached initdb dir
1131
+ cached_data_dir = TestgresConfig .cached_initdb_dir
1132
+
1133
+ # Initialize cached initdb
1134
+ if not os .listdir (cached_data_dir ):
1135
+ call_initdb (cached_data_dir )
1136
+
1112
1137
# Copy cached initdb to current data dir
1113
1138
shutil .copytree (cached_data_dir , data_dir )
1139
+
1114
1140
except Exception as e :
1115
1141
raise InitNodeException (str (e ))
1116
1142
0 commit comments