Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0459830

Browse files
committed
add some prefixes for mkdtemp()
1 parent d800f48 commit 0459830

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

testgres/backup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from .consts import \
1010
DATA_DIR, \
11+
TMP_NODE, \
12+
TMP_BACKUP, \
1113
PG_CONF_FILE, \
1214
BACKUP_LOG_FILE, \
1315
DEFAULT_XLOG_METHOD
@@ -49,7 +51,7 @@ def __init__(self,
4951

5052
# Set default arguments
5153
username = username or default_username()
52-
base_dir = base_dir or tempfile.mkdtemp()
54+
base_dir = base_dir or tempfile.mkdtemp(prefix=TMP_BACKUP)
5355

5456
# public
5557
self.original_node = node
@@ -96,7 +98,7 @@ def _prepare_dir(self, destroy):
9698
available = not destroy
9799

98100
if available:
99-
dest_base_dir = tempfile.mkdtemp()
101+
dest_base_dir = tempfile.mkdtemp(prefix=TMP_NODE)
100102

101103
data1 = os.path.join(self.base_dir, DATA_DIR)
102104
data2 = os.path.join(dest_base_dir, DATA_DIR)

testgres/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from contextlib import contextmanager
99

10+
from .consts import TMP_CACHE
11+
1012

1113
class GlobalConfig(object):
1214
"""
@@ -101,6 +103,7 @@ def copy(self):
101103
config_stack = []
102104

103105

106+
@atexit.register
104107
def rm_cached_initdb_dirs():
105108
for d in cached_initdb_dirs:
106109
shutil.rmtree(d, ignore_errors=True)
@@ -161,8 +164,5 @@ def configure_testgres(**options):
161164
testgres_config.update(options)
162165

163166

164-
# NOTE: to be executed at exit()
165-
atexit.register(rm_cached_initdb_dirs)
166-
167167
# NOTE: assign initial cached dir for initdb
168-
testgres_config.cached_initdb_dir = tempfile.mkdtemp()
168+
testgres_config.cached_initdb_dir = tempfile.mkdtemp(prefix=TMP_CACHE)

testgres/consts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
DATA_DIR = "data"
55
LOGS_DIR = "logs"
66

7+
# prefixes for temp dirs
8+
TMP_NODE = 'tgsn_'
9+
TMP_CACHE = 'tgsc_'
10+
TMP_BACKUP = 'tgsb_'
11+
712
# names for config files
813
RECOVERY_CONF_FILE = "recovery.conf"
914
PG_AUTO_CONF_FILE = "postgresql.auto.conf"

testgres/node.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .consts import \
2424
DATA_DIR, \
2525
LOGS_DIR, \
26+
TMP_NODE, \
2627
PG_CONF_FILE, \
2728
PG_AUTO_CONF_FILE, \
2829
HBA_CONF_FILE, \
@@ -106,7 +107,9 @@ def __enter__(self):
106107
def __exit__(self, type, value, traceback):
107108
self.free_port()
108109

109-
got_exception = value is not None
110+
# NOTE: ctrl+C does not count!
111+
got_exception = type is not None and type != KeyboardInterrupt
112+
110113
c1 = self.cleanup_on_good_exit and not got_exception
111114
c2 = self.cleanup_on_bad_exit and got_exception
112115

@@ -152,8 +155,9 @@ def _try_shutdown(self, max_attempts):
152155
except ExecUtilException:
153156
pass # one more time
154157
except Exception:
155-
# TODO: probably kill stray instance
158+
# TODO: probably should kill stray instance
156159
eprint('cannot stop node {}'.format(self.name))
160+
break
157161

158162
attempts += 1
159163

@@ -195,7 +199,7 @@ def _create_recovery_conf(self, username):
195199

196200
def _prepare_dirs(self):
197201
if not self.base_dir:
198-
self.base_dir = tempfile.mkdtemp()
202+
self.base_dir = tempfile.mkdtemp(prefix=TMP_NODE)
199203

200204
if not os.path.exists(self.base_dir):
201205
os.makedirs(self.base_dir)

0 commit comments

Comments
 (0)