From: Michael Tremer Date: Fri, 19 May 2023 16:53:46 +0000 (+0000) Subject: backend: Implement writing to stdin of shell commands X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0646a269cd45204a1b82a07c18a5ae55d2192808;p=pbs.git backend: Implement writing to stdin of shell commands Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index db7de0eb..8c7263b5 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -229,18 +229,36 @@ class Backend(object): # Run the command return await self._command(*command, env=env, **kwargs) - async def _command(self, *command, return_output=False, **kwargs): + async def _command(self, *command, return_output=False, input=None, **kwargs): log.debug("Running command: %s" % " ".join(command)) # Fork child process process = await asyncio.create_subprocess_exec( *command, - stdin=asyncio.subprocess.DEVNULL, + stdin=asyncio.subprocess.PIPE if input else asyncio.subprocess.DEVNULL, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, **kwargs, ) + # Send input + if input: + # Convert to bytes + if not isinstance(input, bytes): + input = input.encode() + + # Write the entire data chunk by chunk + while input: + chunk, input = input[0:64], input[64:] + if not chunk: + break + + process.stdin.write(chunk) + await process.stdin.drain() + + # Close the input once we are done + process.stdin.close() + stdout = [] # Fetch output of command and send it to the logger