Files
seaweedfs/seaweed-block/.github/scripts/go-test-output.py
T

59 lines
1.6 KiB
Python

"""Drain a Go stream even when recording/rendering fails; never judge tests."""
import json
import os
import sys
sys.stdin.reconfigure(encoding='utf-8', errors='replace')
def warn(error):
try:
print('test evidence unavailable: '+str(error), file=sys.stderr)
except OSError:
pass
archive = None
try:
archive = open(sys.argv[1], 'a', encoding='utf-8', buffering=1)
except OSError as error:
warn(error)
raw = len(sys.argv) > 2 and sys.argv[2] == '--raw'
console = True
for line in sys.stdin:
if archive is not None:
try:
archive.write(line)
except OSError as error:
warn(error)
try:
archive.close()
except OSError:
pass
archive = None
output = line
if not raw:
try:
output = json.loads(line).get('Output', '')
except (ValueError, AttributeError) as error:
# Keep and display unexpected text, but keep draining the producer.
warn(error)
if console:
try:
sys.stdout.write(output)
sys.stdout.flush()
except OSError:
console = False
# Avoid an interpreter-exit BrokenPipe flush; the stream still
# drains Go to EOF so a dead console cannot send SIGPIPE upstream.
try:
with open(os.devnull, 'w') as sink:
os.dup2(sink.fileno(), sys.stdout.fileno())
except OSError:
pass
if archive is not None:
try:
archive.close()
except OSError as error:
warn(error)