merge_output.py 990 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Merges stdout and stderr into a single stream with labels."""
  2. __copyright__ = """
  3. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. Exceptions. See /LICENSE for license information.
  5. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. """
  7. import subprocess
  8. import sys
  9. def label_output(label: str, output: str) -> None:
  10. """Prints output with labels.
  11. This mirrors label_output in scripts/autoupdate_testdata_base.py and should
  12. be kept in sync. They're separate in order to avoid a subprocess or import
  13. complexity.
  14. """
  15. if output:
  16. for line in output.splitlines():
  17. print(" ".join(filter(None, (label, line))))
  18. def main() -> None:
  19. p = subprocess.run(
  20. sys.argv[1:],
  21. stdout=subprocess.PIPE,
  22. stderr=subprocess.PIPE,
  23. encoding="utf-8",
  24. )
  25. label_output("STDOUT:", p.stdout)
  26. label_output("STDERR:", p.stderr)
  27. exit(p.returncode)
  28. if __name__ == "__main__":
  29. main()