forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
65 lines (53 loc) · 1.98 KB
/
utils.py
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
54
55
56
57
58
59
60
61
62
63
64
65
# Lint as: python3
# Copyright 2020 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# pylint: disable=missing-docstring
import argparse
import os
import re
import subprocess
from typing import Sequence
def create_markdown_table(rows: Sequence[Sequence[str]]):
"""Converts a 2D array to a Markdown table."""
return '\n'.join([' | '.join(row) for row in rows])
def check_and_get_output_lines(command: Sequence[str],
dry_run: bool = False,
log_stderr: bool = True):
print(f'Running: `{" ".join(command)}`')
if dry_run:
return None, None
process = subprocess.run(
command,
# TODO(#4131) python>=3.7: Use capture_output=True.
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
# TODO(#4131) python>=3.7: Replace 'universal_newlines' with 'text'.
universal_newlines=True)
if log_stderr:
for line in process.stderr.splitlines():
print(line)
process.check_returncode()
return process.stdout.splitlines()
def get_test_targets(test_suite_path: str):
"""Returns a list of test targets for the given test suite."""
# Check if the suite exists (which may not be true for failing suites).
# We use two queries here because the return code for a failed query is
# unfortunately the same as the return code for a bazel configuration error.
target_dir = test_suite_path.split(':')[0]
query = [
'bazel', 'query', '--ui_event_filters=-DEBUG',
'--noshow_loading_progress', '--noshow_progress', f'{target_dir}/...'
]
targets = check_and_get_output_lines(query)
if test_suite_path not in targets:
return []
query = [
'bazel', 'query', '--ui_event_filters=-DEBUG',
'--noshow_loading_progress', '--noshow_progress',
f'tests({test_suite_path})'
]
tests = check_and_get_output_lines(query)
return tests