diff options
author | Helmut Grohne <helmut@subdivi.de> | 2022-06-23 17:27:42 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2022-06-23 17:27:42 +0200 |
commit | 8d8008457c8f081dd209772beb10ef98fb29faea (patch) | |
tree | b984793b5bd747205800453fa476da7c1fa572ac /mdbp/ssh.py | |
parent | 68e2e0a5f29e011d2304d03cf26f10e7e8846d3d (diff) | |
download | mdbp-8d8008457c8f081dd209772beb10ef98fb29faea.tar.gz |
ssh: use Popen as a context manager
Diffstat (limited to 'mdbp/ssh.py')
-rw-r--r-- | mdbp/ssh.py | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/mdbp/ssh.py b/mdbp/ssh.py index 4cb64cd..e53b23f 100644 --- a/mdbp/ssh.py +++ b/mdbp/ssh.py @@ -55,15 +55,20 @@ def main() -> None: build = buildjson(args.command.pop()) cmd = ["ssh", args.host, "mdbp-streamapi", *args.command] - proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=sys.stdout - if build["output"].get("log", True) - else subprocess.DEVNULL) - assert proc.stdin is not None - produce_request_tar(build, proc.stdin) - proc.stdin.close() - exitcode = 0 with contextlib.ExitStack() as stack: + proc = stack.enter_context( + subprocess.Popen( + cmd, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=sys.stdout if build["output"].get("log", True) + else subprocess.DEVNULL + ), + ) + assert proc.stdin is not None + produce_request_tar(build, proc.stdin) + proc.stdin.close() + exitcode = 0 try: outtar = stack.enter_context(tarfile.open(fileobj=proc.stdout, mode="r|")) @@ -77,7 +82,7 @@ def main() -> None: raise ValueError("expected flat tar as output") outtar.extract(member, build["output"]["directory"], set_attrs=False) - sys.exit(proc.wait() or exitcode) + sys.exit(proc.wait() or exitcode) if __name__ == "__main__": main() |