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

Commit9e0899e

Browse files
author
v.shepard
committed
PBCKP-152 use black for formatting
1 parentf9b6bdb commit9e0899e

File tree

8 files changed

+125
-108
lines changed

8 files changed

+125
-108
lines changed

‎testgres/cache.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
fromsiximportraise_from
66

7-
from .op_ops.local_opsimportLocalOperations
8-
from .op_ops.os_opsimportOsOperations
7+
from .os_ops.local_opsimportLocalOperations
8+
from .os_ops.os_opsimportOsOperations
99
from .configimporttestgres_config
1010

1111
from .constsimportXLOG_CONTROL_FILE
@@ -25,6 +25,7 @@ def cached_initdb(data_dir, logfile=None, params=None, os_ops: OsOperations = Lo
2525
"""
2626
Perform initdb or use cached node files.
2727
"""
28+
testgres_config.os_ops=os_ops
2829

2930
defcall_initdb(initdb_dir,log=None):
3031
try:
@@ -60,7 +61,7 @@ def call_initdb(initdb_dir, log=None):
6061

6162
# XXX: build new WAL segment with our system id
6263
_params= [get_bin_path("pg_resetwal"),"-D",data_dir,"-f"]
63-
execute_utility(_params,logfile,os_ops)
64+
execute_utility(_params,logfile,os_ops=os_ops)
6465

6566
exceptExecUtilExceptionase:
6667
msg="Failed to reset WAL for system id"

‎testgres/config.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
importtempfile
66

77
fromcontextlibimportcontextmanager
8-
fromshutilimportrmtree
98

10-
from .op_ops.local_opsimportLocalOperations
9+
from .os_ops.local_opsimportLocalOperations
1110
from .constsimportTMP_CACHE
1211

1312

@@ -44,7 +43,7 @@ class GlobalConfig(object):
4443
_cached_initdb_dir=None
4544
""" underlying class attribute for cached_initdb_dir property """
4645

47-
os_ops=None
46+
os_ops=LocalOperations()
4847
""" OsOperation object that allows work on remote host """
4948
@property
5049
defcached_initdb_dir(self):
@@ -137,9 +136,9 @@ def copy(self):
137136

138137

139138
@atexit.register
140-
def_rm_cached_initdb_dirs(os_ops=LocalOperations()):
139+
def_rm_cached_initdb_dirs():
141140
fordincached_initdb_dirs:
142-
os_ops.rmdirs(d,ignore_errors=True)
141+
testgres_config.os_ops.rmdirs(d,ignore_errors=True)
143142

144143

145144
defpush_config(**options):

‎testgres/defaults.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
importdatetime
2-
importos
32
importstruct
43
importuuid
54

6-
from .op_ops.local_opsimportLocalOperations
5+
from .os_ops.local_opsimportLocalOperations
76

87

98
defdefault_dbname():

‎testgres/node.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
# coding: utf-8
22

3-
importio
43
importos
54
importrandom
6-
importshutil
75
importsignal
86
importthreading
97
fromqueueimportQueue
108

119
importpsutil
12-
importsubprocess
1310
importtime
1411

15-
fromop_ops.local_opsimportLocalOperations
16-
fromop_ops.os_opsimportOsOperations
17-
fromop_ops.remote_opsimportRemoteOperations
12+
from .os_ops.local_opsimportLocalOperations
13+
from .os_ops.remote_opsimportRemoteOperations
1814

1915
try:
2016
fromcollections.abcimportIterable
@@ -32,7 +28,6 @@
3228

3329
fromshutilimportrmtree
3430
fromsiximportraise_from,iteritems,text_type
35-
fromtempfileimportmkstemp,mkdtemp
3631

3732
from .enumsimport \
3833
NodeStatus, \
@@ -96,7 +91,6 @@
9691
eprint, \
9792
get_bin_path, \
9893
get_pg_version, \
99-
file_tail, \
10094
reserve_port, \
10195
release_port, \
10296
execute_utility, \
@@ -163,6 +157,7 @@ def __init__(self, name=None, port=None, base_dir=None,
163157
else:
164158
self.os_ops=RemoteOperations(host,hostname,ssh_key)
165159

160+
testgres_config.os_ops=self.os_ops
166161
# defaults for __exit__()
167162
self.cleanup_on_good_exit=testgres_config.node_cleanup_on_good_exit
168163
self.cleanup_on_bad_exit=testgres_config.node_cleanup_on_bad_exit
@@ -628,7 +623,7 @@ def status(self):
628623
"-D",self.data_dir,
629624
"status"
630625
]# yapf: disable
631-
execute_utility(_params,self.utils_log_file)
626+
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
632627
returnNodeStatus.Running
633628

634629
exceptExecUtilExceptionase:
@@ -650,7 +645,7 @@ def get_control_data(self):
650645
_params+= ["-D"]ifself._pg_version>=PgVer('9.5')else []
651646
_params+= [self.data_dir]
652647

653-
data=execute_utility(_params,self.utils_log_file)
648+
data=execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
654649

655650
out_dict= {}
656651

@@ -713,7 +708,7 @@ def start(self, params=[], wait=True):
713708
]+params# yapf: disable
714709

715710
try:
716-
execute_utility(_params,self.utils_log_file)
711+
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
717712
exceptExecUtilExceptionase:
718713
msg='Cannot start node'
719714
files=self._collect_special_files()
@@ -744,7 +739,7 @@ def stop(self, params=[], wait=True):
744739
"stop"
745740
]+params# yapf: disable
746741

747-
execute_utility(_params,self.utils_log_file)
742+
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
748743

749744
self._maybe_stop_logger()
750745
self.is_started=False
@@ -786,7 +781,7 @@ def restart(self, params=[]):
786781
]+params# yapf: disable
787782

788783
try:
789-
execute_utility(_params,self.utils_log_file)
784+
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
790785
exceptExecUtilExceptionase:
791786
msg='Cannot restart node'
792787
files=self._collect_special_files()
@@ -813,7 +808,7 @@ def reload(self, params=[]):
813808
"reload"
814809
]+params# yapf: disable
815810

816-
execute_utility(_params,self.utils_log_file)
811+
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
817812

818813
returnself
819814

@@ -835,7 +830,7 @@ def promote(self, dbname=None, username=None):
835830
"promote"
836831
]# yapf: disable
837832

838-
execute_utility(_params,self.utils_log_file)
833+
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
839834

840835
# for versions below 10 `promote` is asynchronous so we need to wait
841836
# until it actually becomes writable
@@ -870,7 +865,7 @@ def pg_ctl(self, params):
870865
"-w"# wait
871866
]+params# yapf: disable
872867

873-
returnexecute_utility(_params,self.utils_log_file)
868+
returnexecute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
874869

875870
deffree_port(self):
876871
"""
@@ -1035,10 +1030,9 @@ def dump(self,
10351030
# Generate tmpfile or tmpdir
10361031
deftmpfile():
10371032
ifformat==DumpFormat.Directory:
1038-
fname=mkdtemp(prefix=TMP_DUMP)
1033+
fname=self.os_ops.mkdtemp(prefix=TMP_DUMP)
10391034
else:
1040-
fd,fname=mkstemp(prefix=TMP_DUMP)
1041-
os.close(fd)
1035+
fname=self.os_ops.mkstemp(prefix=TMP_DUMP)
10421036
returnfname
10431037

10441038
# Set default arguments
@@ -1056,7 +1050,7 @@ def tmpfile():
10561050
"-F",format.value
10571051
]# yapf: disable
10581052

1059-
execute_utility(_params,self.utils_log_file)
1053+
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
10601054

10611055
returnfilename
10621056

@@ -1085,7 +1079,7 @@ def restore(self, filename, dbname=None, username=None):
10851079

10861080
# try pg_restore if dump is binary formate, and psql if not
10871081
try:
1088-
execute_utility(_params,self.utils_log_name)
1082+
execute_utility(_params,self.utils_log_name,os_ops=self.os_ops)
10891083
exceptExecUtilException:
10901084
self.psql(filename=filename,dbname=dbname,username=username)
10911085

@@ -1417,7 +1411,7 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs):
14171411
# should be the last one
14181412
_params.append(dbname)
14191413

1420-
returnexecute_utility(_params,self.utils_log_file)
1414+
returnexecute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
14211415

14221416
defconnect(self,
14231417
dbname=None,

‎testgres/op_ops/local_ops.pyrenamed to‎testgres/os_ops/local_ops.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,35 @@
1414

1515

1616
classLocalOperations(OsOperations):
17-
1817
def__init__(self,username=None):
1918
super().__init__()
2019
self.username=usernameorself.get_user()
2120

2221
# Command execution
2322
defexec_command(self,cmd,wait_exit=False,verbose=False,expect_error=False):
2423
ifisinstance(cmd,list):
25-
cmd=' '.join(cmd)
24+
cmd=" ".join(cmd)
2625
log.debug(f"os_ops.exec_command: `{cmd}`; remote={self.remote}")
2726
# Source global profile file + execute command
2827
try:
29-
process=subprocess.run(cmd,shell=True,text=True,
30-
stdout=subprocess.PIPE,
31-
stderr=subprocess.PIPE,
32-
timeout=CMD_TIMEOUT_SEC)
28+
process=subprocess.run(
29+
cmd,
30+
shell=True,
31+
text=True,
32+
stdout=subprocess.PIPE,
33+
stderr=subprocess.PIPE,
34+
timeout=CMD_TIMEOUT_SEC,
35+
)
3336
exit_status=process.returncode
3437
result=process.stdout
3538
error=process.stderr
3639

3740
ifexpect_error:
3841
raiseException(result,error)
39-
ifexit_status!=0or'error'inerror.lower():
40-
log.error(f"Problem in executing command: `{cmd}`\nerror:{error}\nexit_code:{exit_status}")
42+
ifexit_status!=0or"error"inerror.lower():
43+
log.error(
44+
f"Problem in executing command: `{cmd}`\nerror:{error}\nexit_code:{exit_status}"
45+
)
4146
exit(1)
4247

4348
ifverbose:
@@ -55,7 +60,7 @@ def environ(self, var_name):
5560
returnself.exec_command(cmd).strip()
5661

5762
deffind_executable(self,executable):
58-
search_paths=self.environ('PATH')
63+
search_paths=self.environ("PATH")
5964
ifnotsearch_paths:
6065
returnNone
6166

@@ -74,12 +79,12 @@ def is_executable(self, file):
7479
defadd_to_path(self,new_path):
7580
pathsep=self.pathsep
7681
# Check if the directory is already in PATH
77-
path=self.environ('PATH')
82+
path=self.environ("PATH")
7883
ifnew_pathnotinpath.split(pathsep):
7984
ifself.remote:
8085
self.exec_command(f"export PATH={new_path}{pathsep}{path}")
8186
else:
82-
os.environ['PATH']=f"{new_path}{pathsep}{path}"
87+
os.environ["PATH"]=f"{new_path}{pathsep}{path}"
8388
returnpathsep
8489

8590
defset_env(self,var_name,var_val):
@@ -112,17 +117,22 @@ def path_exists(self, path):
112117
@property
113118
defpathsep(self):
114119
os_name=self.get_name()
115-
ifos_name=='posix':
116-
pathsep=':'
117-
elifos_name=='nt':
118-
pathsep=';'
120+
ifos_name=="posix":
121+
pathsep=":"
122+
elifos_name=="nt":
123+
pathsep=";"
119124
else:
120125
raiseException(f"Unsupported operating system:{os_name}")
121126
returnpathsep
122127

123128
defmkdtemp(self,prefix=None):
124129
returntempfile.mkdtemp(prefix=prefix)
125130

131+
defmkstemp(self,prefix=None):
132+
fd,filename=tempfile.mkstemp(prefix=prefix)
133+
os.close(fd)# Close the file descriptor immediately after creating the file
134+
returnfilename
135+
126136
defcopytree(self,src,dst):
127137
returnshutil.copytree(src,dst)
128138

@@ -140,11 +150,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
140150
read_and_write: If True, the file will be opened with read and write permissions ('r+' option);
141151
if False (default), only write permission will be used ('w', 'a', 'wb', or 'ab' option)
142152
"""
143-
mode='wb'ifbinaryelse'w'
153+
mode="wb"ifbinaryelse"w"
144154
ifnottruncate:
145-
mode='a'+mode
155+
mode="a"+mode
146156
ifread_and_write:
147-
mode='r+'+mode
157+
mode="r+"+mode
148158

149159
withopen(filename,mode)asfile:
150160
ifisinstance(data,list):
@@ -161,11 +171,11 @@ def touch(self, filename):
161171
This method behaves as the 'touch' command in Unix. It's equivalent to calling 'touch filename' in the shell.
162172
"""
163173
# cross-python touch(). It is vulnerable to races, but who cares?
164-
withopen(filename,'a'):
174+
withopen(filename,"a"):
165175
os.utime(filename,None)
166176

167177
defread(self,filename):
168-
withopen(filename,'r')asfile:
178+
withopen(filename,"r")asfile:
169179
returnfile.read()
170180

171181
defreadlines(self,filename,num_lines=0,encoding=None):
@@ -176,14 +186,14 @@ def readlines(self, filename, num_lines=0, encoding=None):
176186
assertnum_lines>=0
177187

178188
ifnum_lines==0:
179-
withopen(filename,'r',encoding=encoding)asfile:
189+
withopen(filename,"r",encoding=encoding)asfile:
180190
returnfile.readlines()
181191

182192
else:
183193
bufsize=8192
184194
buffers=1
185195

186-
withopen(filename,'r',encoding=encoding)asfile:
196+
withopen(filename,"r",encoding=encoding)asfile:
187197
file.seek(0,os.SEEK_END)
188198
end_pos=file.tell()
189199

@@ -197,23 +207,25 @@ def readlines(self, filename, num_lines=0, encoding=None):
197207
ifcur_lines>=num_linesorpos==0:
198208
returnlines[-num_lines:]
199209

200-
buffers=int(buffers*max(2,int(num_lines/max(cur_lines,1))))# Adjust buffer size
210+
buffers=int(
211+
buffers*max(2,int(num_lines/max(cur_lines,1)))
212+
)# Adjust buffer size
201213

202214
defisfile(self,remote_file):
203215
returnos.path.isfile(remote_file)
204216

205217
# Processes control
206218
defkill(self,pid,signal):
207219
# Kill the process
208-
cmd=f'kill -{signal}{pid}'
220+
cmd=f"kill -{signal}{pid}"
209221
returnself.exec_command(cmd)
210222

211223
defget_pid(self):
212224
# Get current process id
213225
returnos.getpid()
214226

215227
# Database control
216-
defdb_connect(self,dbname,user,password=None,host='localhost',port=5432):
228+
defdb_connect(self,dbname,user,password=None,host="localhost",port=5432):
217229
conn=pglib.connect(
218230
host=host,
219231
port=port,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp