Commit 2b268ecb authored by Tim Bleimehl's avatar Tim Bleimehl 🤸🏼
Browse files

we are getting closer to fix python file streaming :)

parent ab1a9123
......@@ -59,11 +59,8 @@ class Executer:
uncompress_file_before_stdin: bool = False,
decode_byte_output: bool = True,
) -> str:
cmd = (
["/bin/sh", "-c", command]
if isinstance(command, str)
else ["/bin/sh", "-c"] + command
)
cmd = " ".join(command) if isinstance(command, list) else command
cmd = cmd.replace('"', "'")
# We can answer these questions later:
# https://stackoverflow.com/questions/70254049/dockersdk-exec-run-output-to-file
# https://github.com/docker/docker-py/issues/2323
......@@ -92,10 +89,16 @@ class Executer:
container.put_archive(
os.path.dirname(container_target_path), stream.getvalue()
)
cmd.append(f" < {container_target_path}")
cmd = cmd + f" < {container_target_path}"
if stream_result_to_file:
if compress_result_to_file:
f = gzip.open(stream_result_to_file, "wb")
else:
f = open(stream_result_to_file, "wb")
# ToDo: with https://docker-py.readthedocs.io/en/stable/api.html#docker.api.container.ContainerApiMixin.get_archive
print("cmd:", cmd)
process = container.exec_run(
cmd=cmd,
cmd=["/bin/sh", "-c", f'"{cmd}"'],
stream=True,
demux=True,
# METHOD 1 (see further down for explaination)
......@@ -107,57 +110,13 @@ class Executer:
stdin=False,
tty=False,
)
if stream_file_to_stdin:
if uncompress_file_before_stdin:
f = gzip.open(stream_file_to_stdin, "rb")
else:
f = open(stream_file_to_stdin, "rb")
##### METHOD 1 - preferred if fixable
# Socket stream method. It propaply streams (i think) but i can not seperate stdout from stdin at the end to catch the result :/
# helping links:
# https://github.com/docker/docker-py/issues/1507#issuecomment-901300117
# https://github.com/docker/docker-py/issues/983
# https://gist.github.com/zbyte64/6800eae10ce082bb78f0b7a2cca5cbc2
"""
s: socket.SocketIO = process.output
print(" ".join(cmd))
print(s._mode)
s._sock.setblocking(False)
end_of_file = False
chunksize = 8192
# os.write(s.fileno(), b"<<EOF\n")
while not end_of_file:
# stream to stdin
chunk = f.read(chunksize)
if chunk:
print(chunk)
os.write(s.fileno(), chunk)
else:
end_of_file = True
# os.write(s.fileno(), b"\nEOF")
time.sleep(1)
# here i get all my input mixed with the output :/ unuseable atm
print("readall:", s.read().decode("utf-8"))
# print(os.read(s.fileno(), 10000).decode("utf-8"))
s._sock.close()
"""
process
if stream_result_to_file:
if compress_result_to_file:
f = gzip.open(stream_result_to_file, "wb")
else:
f = open(stream_result_to_file, "wb")
# ToDo: Refactor with https://docker-py.readthedocs.io/en/stable/api.html#docker.api.container.ContainerApiMixin.get_archive
stdout = b""
stderr = b""
for output in process.output:
if stream_result_to_file:
if stream_result_to_file and output[0]:
f.write(output)
else:
elif output[0]:
stdout += output[0]
if output[1]:
stderr += output[1]
......@@ -167,9 +126,12 @@ class Executer:
raise Exception(
f"ERROR during container ({self.container_name}) exec:\nCommand: '{command}'\nError: '{stderr.decode('utf-8')}'"
)
elif decode_byte_output:
print(stdout)
return stdout.decode("utf-8").rstrip("\n")
else:
print(stdout)
return stdout
exit()
stdout = b""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment