Skip to content

Commit 2b34236

Browse files
execute_utility2, get_bin_path2, get_pg_config2 are added
This the functions with explicit os_ops argument. testgres/utils.py - [add] def execute_utility2(os_ops: OsOperations, args, logfile=None, verbose=False) - [add] def get_bin_path2(os_ops: OsOperations, filename) - [add] def get_pg_config2(os_ops: OsOperations, pg_config_path): ATTENTION get_pg_config does not change tconf.os_ops now testgres/cache.py - cached_initdb - [add] make_utility_path - it is used for pg_resetwal, too.
1 parent de738f9 commit 2b34236

File tree

4 files changed

+77
-36
lines changed

4 files changed

+77
-36
lines changed

testgres/backup.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
from .exceptions import BackupException
1717

18+
from .operations.os_ops import OsOperations
19+
1820
from .utils import \
19-
get_bin_path, \
20-
execute_utility, \
21+
get_bin_path2, \
22+
execute_utility2, \
2123
clean_on_error
2224

2325

@@ -44,6 +46,9 @@ def __init__(self,
4446
username: database user name.
4547
xlog_method: none | fetch | stream (see docs)
4648
"""
49+
assert node.os_ops is not None
50+
assert isinstance(node.os_ops, OsOperations)
51+
4752
if not options:
4853
options = []
4954
self.os_ops = node.os_ops
@@ -73,15 +78,15 @@ def __init__(self,
7378
data_dir = os.path.join(self.base_dir, DATA_DIR)
7479

7580
_params = [
76-
get_bin_path("pg_basebackup"),
81+
get_bin_path2(self.os_ops, "pg_basebackup"),
7782
"-p", str(node.port),
7883
"-h", node.host,
7984
"-U", username,
8085
"-D", data_dir,
8186
"-X", xlog_method.value
8287
] # yapf: disable
8388
_params += options
84-
execute_utility(_params, self.log_file)
89+
execute_utility2(self.os_ops, _params, self.log_file)
8590

8691
def __enter__(self):
8792
return self

testgres/cache.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
ExecUtilException
1616

1717
from .utils import \
18-
get_bin_path, \
19-
execute_utility
18+
get_bin_path2, \
19+
execute_utility2
2020

2121
from .operations.local_ops import LocalOperations
2222
from .operations.os_ops import OsOperations
@@ -27,11 +27,23 @@ def cached_initdb(data_dir, logfile=None, params=None, os_ops: OsOperations = Lo
2727
Perform initdb or use cached node files.
2828
"""
2929

30+
assert os_ops is not None
31+
assert isinstance(os_ops, OsOperations)
32+
33+
def make_utility_path(name):
34+
assert name is not None
35+
assert type(name) == str
36+
37+
if bin_path:
38+
return os.path.join(bin_path, name)
39+
40+
return get_bin_path2(os_ops, name)
41+
3042
def call_initdb(initdb_dir, log=logfile):
3143
try:
32-
initdb_path = os.path.join(bin_path, 'initdb') if bin_path else get_bin_path("initdb")
44+
initdb_path = make_utility_path("initdb")
3345
_params = [initdb_path, "-D", initdb_dir, "-N"]
34-
execute_utility(_params + (params or []), log)
46+
execute_utility2(os_ops, _params + (params or []), log)
3547
except ExecUtilException as e:
3648
raise_from(InitNodeException("Failed to run initdb"), e)
3749

@@ -63,8 +75,8 @@ def call_initdb(initdb_dir, log=logfile):
6375
os_ops.write(pg_control, new_pg_control, truncate=True, binary=True, read_and_write=True)
6476

6577
# XXX: build new WAL segment with our system id
66-
_params = [get_bin_path("pg_resetwal"), "-D", data_dir, "-f"]
67-
execute_utility(_params, logfile)
78+
_params = [make_utility_path("pg_resetwal"), "-D", data_dir, "-f"]
79+
execute_utility2(os_ops, _params, logfile)
6880

6981
except ExecUtilException as e:
7082
msg = "Failed to reset WAL for system id"

testgres/node.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@
8989
from .utils import \
9090
PgVer, \
9191
eprint, \
92-
get_bin_path, \
92+
get_bin_path2, \
9393
get_pg_version, \
94-
execute_utility, \
94+
execute_utility2, \
9595
options_string, \
9696
clean_on_error
9797

@@ -301,7 +301,7 @@ def base_dir(self):
301301
@property
302302
def bin_dir(self):
303303
if not self._bin_dir:
304-
self._bin_dir = os.path.dirname(get_bin_path("pg_config"))
304+
self._bin_dir = os.path.dirname(get_bin_path2(self.os_ops, "pg_config"))
305305
return self._bin_dir
306306

307307
@property
@@ -684,7 +684,7 @@ def status(self):
684684
"-D", self.data_dir,
685685
"status"
686686
] # yapf: disable
687-
status_code, out, error = execute_utility(_params, self.utils_log_file, verbose=True)
687+
status_code, out, error = execute_utility2(self.os_ops, _params, self.utils_log_file, verbose=True)
688688
if error and 'does not exist' in error:
689689
return NodeStatus.Uninitialized
690690
elif 'no server running' in out:
@@ -710,7 +710,7 @@ def get_control_data(self):
710710
_params += ["-D"] if self._pg_version >= PgVer('9.5') else []
711711
_params += [self.data_dir]
712712

713-
data = execute_utility(_params, self.utils_log_file)
713+
data = execute_utility2(self.os_ops, _params, self.utils_log_file)
714714

715715
out_dict = {}
716716

@@ -793,7 +793,7 @@ def start(self, params=[], wait=True):
793793

794794
def LOCAL__start_node():
795795
# 'error' will be None on Windows
796-
_, _, error = execute_utility(_params, self.utils_log_file, verbose=True)
796+
_, _, error = execute_utility2(self.os_ops, _params, self.utils_log_file, verbose=True)
797797
assert error is None or type(error) == str # noqa: E721
798798
if error and 'does not exist' in error:
799799
raise Exception(error)
@@ -882,7 +882,7 @@ def stop(self, params=[], wait=True):
882882
"stop"
883883
] + params # yapf: disable
884884

885-
execute_utility(_params, self.utils_log_file)
885+
execute_utility2(self.os_ops, _params, self.utils_log_file)
886886

887887
self._maybe_stop_logger()
888888
self.is_started = False
@@ -924,7 +924,7 @@ def restart(self, params=[]):
924924
] + params # yapf: disable
925925

926926
try:
927-
error_code, out, error = execute_utility(_params, self.utils_log_file, verbose=True)
927+
error_code, out, error = execute_utility2(self.os_ops, _params, self.utils_log_file, verbose=True)
928928
if error and 'could not start server' in error:
929929
raise ExecUtilException
930930
except ExecUtilException as e:
@@ -953,7 +953,7 @@ def reload(self, params=[]):
953953
"reload"
954954
] + params # yapf: disable
955955

956-
execute_utility(_params, self.utils_log_file)
956+
execute_utility2(self.os_ops, _params, self.utils_log_file)
957957

958958
return self
959959

@@ -975,7 +975,7 @@ def promote(self, dbname=None, username=None):
975975
"promote"
976976
] # yapf: disable
977977

978-
execute_utility(_params, self.utils_log_file)
978+
execute_utility2(self.os_ops, _params, self.utils_log_file)
979979

980980
# for versions below 10 `promote` is asynchronous so we need to wait
981981
# until it actually becomes writable
@@ -1010,7 +1010,7 @@ def pg_ctl(self, params):
10101010
"-w" # wait
10111011
] + params # yapf: disable
10121012

1013-
return execute_utility(_params, self.utils_log_file)
1013+
return execute_utility2(self.os_ops, _params, self.utils_log_file)
10141014

10151015
def free_port(self):
10161016
"""
@@ -1230,7 +1230,7 @@ def tmpfile():
12301230
"-F", format.value
12311231
] # yapf: disable
12321232

1233-
execute_utility(_params, self.utils_log_file)
1233+
execute_utility2(self.os_ops, _params, self.utils_log_file)
12341234

12351235
return filename
12361236

@@ -1259,7 +1259,7 @@ def restore(self, filename, dbname=None, username=None):
12591259

12601260
# try pg_restore if dump is binary format, and psql if not
12611261
try:
1262-
execute_utility(_params, self.utils_log_name)
1262+
execute_utility2(self.os_ops, _params, self.utils_log_name)
12631263
except ExecUtilException:
12641264
self.psql(filename=filename, dbname=dbname, username=username)
12651265

@@ -1612,7 +1612,7 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs):
16121612
# should be the last one
16131613
_params.append(dbname)
16141614

1615-
return execute_utility(_params, self.utils_log_file)
1615+
return execute_utility2(self.os_ops, _params, self.utils_log_file)
16161616

16171617
def connect(self,
16181618
dbname=None,
@@ -1809,7 +1809,7 @@ def _get_bin_path(self, filename):
18091809
if self.bin_dir:
18101810
bin_path = os.path.join(self.bin_dir, filename)
18111811
else:
1812-
bin_path = get_bin_path(filename)
1812+
bin_path = get_bin_path2(self.os_ops, filename)
18131813
return bin_path
18141814

18151815
def _escape_config_value(value):

testgres/utils.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from .helpers.port_manager import PortManager
1717
from .exceptions import ExecUtilException
1818
from .config import testgres_config as tconf
19+
from .operations.os_ops import OsOperations
20+
from .operations.remote_ops import RemoteOperations
1921

2022
# rows returned by PG_CONFIG
2123
_pg_config_data = {}
@@ -68,7 +70,14 @@ def execute_utility(args, logfile=None, verbose=False):
6870
Returns:
6971
stdout of executed utility.
7072
"""
71-
exit_status, out, error = tconf.os_ops.exec_command(args, verbose=True)
73+
return execute_utility2(tconf.os_ops, args, logfile, verbose)
74+
75+
76+
def execute_utility2(os_ops: OsOperations, args, logfile=None, verbose=False):
77+
assert os_ops is not None
78+
assert isinstance(os_ops, OsOperations)
79+
80+
exit_status, out, error = os_ops.exec_command(args, verbose=True)
7281
# decode result
7382
out = '' if not out else out
7483
if isinstance(out, bytes):
@@ -79,11 +88,11 @@ def execute_utility(args, logfile=None, verbose=False):
7988
# write new log entry if possible
8089
if logfile:
8190
try:
82-
tconf.os_ops.write(filename=logfile, data=args, truncate=True)
91+
os_ops.write(filename=logfile, data=args, truncate=True)
8392
if out:
8493
# comment-out lines
8594
lines = [u'\n'] + ['# ' + line for line in out.splitlines()] + [u'\n']
86-
tconf.os_ops.write(filename=logfile, data=lines)
95+
os_ops.write(filename=logfile, data=lines)
8796
except IOError:
8897
raise ExecUtilException(
8998
"Problem with writing to logfile `{}` during run command `{}`".format(logfile, args))
@@ -98,25 +107,32 @@ def get_bin_path(filename):
98107
Return absolute path to an executable using PG_BIN or PG_CONFIG.
99108
This function does nothing if 'filename' is already absolute.
100109
"""
110+
return get_bin_path2(tconf.os_ops, filename)
111+
112+
113+
def get_bin_path2(os_ops: OsOperations, filename):
114+
assert os_ops is not None
115+
assert isinstance(os_ops, OsOperations)
116+
101117
# check if it's already absolute
102118
if os.path.isabs(filename):
103119
return filename
104-
if tconf.os_ops.remote:
120+
if isinstance(os_ops, RemoteOperations):
105121
pg_config = os.environ.get("PG_CONFIG_REMOTE") or os.environ.get("PG_CONFIG")
106122
else:
107123
# try PG_CONFIG - get from local machine
108124
pg_config = os.environ.get("PG_CONFIG")
109125

110126
if pg_config:
111-
bindir = get_pg_config()["BINDIR"]
127+
bindir = get_pg_config(pg_config, os_ops)["BINDIR"]
112128
return os.path.join(bindir, filename)
113129

114130
# try PG_BIN
115-
pg_bin = tconf.os_ops.environ("PG_BIN")
131+
pg_bin = os_ops.environ("PG_BIN")
116132
if pg_bin:
117133
return os.path.join(pg_bin, filename)
118134

119-
pg_config_path = tconf.os_ops.find_executable('pg_config')
135+
pg_config_path = os_ops.find_executable('pg_config')
120136
if pg_config_path:
121137
bindir = get_pg_config(pg_config_path)["BINDIR"]
122138
return os.path.join(bindir, filename)
@@ -129,12 +145,20 @@ def get_pg_config(pg_config_path=None, os_ops=None):
129145
Return output of pg_config (provided that it is installed).
130146
NOTE: this function caches the result by default (see GlobalConfig).
131147
"""
132-
if os_ops:
133-
tconf.os_ops = os_ops
148+
149+
if os_ops is None:
150+
os_ops = tconf.os_ops
151+
152+
return get_pg_config2(os_ops, pg_config_path)
153+
154+
155+
def get_pg_config2(os_ops: OsOperations, pg_config_path):
156+
assert os_ops is not None
157+
assert isinstance(os_ops, OsOperations)
134158

135159
def cache_pg_config_data(cmd):
136160
# execute pg_config and get the output
137-
out = tconf.os_ops.exec_command(cmd, encoding='utf-8')
161+
out = os_ops.exec_command(cmd, encoding='utf-8')
138162

139163
data = {}
140164
for line in out.splitlines():
@@ -158,7 +182,7 @@ def cache_pg_config_data(cmd):
158182
return _pg_config_data
159183

160184
# try specified pg_config path or PG_CONFIG
161-
if tconf.os_ops.remote:
185+
if isinstance(os_ops, RemoteOperations):
162186
pg_config = pg_config_path or os.environ.get("PG_CONFIG_REMOTE") or os.environ.get("PG_CONFIG")
163187
else:
164188
# try PG_CONFIG - get from local machine

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy