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

Skip to content

Commit 3e9cad1

Browse files
node_base.py is updated
Names from 'utils' module are used explicitly.
1 parent 9e1979f commit 3e9cad1

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

testgres/node_base.py

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,6 @@
8181

8282
from . import utils
8383

84-
from .utils import \
85-
PgVer, \
86-
eprint, \
87-
get_bin_path2, \
88-
get_pg_version2, \
89-
execute_utility2, \
90-
options_string, \
91-
clean_on_error
92-
9384
from .backup import NodeBackup
9485

9586
from .operations.os_ops import OsOperations
@@ -143,7 +134,7 @@ def __init__(self, os_ops: OsOperations, name=None, port=None, bin_dir=None):
143134
self._os_ops = os_ops
144135

145136
# private
146-
self._pg_version = PgVer(get_pg_version2(os_ops, bin_dir))
137+
self._pg_version = utils.PgVer(utils.get_pg_version2(os_ops, bin_dir))
147138
self._should_free_port = port is None
148139
self._bin_dir = bin_dir
149140
self._logger = None
@@ -202,7 +193,7 @@ def pid(self):
202193
"status"
203194
] # yapf: disable
204195

205-
status_code, out, error = execute_utility2(
196+
status_code, out, error = utils.execute_utility2(
206197
self._os_ops,
207198
_params,
208199
self.utils_log_file,
@@ -404,7 +395,7 @@ def master(self):
404395
@property
405396
def bin_dir(self):
406397
if not self._bin_dir:
407-
self._bin_dir = os.path.dirname(get_bin_path2(self._os_ops, "pg_config"))
398+
self._bin_dir = os.path.dirname(utils.get_bin_path2(self._os_ops, "pg_config"))
408399
return self._bin_dir
409400

410401
@property
@@ -448,7 +439,7 @@ def _try_shutdown(self, max_attempts, with_force=False):
448439
except ExecUtilException:
449440
continue # one more time
450441
except Exception:
451-
eprint('cannot stop node {}'.format(self.name))
442+
utils.eprint('cannot stop node {}'.format(self.name))
452443
break
453444

454445
return # OK
@@ -480,7 +471,7 @@ def _try_shutdown(self, max_attempts, with_force=False):
480471
ps_command)
481472

482473
try:
483-
eprint('Force stopping node {0} with PID {1}'.format(self.name, node_pid))
474+
utils.eprint('Force stopping node {0} with PID {1}'.format(self.name, node_pid))
484475
self._os_ops.kill(node_pid, signal.SIGKILL, expect_error=False)
485476
except Exception:
486477
# The node has already stopped
@@ -491,11 +482,11 @@ def _try_shutdown(self, max_attempts, with_force=False):
491482
assert type(ps_output) == str # noqa: E721
492483

493484
if ps_output == "":
494-
eprint('Node {0} has been stopped successfully.'.format(self.name))
485+
utils.eprint('Node {0} has been stopped successfully.'.format(self.name))
495486
return
496487

497488
if ps_output == str(node_pid):
498-
eprint('Failed to stop node {0}.'.format(self.name))
489+
utils.eprint('Failed to stop node {0}.'.format(self.name))
499490
return
500491

501492
__class__._throw_bugcheck__unexpected_result_of_ps(
@@ -542,9 +533,9 @@ def _create_recovery_conf(self, username, slot=None):
542533

543534
line = (
544535
"primary_conninfo='{}'\n"
545-
).format(options_string(**conninfo)) # yapf: disable
536+
).format(utils.options_string(**conninfo)) # yapf: disable
546537
# Since 12 recovery.conf had disappeared
547-
if self.version >= PgVer('12'):
538+
if self.version >= utils.PgVer('12'):
548539
signal_name = os.path.join(self.data_dir, "standby.signal")
549540
self._os_ops.touch(signal_name)
550541
else:
@@ -574,7 +565,7 @@ def _create_recovery_conf(self, username, slot=None):
574565

575566
line += "primary_slot_name={}\n".format(slot)
576567

577-
if self.version >= PgVer('12'):
568+
if self.version >= utils.PgVer('12'):
578569
self.append_conf(line=line)
579570
else:
580571
self.append_conf(filename=RECOVERY_CONF_FILE, line=line)
@@ -741,9 +732,9 @@ def get_auth_method(t):
741732
# binary replication
742733
if allow_streaming:
743734
# select a proper wal_level for PostgreSQL
744-
wal_level = 'replica' if self._pg_version >= PgVer('9.6') else 'hot_standby'
735+
wal_level = 'replica' if self._pg_version >= utils.PgVer('9.6') else 'hot_standby'
745736

746-
if self._pg_version < PgVer('13'):
737+
if self._pg_version < utils.PgVer('13'):
747738
self.append_conf(hot_standby=True,
748739
wal_keep_segments=WAL_KEEP_SEGMENTS,
749740
wal_level=wal_level) # yapf: disable
@@ -754,7 +745,7 @@ def get_auth_method(t):
754745

755746
# logical replication
756747
if allow_logical:
757-
if self._pg_version < PgVer('10'):
748+
if self._pg_version < utils.PgVer('10'):
758749
raise InitNodeException("Logical replication is only "
759750
"available on PostgreSQL 10 and newer")
760751

@@ -823,7 +814,7 @@ def status(self):
823814
"-D", self.data_dir,
824815
"status"
825816
] # yapf: disable
826-
status_code, out, error = execute_utility2(self._os_ops, _params, self.utils_log_file, verbose=True)
817+
status_code, out, error = utils.execute_utility2(self._os_ops, _params, self.utils_log_file, verbose=True)
827818
if error and 'does not exist' in error:
828819
return NodeStatus.Uninitialized
829820
elif 'no server running' in out:
@@ -846,10 +837,10 @@ def get_control_data(self):
846837

847838
# this one is tricky (blame PG 9.4)
848839
_params = [self._get_bin_path("pg_controldata")]
849-
_params += ["-D"] if self._pg_version >= PgVer('9.5') else []
840+
_params += ["-D"] if self._pg_version >= utils.PgVer('9.5') else []
850841
_params += [self.data_dir]
851842

852-
data = execute_utility2(self._os_ops, _params, self.utils_log_file)
843+
data = utils.execute_utility2(self._os_ops, _params, self.utils_log_file)
853844

854845
out_dict = {}
855846

@@ -932,7 +923,7 @@ def start(self, params=[], wait=True):
932923

933924
def LOCAL__start_node():
934925
# 'error' will be None on Windows
935-
_, _, error = execute_utility2(self._os_ops, _params, self.utils_log_file, verbose=True)
926+
_, _, error = utils.execute_utility2(self._os_ops, _params, self.utils_log_file, verbose=True)
936927
assert error is None or type(error) == str # noqa: E721
937928
if error and 'does not exist' in error:
938929
raise Exception(error)
@@ -1021,7 +1012,7 @@ def stop(self, params=[], wait=True):
10211012
"stop"
10221013
] + params # yapf: disable
10231014

1024-
execute_utility2(self._os_ops, _params, self.utils_log_file)
1015+
utils.execute_utility2(self._os_ops, _params, self.utils_log_file)
10251016

10261017
self._maybe_stop_logger()
10271018
self.is_started = False
@@ -1063,7 +1054,7 @@ def restart(self, params=[]):
10631054
] + params # yapf: disable
10641055

10651056
try:
1066-
error_code, out, error = execute_utility2(self._os_ops, _params, self.utils_log_file, verbose=True)
1057+
error_code, out, error = utils.execute_utility2(self._os_ops, _params, self.utils_log_file, verbose=True)
10671058
if error and 'could not start server' in error:
10681059
raise ExecUtilException
10691060
except ExecUtilException as e:
@@ -1092,7 +1083,7 @@ def reload(self, params=[]):
10921083
"reload"
10931084
] + params # yapf: disable
10941085

1095-
execute_utility2(self._os_ops, _params, self.utils_log_file)
1086+
utils.execute_utility2(self._os_ops, _params, self.utils_log_file)
10961087

10971088
return self
10981089

@@ -1114,11 +1105,11 @@ def promote(self, dbname=None, username=None):
11141105
"promote"
11151106
] # yapf: disable
11161107

1117-
execute_utility2(self._os_ops, _params, self.utils_log_file)
1108+
utils.execute_utility2(self._os_ops, _params, self.utils_log_file)
11181109

11191110
# for versions below 10 `promote` is asynchronous so we need to wait
11201111
# until it actually becomes writable
1121-
if self._pg_version < PgVer('10'):
1112+
if self._pg_version < utils.PgVer('10'):
11221113
check_query = "SELECT pg_is_in_recovery()"
11231114

11241115
self.poll_query_until(query=check_query,
@@ -1149,7 +1140,7 @@ def pg_ctl(self, params):
11491140
"-w" # wait
11501141
] + params # yapf: disable
11511142

1152-
return execute_utility2(self._os_ops, _params, self.utils_log_file)
1143+
return utils.execute_utility2(self._os_ops, _params, self.utils_log_file)
11531144

11541145
def free_port(self):
11551146
"""
@@ -1365,7 +1356,7 @@ def tmpfile():
13651356
"-F", format.value
13661357
] # yapf: disable
13671358

1368-
execute_utility2(self._os_ops, _params, self.utils_log_file)
1359+
utils.execute_utility2(self._os_ops, _params, self.utils_log_file)
13691360

13701361
return filename
13711362

@@ -1394,7 +1385,7 @@ def restore(self, filename, dbname=None, username=None):
13941385

13951386
# try pg_restore if dump is binary format, and psql if not
13961387
try:
1397-
execute_utility2(self._os_ops, _params, self.utils_log_name)
1388+
utils.execute_utility2(self._os_ops, _params, self.utils_log_name)
13981389
except ExecUtilException:
13991390
self.psql(filename=filename, dbname=dbname, username=username)
14001391

@@ -1523,7 +1514,7 @@ def replicate(self, name=None, slot=None, **kwargs):
15231514
"""
15241515

15251516
# transform backup into a replica
1526-
with clean_on_error(self.backup(**kwargs)) as backup:
1517+
with utils.clean_on_error(self.backup(**kwargs)) as backup:
15271518
return backup.spawn_replica(name=name, destroy=True, slot=slot)
15281519

15291520
def set_synchronous_standbys(self, standbys):
@@ -1552,7 +1543,7 @@ def set_synchronous_standbys(self, standbys):
15521543
master.restart()
15531544
15541545
"""
1555-
if self._pg_version >= PgVer('9.6'):
1546+
if self._pg_version >= utils.PgVer('9.6'):
15561547
if isinstance(standbys, Iterable):
15571548
standbys = First(1, standbys)
15581549
else:
@@ -1573,7 +1564,7 @@ def catchup(self, dbname=None, username=None):
15731564
if not self.master:
15741565
raise TestgresException("Node doesn't have a master")
15751566

1576-
if self._pg_version >= PgVer('10'):
1567+
if self._pg_version >= utils.PgVer('10'):
15771568
poll_lsn = "select pg_catalog.pg_current_wal_lsn()::text"
15781569
wait_lsn = "select pg_catalog.pg_last_wal_replay_lsn() >= '{}'::pg_lsn"
15791570
else:
@@ -1747,7 +1738,7 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs):
17471738
# should be the last one
17481739
_params.append(dbname)
17491740

1750-
return execute_utility2(self._os_ops, _params, self.utils_log_file)
1741+
return utils.execute_utility2(self._os_ops, _params, self.utils_log_file)
17511742

17521743
def connect(self,
17531744
dbname=None,
@@ -1944,7 +1935,7 @@ def _get_bin_path(self, filename):
19441935
if self.bin_dir:
19451936
bin_path = os.path.join(self.bin_dir, filename)
19461937
else:
1947-
bin_path = get_bin_path2(self._os_ops, filename)
1938+
bin_path = utils.get_bin_path2(self._os_ops, filename)
19481939
return bin_path
19491940

19501941
def _escape_config_value(value):

0 commit comments

Comments
 (0)