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

Commite35fb1d

Browse files
demonolockvshepard
authored and
vshepard
committed
Refactoring local_ops.py
1 parentde1ce5c commite35fb1d

File tree

4 files changed

+62
-119
lines changed

4 files changed

+62
-119
lines changed

‎testgres/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
from .operations.local_opsimportLocalOperations
5353
from .operations.remote_opsimportRemoteOperations
5454

55+
from .helpers.port_managerimportPortManager
56+
5557
__all__= [
5658
"get_new_node",
5759
"get_remote_node",
@@ -62,6 +64,6 @@
6264
"XLogMethod","IsolationLevel","NodeStatus","ProcessType","DumpFormat",
6365
"PostgresNode","NodeApp",
6466
"reserve_port","release_port","bound_ports","get_bin_path","get_pg_config","get_pg_version",
65-
"First","Any",
67+
"First","Any","PortManager",
6668
"OsOperations","LocalOperations","RemoteOperations","ConnectionParams"
6769
]

‎testgres/node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,8 @@ def status(self):
623623
"-D",self.data_dir,
624624
"status"
625625
]# yapf: disable
626-
status_code,out,err=execute_utility(_params,self.utils_log_file,verbose=True)
627-
if'does not exist'inerr:
626+
status_code,out,error=execute_utility(_params,self.utils_log_file,verbose=True)
627+
iferrorand'does not exist'inerror:
628628
returnNodeStatus.Uninitialized
629629
elif'no server running'inout:
630630
returnNodeStatus.Stopped
@@ -717,7 +717,7 @@ def start(self, params=[], wait=True):
717717

718718
try:
719719
exit_status,out,error=execute_utility(_params,self.utils_log_file,verbose=True)
720-
if'does not exist'inerror:
720+
iferrorand'does not exist'inerror:
721721
raiseException
722722
exceptExceptionase:
723723
msg='Cannot start node'
@@ -791,7 +791,7 @@ def restart(self, params=[]):
791791

792792
try:
793793
error_code,out,error=execute_utility(_params,self.utils_log_file,verbose=True)
794-
if'could not start server'inerror:
794+
iferrorand'could not start server'inerror:
795795
raiseExecUtilException
796796
exceptExecUtilExceptionase:
797797
msg='Cannot restart node'

‎testgres/operations/local_ops.py

Lines changed: 54 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,6 @@ def __init__(self, conn_params=None):
3838
self.remote=False
3939
self.username=conn_params.usernameorself.get_user()
4040

41-
@staticmethod
42-
def_run_command(cmd,shell,input,stdin,stdout,stderr,timeout,encoding,temp_file=None,get_process=None):
43-
"""Execute a command and return the process."""
44-
iftemp_fileisnotNone:
45-
stdout=stdoutortemp_file
46-
stderr=stderrorsubprocess.STDOUT
47-
else:
48-
stdout=stdoutorsubprocess.PIPE
49-
stderr=stderrorsubprocess.PIPE
50-
51-
process=subprocess.Popen(
52-
cmd,
53-
shell=shell,
54-
stdin=stdinorsubprocess.PIPEifinputisnotNoneelseNone,
55-
stdout=stdout,
56-
stderr=stderr,
57-
)
58-
59-
ifget_process:
60-
returnNone,process
61-
try:
62-
returnprocess.communicate(input=input.encode(encoding)ifinputelseNone,timeout=timeout),process
63-
exceptsubprocess.TimeoutExpired:
64-
process.kill()
65-
raiseExecUtilException("Command timed out after {} seconds.".format(timeout))
66-
6741
@staticmethod
6842
def_raise_exec_exception(message,command,exit_code,output):
6943
"""Raise an ExecUtilException."""
@@ -72,105 +46,72 @@ def _raise_exec_exception(message, command, exit_code, output):
7246
exit_code=exit_code,
7347
out=output)
7448

75-
defexec_command(self,cmd,wait_exit=False,verbose=False,
76-
expect_error=False,encoding=None,shell=False,text=False,
77-
input=None,stdin=None,stdout=None,stderr=None,
78-
get_process=None,timeout=None):
79-
"""
80-
Execute a command in a subprocess.
81-
82-
Args:
83-
- cmd: The command to execute.
84-
- wait_exit: Whether to wait for the subprocess to exit before returning.
85-
- verbose: Whether to return verbose output.
86-
- expect_error: Whether to raise an error if the subprocess exits with an error status.
87-
- encoding: The encoding to use for decoding the subprocess output.
88-
- shell: Whether to use shell when executing the subprocess.
89-
- text: Whether to return str instead of bytes for the subprocess output.
90-
- input: The input to pass to the subprocess.
91-
- stdout: The stdout to use for the subprocess.
92-
- stderr: The stderr to use for the subprocess.
93-
- proc: The process to use for subprocess creation.
94-
:return: The output of the subprocess.
95-
"""
96-
ifos.name=='nt':
97-
returnself._exec_command_windows(cmd,wait_exit=wait_exit,verbose=verbose,
98-
expect_error=expect_error,encoding=encoding,shell=shell,text=text,
99-
input=input,stdin=stdin,stdout=stdout,stderr=stderr,
100-
get_process=get_process,timeout=timeout)
101-
else:
49+
@staticmethod
50+
def_process_output(encoding,temp_file_path):
51+
"""Process the output of a command from a temporary file."""
52+
withopen(temp_file_path,'rb')astemp_file:
53+
output=temp_file.read()
54+
ifencoding:
55+
output=output.decode(encoding)
56+
returnoutput,None# In Windows stderr writing in stdout
57+
58+
def_run_command(self,cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding):
59+
"""Execute a command and return the process and its output."""
60+
ifos.name=='nt'andstdoutisNone:# Windows
61+
withtempfile.NamedTemporaryFile(mode='w+b',delete=False)astemp_file:
62+
stdout=temp_file
63+
stderr=subprocess.STDOUT
64+
process=subprocess.Popen(
65+
cmd,
66+
shell=shell,
67+
stdin=stdinorsubprocess.PIPEifinputisnotNoneelseNone,
68+
stdout=stdout,
69+
stderr=stderr,
70+
)
71+
ifget_process:
72+
returnprocess,None,None
73+
temp_file_path=temp_file.name
74+
75+
# Wait process finished
76+
process.wait()
77+
78+
output,error=self._process_output(encoding,temp_file_path)
79+
returnprocess,output,error
80+
else:# Other OS
10281
process=subprocess.Popen(
10382
cmd,
10483
shell=shell,
105-
stdin=stdin,
106-
stdout=stdout,
107-
stderr=stderr,
84+
stdin=stdinorsubprocess.PIPEifinputisnotNoneelseNone,
85+
stdout=stdoutorsubprocess.PIPE,
86+
stderr=stderrorsubprocess.PIPE,
10887
)
10988
ifget_process:
110-
returnprocess
111-
89+
returnprocess,None,None
11290
try:
113-
result,error=process.communicate(input,timeout=timeout)
91+
output,error=process.communicate(input=input.encode(encoding)ifinputelseNone,timeout=timeout)
92+
ifencoding:
93+
output=output.decode(encoding)
94+
error=error.decode(encoding)
95+
returnprocess,output,error
11496
exceptsubprocess.TimeoutExpired:
11597
process.kill()
11698
raiseExecUtilException("Command timed out after {} seconds.".format(timeout))
117-
exit_status=process.returncode
11899

119-
error_found=exit_status!=0orhas_errors(error)
120-
121-
ifencoding:
122-
result=result.decode(encoding)
123-
error=error.decode(encoding)
124-
125-
ifexpect_error:
126-
raiseException(result,error)
127-
128-
ifexit_status!=0orerror_found:
129-
ifexit_status==0:
130-
exit_status=1
131-
self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,exit_status,result)
132-
ifverbose:
133-
returnexit_status,result,error
134-
else:
135-
returnresult
100+
defexec_command(self,cmd,wait_exit=False,verbose=False,expect_error=False,encoding=None,shell=False,
101+
text=False,input=None,stdin=None,stdout=None,stderr=None,get_process=False,timeout=None):
102+
"""
103+
Execute a command in a subprocess and handle the output based on the provided parameters.
104+
"""
105+
process,output,error=self._run_command(cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding)
106+
ifget_process:
107+
returnprocess
108+
ifprocess.returncode!=0or (has_errors(error)andnotexpect_error):
109+
self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,process.returncode,error)
136110

137-
@staticmethod
138-
def_process_output(process,encoding,temp_file=None):
139-
"""Process the output of a command."""
140-
iftemp_fileisnotNone:
141-
temp_file.seek(0)
142-
output=temp_file.read()
111+
ifverbose:
112+
returnprocess.returncode,output,error
143113
else:
144-
output=process.stdout.read()
145-
146-
ifencoding:
147-
output=output.decode(encoding)
148-
149-
returnoutput
150-
151-
def_exec_command_windows(self,cmd,wait_exit=False,verbose=False,
152-
expect_error=False,encoding=None,shell=False,text=False,
153-
input=None,stdin=None,stdout=None,stderr=None,
154-
get_process=None,timeout=None):
155-
withtempfile.NamedTemporaryFile(mode='w+b')astemp_file:
156-
_,process=self._run_command(cmd,shell,input,stdin,stdout,stderr,timeout,encoding,temp_file,get_process)
157-
ifget_process:
158-
returnprocess
159-
result=self._process_output(process,encoding,temp_file)
160-
161-
ifprocess.returncode!=0orhas_errors(result):
162-
ifprocess.returncode==0:
163-
process.returncode=1
164-
ifexpect_error:
165-
ifverbose:
166-
returnprocess.returncode,result,result
167-
else:
168-
returnresult
169-
else:
170-
self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,process.returncode,
171-
result)
172-
173-
return (process.returncode,result,result)ifverboseelseresult
114+
returnoutput
174115

175116
# Environment setup
176117
defenviron(self,var_name):

‎testgres/operations/os_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
8181
deftouch(self,filename):
8282
raiseNotImplementedError()
8383

84-
defread(self,filename):
84+
defread(self,filename,encoding,binary):
8585
raiseNotImplementedError()
8686

8787
defreadlines(self,filename):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp