diff options
author | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2018-10-26 16:49:23 +0100 |
---|---|---|
committer | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2018-10-26 16:50:42 +0100 |
commit | 1b308fe34aa2bfea4098aa724cdd75b8b08fc984 (patch) | |
tree | 7b527e90d5bb64c2adf4418ab7a3b8ccf054c0c5 /buildstream/_stream.py | |
parent | a8250ca42e982a9d86f61226596609d2e67c29b2 (diff) | |
download | buildstream-1b308fe34aa2bfea4098aa724cdd75b8b08fc984.tar.gz |
_stream.py: Preserve stdout FD across checkout-to-stdout
In order to support things which might need to use the stdout FD
after checkout has completed writing a tarball to it, preserve it
by means of `os.dup()` and `os.dup2()`
Signed-off-by: Daniel Silverstone <daniel.silverstone@codethink.co.uk>
Diffstat (limited to 'buildstream/_stream.py')
-rw-r--r-- | buildstream/_stream.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/buildstream/_stream.py b/buildstream/_stream.py index e7a71978b..6e2e8b25b 100644 --- a/buildstream/_stream.py +++ b/buildstream/_stream.py @@ -423,9 +423,16 @@ class Stream(): else: if location == '-': with target.timed_activity("Creating tarball"): - with os.fdopen(sys.stdout.fileno(), 'wb') as fo: - with tarfile.open(fileobj=fo, mode="w|") as tf: - sandbox_vroot.export_to_tar(tf, '.') + # Save the stdout FD to restore later + saved_fd = os.dup(sys.stdout.fileno()) + try: + with os.fdopen(sys.stdout.fileno(), 'wb') as fo: + with tarfile.open(fileobj=fo, mode="w|") as tf: + sandbox_vroot.export_to_tar(tf, '.') + finally: + # No matter what, restore stdout for further use + os.dup2(saved_fd, sys.stdout.fileno()) + os.close(saved_fd) else: with target.timed_activity("Creating tarball '{}'" .format(location)): |