Skip to content

Commit 019cc4c

Browse files
author
vshepard
committed
Fix initdb error on Windows
1 parent 846c05f commit 019cc4c

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

testgres/operations/local_ops.py

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,41 @@ def exec_command(self, cmd, wait_exit=False, verbose=False,
5555
- proc: The process to use for subprocess creation.
5656
:return: The output of the subprocess.
5757
"""
58-
if os.name == 'nt':
59-
with tempfile.NamedTemporaryFile() as buf:
60-
process = subprocess.Popen(cmd, stdout=buf, stderr=subprocess.STDOUT)
61-
process.communicate()
62-
buf.seek(0)
63-
result = buf.read().decode(encoding)
64-
return result
58+
process = subprocess.Popen(
59+
cmd,
60+
shell=shell,
61+
stdin=stdin,
62+
stdout=stdout,
63+
stderr=stderr,
64+
text=text
65+
)
66+
67+
if get_process:
68+
return process
69+
70+
try:
71+
result, error = process.communicate(input=input, timeout=timeout)
72+
except subprocess.TimeoutExpired:
73+
process.kill()
74+
raise ExecUtilException("Command timed out after {} seconds.".format(timeout))
75+
exit_status = process.returncode
76+
77+
if encoding:
78+
result = result.decode(encoding) if result else ''
79+
error = error.decode(encoding) if error else ''
80+
81+
if exit_status != 0 or (expect_error and error):
82+
if exit_status == 0:
83+
exit_status = 1
84+
raise ExecUtilException(message='Utility exited with non-zero code. Error `{}`'.format(error),
85+
command=cmd,
86+
exit_code=exit_status,
87+
out=result)
88+
89+
if verbose:
90+
return exit_status, result, error
6591
else:
66-
process = subprocess.Popen(
67-
cmd,
68-
shell=shell,
69-
stdout=stdout,
70-
stderr=stderr,
71-
)
72-
if get_process:
73-
return process
74-
75-
try:
76-
result, error = process.communicate(input, timeout=timeout)
77-
except subprocess.TimeoutExpired:
78-
process.kill()
79-
raise ExecUtilException("Command timed out after {} seconds.".format(timeout))
80-
exit_status = process.returncode
81-
82-
error_found = exit_status != 0 or any(marker in error for marker in error_markers)
83-
84-
if encoding:
85-
result = result.decode(encoding)
86-
error = error.decode(encoding)
87-
88-
if expect_error:
89-
raise Exception(result, error)
90-
91-
if exit_status != 0 or error_found:
92-
if exit_status == 0:
93-
exit_status = 1
94-
raise ExecUtilException(message='Utility exited with non-zero code. Error `{}`'.format(error),
95-
command=cmd,
96-
exit_code=exit_status,
97-
out=result)
98-
if verbose:
99-
return exit_status, result, error
100-
else:
101-
return result
92+
return result
10293

10394
# Environment setup
10495
def environ(self, var_name):

0 commit comments

Comments
 (0)