| # Copyright 2021 The Chromium Authors |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| """Utilities for invoking executables. |
| # Regex for matching 7-bit and 8-bit C1 ANSI sequences. |
| # Credit: https://stackoverflow.com/a/14693789/4692014 |
| _ANSI_ESCAPE_8BIT_REGEX = re.compile( |
| (?: # either 7-bit C1, two bytes, ESC Fe (omitting CSI) |
| | # or a single 8-bit byte Fe (omitting CSI) |
| | # or CSI + control codes |
| [ -/]* # Intermediate bytes |
| def run_and_tee_output(args): |
| """Runs the test executable passing-thru its output to stdout (in a |
| terminal-colors-friendly way). Waits for the executable to exit. |
| The full executable output as an UTF-8 string. |
| output_bytes = subprocess.check_output(args) |
| # Strip ANSI / terminal escapes. |
| output_bytes = _ANSI_ESCAPE_8BIT_REGEX.sub(b'', output_bytes) |
| return output_bytes.decode('utf-8') |