File: exe_util.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (53 lines) | stat: -rw-r--r-- 1,631 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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.
"""

import subprocess
import re
import sys

# Regex for matching 7-bit and 8-bit C1 ANSI sequences.
# Credit: https://stackoverflow.com/a/14693789/4692014
_ANSI_ESCAPE_8BIT_REGEX = re.compile(
    r"""
    (?: # either 7-bit C1, two bytes, ESC Fe (omitting CSI)
        \x1B
        [@-Z\\-_]
    |   # or a single 8-bit byte Fe (omitting CSI)
        [\x80-\x9A\x9C-\x9F]
    |   # or CSI + control codes
        (?: # 7-bit CSI, ESC [
            \x1B\[
        |   # 8-bit CSI, 9B
            \x9B
        )
        [0-?]*  # Parameter bytes
        [ -/]*  # Intermediate bytes
        [@-~]   # Final byte
    )
""", re.VERBOSE)


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.

    Returns:
        The full executable output as an UTF-8 string.
    """
    # Capture stdout (where test results are written), but inherit stderr. This
    # way errors related to invalid arguments are printed normally.
    proc = subprocess.Popen(args, stdout=subprocess.PIPE)
    captured_output = b''
    while proc.poll() is None:
        buf = proc.stdout.read()
        # Write captured output directly, so escape sequences are preserved.
        sys.stdout.buffer.write(buf)
        captured_output += buf

    captured_output = _ANSI_ESCAPE_8BIT_REGEX.sub(
        '', captured_output.decode('utf-8'))

    return captured_output