]> git.ipfire.org Git - pbs.git/commitdiff
backend: Implement writing to stdin of shell commands
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 May 2023 16:53:46 +0000 (16:53 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 May 2023 16:53:46 +0000 (16:53 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/__init__.py

index db7de0ebc767ca5e22813aeef380d50d513dc591..8c7263b5b95853e717e40ac7ff906254541d680b 100644 (file)
@@ -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