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

Commit364d358

Browse files
Merge pull request#158 from dmitry-lipetsk/D20241206_002--local_op-run_command
LocalOperations::_run_command is refactored
2 parentscf1d227 +e3999dd commit364d358

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

‎testgres/operations/local_ops.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,47 +64,55 @@ def _process_output(encoding, temp_file_path):
6464
output=output.decode(encoding)
6565
returnoutput,None# In Windows stderr writing in stdout
6666

67-
def_run_command(self,cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding):
68-
"""Execute a command and return the process and its output."""
69-
ifos.name=='nt'andstdoutisNone:# Windows
70-
withtempfile.NamedTemporaryFile(mode='w+b',delete=False)astemp_file:
71-
stdout=temp_file
72-
stderr=subprocess.STDOUT
73-
process=subprocess.Popen(
74-
cmd,
75-
shell=shell,
76-
stdin=stdinorsubprocess.PIPEifinputisnotNoneelseNone,
77-
stdout=stdout,
78-
stderr=stderr,
79-
)
80-
ifget_process:
81-
returnprocess,None,None
82-
temp_file_path=temp_file.name
83-
84-
# Wait process finished
85-
process.wait()
86-
87-
output,error=self._process_output(encoding,temp_file_path)
88-
returnprocess,output,error
89-
else:# Other OS
67+
def_run_command__nt(self,cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding):
68+
withtempfile.NamedTemporaryFile(mode='w+b',delete=False)astemp_file:
69+
stdout=temp_file
70+
stderr=subprocess.STDOUT
9071
process=subprocess.Popen(
9172
cmd,
9273
shell=shell,
9374
stdin=stdinorsubprocess.PIPEifinputisnotNoneelseNone,
94-
stdout=stdoutorsubprocess.PIPE,
95-
stderr=stderrorsubprocess.PIPE,
75+
stdout=stdout,
76+
stderr=stderr,
9677
)
9778
ifget_process:
9879
returnprocess,None,None
99-
try:
100-
output,error=process.communicate(input=input.encode(encoding)ifinputelseNone,timeout=timeout)
101-
ifencoding:
102-
output=output.decode(encoding)
103-
error=error.decode(encoding)
104-
returnprocess,output,error
105-
exceptsubprocess.TimeoutExpired:
106-
process.kill()
107-
raiseExecUtilException("Command timed out after {} seconds.".format(timeout))
80+
temp_file_path=temp_file.name
81+
82+
# Wait process finished
83+
process.wait()
84+
85+
output,error=self._process_output(encoding,temp_file_path)
86+
returnprocess,output,error
87+
88+
def_run_command__generic(self,cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding):
89+
process=subprocess.Popen(
90+
cmd,
91+
shell=shell,
92+
stdin=stdinorsubprocess.PIPEifinputisnotNoneelseNone,
93+
stdout=stdoutorsubprocess.PIPE,
94+
stderr=stderrorsubprocess.PIPE,
95+
)
96+
ifget_process:
97+
returnprocess,None,None
98+
try:
99+
output,error=process.communicate(input=input.encode(encoding)ifinputelseNone,timeout=timeout)
100+
ifencoding:
101+
output=output.decode(encoding)
102+
error=error.decode(encoding)
103+
returnprocess,output,error
104+
exceptsubprocess.TimeoutExpired:
105+
process.kill()
106+
raiseExecUtilException("Command timed out after {} seconds.".format(timeout))
107+
108+
def_run_command(self,cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding):
109+
"""Execute a command and return the process and its output."""
110+
ifos.name=='nt'andstdoutisNone:# Windows
111+
method=__class__._run_command__nt
112+
else:# Other OS
113+
method=__class__._run_command__generic
114+
115+
returnmethod(self,cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding)
108116

109117
defexec_command(self,cmd,wait_exit=False,verbose=False,expect_error=False,encoding=None,shell=False,
110118
text=False,input=None,stdin=None,stdout=None,stderr=None,get_process=False,timeout=None,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp