|
1 | 1 | # type: ignore |
| 2 | +import argparse |
| 3 | +import itertools |
2 | 4 | import pathlib |
| 5 | +import textwrap |
3 | 6 |
|
4 | | -files = pathlib.Path("logs").rglob("**/*-log") |
5 | | -files = sorted(filter(lambda x: x.is_file(), files)) |
| 7 | +parser = argparse.ArgumentParser() |
| 8 | +parser.add_argument("filepaths", nargs="+", type=pathlib.Path) |
| 9 | +args = parser.parse_args() |
| 10 | + |
| 11 | +filepaths = sorted(p for p in args.filepaths if p.is_file()) |
| 12 | + |
| 13 | + |
| 14 | +def extract_short_test_summary_info(lines): |
| 15 | + up_to_start_of_section = itertools.dropwhile( |
| 16 | + lambda l: "=== short test summary info ===" not in l, |
| 17 | + lines, |
| 18 | + ) |
| 19 | + up_to_section_content = itertools.islice(up_to_start_of_section, 1, None) |
| 20 | + section_content = itertools.takewhile( |
| 21 | + lambda l: l.startswith("FAILED"), up_to_section_content |
| 22 | + ) |
| 23 | + content = "\n".join(section_content) |
| 24 | + |
| 25 | + return content |
| 26 | + |
| 27 | + |
| 28 | +def format_log_message(path): |
| 29 | + py_version = path.name.split("-")[1] |
| 30 | + summary = f"Python {py_version} Test Summary Info" |
| 31 | + with open(path) as f: |
| 32 | + data = extract_short_test_summary_info(line.rstrip() for line in f) |
| 33 | + message = textwrap.dedent( |
| 34 | + f"""\ |
| 35 | + <details><summary>{summary}</summary> |
| 36 | +
|
| 37 | + ``` |
| 38 | + {data} |
| 39 | + ``` |
| 40 | +
|
| 41 | + </details> |
| 42 | + """ |
| 43 | + ) |
| 44 | + |
| 45 | + return message |
6 | 46 |
|
7 | | -message = "\n" |
8 | 47 |
|
9 | 48 | print("Parsing logs ...") |
10 | | -for file in files: |
11 | | - with open(file) as fpt: |
12 | | - print(f"Parsing {file.absolute()}") |
13 | | - data = fpt.read().split("test summary info")[-1].splitlines()[1:-1] |
14 | | - data = "\n".join(data) |
15 | | - py_version = file.name.split("-")[1] |
16 | | - message = f"{message}\n<details>\n<summary>\nPython {py_version} Test Summary Info\n</summary>\n\n```bash\n{data}\n```\n</details>\n" |
| 49 | +message = "\n\n".join(format_log_message(path) for path in filepaths) |
17 | 50 |
|
18 | 51 | output_file = pathlib.Path("pytest-logs.txt") |
19 | | -with open(output_file, "w") as fpt: |
20 | | - print(f"Writing output file to: {output_file.absolute()} ") |
21 | | - fpt.write(message) |
| 52 | +print(f"Writing output file to: {output_file.absolute()}") |
| 53 | +output_file.write_text(message) |
0 commit comments