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

Commit669e134

Browse files
Merge pull request#195 from dmitry-lipetsk/D20250224_002--os_ops-in-free_utilities
execute_utility2, get_bin_path2, get_pg_config2 are added
2 parentsde738f9 +ed3ef60 commit669e134

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 .exceptionsimportBackupException
1717

18+
from .operations.os_opsimportOsOperations
19+
1820
from .utilsimport \
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+
assertnode.os_opsisnotNone
50+
assertisinstance(node.os_ops,OsOperations)
51+
4752
ifnotoptions:
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
returnself

‎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 .utilsimport \
18-
get_bin_path, \
19-
execute_utility
18+
get_bin_path2, \
19+
execute_utility2
2020

2121
from .operations.local_opsimportLocalOperations
2222
from .operations.os_opsimportOsOperations
@@ -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+
assertos_opsisnotNone
31+
assertisinstance(os_ops,OsOperations)
32+
33+
defmake_utility_path(name):
34+
assertnameisnotNone
35+
asserttype(name)==str# noqa: E721
36+
37+
ifbin_path:
38+
returnos.path.join(bin_path,name)
39+
40+
returnget_bin_path2(os_ops,name)
41+
3042
defcall_initdb(initdb_dir,log=logfile):
3143
try:
32-
initdb_path=os.path.join(bin_path,'initdb')ifbin_pathelseget_bin_path("initdb")
44+
initdb_path=make_utility_path("initdb")
3345
_params= [initdb_path,"-D",initdb_dir,"-N"]
34-
execute_utility(_params+ (paramsor []),log)
46+
execute_utility2(os_ops,_params+ (paramsor []),log)
3547
exceptExecUtilExceptionase:
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
exceptExecUtilExceptionase:
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 .utilsimport \
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
defbin_dir(self):
303303
ifnotself._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
returnself._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
iferrorand'does not exist'inerror:
689689
returnNodeStatus.Uninitialized
690690
elif'no server running'inout:
@@ -710,7 +710,7 @@ def get_control_data(self):
710710
_params+= ["-D"]ifself._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
defLOCAL__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
asserterrorisNoneortype(error)==str# noqa: E721
798798
iferrorand'does not exist'inerror:
799799
raiseException(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
iferrorand'could not start server'inerror:
929929
raiseExecUtilException
930930
exceptExecUtilExceptionase:
@@ -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
returnself
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-
returnexecute_utility(_params,self.utils_log_file)
1013+
returnexecute_utility2(self.os_ops,_params,self.utils_log_file)
10141014

10151015
deffree_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
returnfilename
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
exceptExecUtilException:
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-
returnexecute_utility(_params,self.utils_log_file)
1615+
returnexecute_utility2(self.os_ops,_params,self.utils_log_file)
16161616

16171617
defconnect(self,
16181618
dbname=None,
@@ -1809,7 +1809,7 @@ def _get_bin_path(self, filename):
18091809
ifself.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
returnbin_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_managerimportPortManager
1717
from .exceptionsimportExecUtilException
1818
from .configimporttestgres_configastconf
19+
from .operations.os_opsimportOsOperations
20+
from .operations.remote_opsimportRemoteOperations
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+
returnexecute_utility2(tconf.os_ops,args,logfile,verbose)
74+
75+
76+
defexecute_utility2(os_ops:OsOperations,args,logfile=None,verbose=False):
77+
assertos_opsisnotNone
78+
assertisinstance(os_ops,OsOperations)
79+
80+
exit_status,out,error=os_ops.exec_command(args,verbose=True)
7281
# decode result
7382
out=''ifnotoutelseout
7483
ifisinstance(out,bytes):
@@ -79,11 +88,11 @@ def execute_utility(args, logfile=None, verbose=False):
7988
# write new log entry if possible
8089
iflogfile:
8190
try:
82-
tconf.os_ops.write(filename=logfile,data=args,truncate=True)
91+
os_ops.write(filename=logfile,data=args,truncate=True)
8392
ifout:
8493
# comment-out lines
8594
lines= [u'\n']+ ['# '+lineforlineinout.splitlines()]+ [u'\n']
86-
tconf.os_ops.write(filename=logfile,data=lines)
95+
os_ops.write(filename=logfile,data=lines)
8796
exceptIOError:
8897
raiseExecUtilException(
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+
returnget_bin_path2(tconf.os_ops,filename)
111+
112+
113+
defget_bin_path2(os_ops:OsOperations,filename):
114+
assertos_opsisnotNone
115+
assertisinstance(os_ops,OsOperations)
116+
101117
# check if it's already absolute
102118
ifos.path.isabs(filename):
103119
returnfilename
104-
iftconf.os_ops.remote:
120+
ifisinstance(os_ops,RemoteOperations):
105121
pg_config=os.environ.get("PG_CONFIG_REMOTE")oros.environ.get("PG_CONFIG")
106122
else:
107123
# try PG_CONFIG - get from local machine
108124
pg_config=os.environ.get("PG_CONFIG")
109125

110126
ifpg_config:
111-
bindir=get_pg_config()["BINDIR"]
127+
bindir=get_pg_config(pg_config,os_ops)["BINDIR"]
112128
returnos.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
ifpg_bin:
117133
returnos.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
ifpg_config_path:
121137
bindir=get_pg_config(pg_config_path)["BINDIR"]
122138
returnos.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-
ifos_ops:
133-
tconf.os_ops=os_ops
148+
149+
ifos_opsisNone:
150+
os_ops=tconf.os_ops
151+
152+
returnget_pg_config2(os_ops,pg_config_path)
153+
154+
155+
defget_pg_config2(os_ops:OsOperations,pg_config_path):
156+
assertos_opsisnotNone
157+
assertisinstance(os_ops,OsOperations)
134158

135159
defcache_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
forlineinout.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-
iftconf.os_ops.remote:
185+
ifisinstance(os_ops,RemoteOperations):
162186
pg_config=pg_config_pathoros.environ.get("PG_CONFIG_REMOTE")oros.environ.get("PG_CONFIG")
163187
else:
164188
# try PG_CONFIG - get from local machine

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp