Skip to content

Commit 364d358

Browse files
Merge pull request #158 from dmitry-lipetsk/D20241206_002--local_op-run_command
LocalOperations::_run_command is refactored
2 parents cf1d227 + e3999dd commit 364d358

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

testgres/operations/local_ops.py

+42-34
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
return output, 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-
if os.name == 'nt' and stdout is None: # Windows
70-
with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as temp_file:
71-
stdout = temp_file
72-
stderr = subprocess.STDOUT
73-
process = subprocess.Popen(
74-
cmd,
75-
shell=shell,
76-
stdin=stdin or subprocess.PIPE if input is not None else None,
77-
stdout=stdout,
78-
stderr=stderr,
79-
)
80-
if get_process:
81-
return process, 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-
return process, output, error
89-
else: # Other OS
67+
def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process, timeout, encoding):
68+
with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as temp_file:
69+
stdout = temp_file
70+
stderr = subprocess.STDOUT
9071
process = subprocess.Popen(
9172
cmd,
9273
shell=shell,
9374
stdin=stdin or subprocess.PIPE if input is not None else None,
94-
stdout=stdout or subprocess.PIPE,
95-
stderr=stderr or subprocess.PIPE,
75+
stdout=stdout,
76+
stderr=stderr,
9677
)
9778
if get_process:
9879
return process, None, None
99-
try:
100-
output, error = process.communicate(input=input.encode(encoding) if input else None, timeout=timeout)
101-
if encoding:
102-
output = output.decode(encoding)
103-
error = error.decode(encoding)
104-
return process, output, error
105-
except subprocess.TimeoutExpired:
106-
process.kill()
107-
raise ExecUtilException("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+
return process, 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=stdin or subprocess.PIPE if input is not None else None,
93+
stdout=stdout or subprocess.PIPE,
94+
stderr=stderr or subprocess.PIPE,
95+
)
96+
if get_process:
97+
return process, None, None
98+
try:
99+
output, error = process.communicate(input=input.encode(encoding) if input else None, timeout=timeout)
100+
if encoding:
101+
output = output.decode(encoding)
102+
error = error.decode(encoding)
103+
return process, output, error
104+
except subprocess.TimeoutExpired:
105+
process.kill()
106+
raise ExecUtilException("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+
if os.name == 'nt' and stdout is None: # Windows
111+
method = __class__._run_command__nt
112+
else: # Other OS
113+
method = __class__._run_command__generic
114+
115+
return method(self, cmd, shell, input, stdin, stdout, stderr, get_process, timeout, encoding)
108116

109117
def exec_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)