|
4 | 4 | # Entrypoint for `benchcomp filter`
|
5 | 5 |
|
6 | 6 |
|
7 |
| -def main(_): |
8 |
| - raise NotImplementedError # TODO |
| 7 | +import json |
| 8 | +import logging |
| 9 | +import pathlib |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | +import tempfile |
| 13 | + |
| 14 | +import yaml |
| 15 | + |
| 16 | + |
| 17 | +def main(args): |
| 18 | + """Filter the results file by piping it into a list of scripts""" |
| 19 | + |
| 20 | + with open(args.result_file) as handle: |
| 21 | + old_results = yaml.safe_load(handle) |
| 22 | + |
| 23 | + if "filters" not in args.config: |
| 24 | + return old_results |
| 25 | + |
| 26 | + tmp_root = pathlib.Path(tempfile.gettempdir()) / "benchcomp" / "filter" |
| 27 | + tmp_root.mkdir(parents=True, exist_ok=True) |
| 28 | + tmpdir = pathlib.Path(tempfile.mkdtemp(dir=str(tmp_root))) |
| 29 | + |
| 30 | + for idx, filt in enumerate(args.config["filters"]): |
| 31 | + with open(args.result_file) as handle: |
| 32 | + old_results = yaml.safe_load(handle) |
| 33 | + |
| 34 | + json_results = json.dumps(old_results, indent=2) |
| 35 | + in_file = tmpdir / f"{idx}.in.json" |
| 36 | + out_file = tmpdir / f"{idx}.out.json" |
| 37 | + cmd_out = _pipe( |
| 38 | + filt["command_line"], json_results, in_file, out_file) |
| 39 | + |
| 40 | + try: |
| 41 | + new_results = yaml.safe_load(cmd_out) |
| 42 | + except yaml.YAMLError as exc: |
| 43 | + logging.exception( |
| 44 | + "Filter command '%s' produced invalid YAML. Stdin of" |
| 45 | + " the command is saved in %s, stdout is saved in %s.", |
| 46 | + filt["command_line"], in_file, out_file) |
| 47 | + if hasattr(exc, "problem_mark"): |
| 48 | + logging.error( |
| 49 | + "Parse error location: line %d, column %d", |
| 50 | + exc.problem_mark.line+1, exc.problem_mark.column+1) |
| 51 | + sys.exit(1) |
| 52 | + |
| 53 | + with open(args.result_file, "w") as handle: |
| 54 | + yaml.dump(new_results, handle, default_flow_style=False, indent=2) |
| 55 | + |
| 56 | + return new_results |
| 57 | + |
| 58 | + |
| 59 | +def _pipe(shell_command, in_text, in_file, out_file): |
| 60 | + """Pipe `in_text` into `shell_command` and return the output text |
| 61 | +
|
| 62 | + Save the in and out text into files for later inspection if necessary. |
| 63 | + """ |
| 64 | + |
| 65 | + with open(in_file, "w") as handle: |
| 66 | + print(in_text, file=handle) |
| 67 | + |
| 68 | + logging.debug( |
| 69 | + "Piping the contents of '%s' into '%s', saving into '%s'", |
| 70 | + in_file, shell_command, out_file) |
| 71 | + |
| 72 | + timeout = 60 |
| 73 | + with subprocess.Popen( |
| 74 | + shell_command, shell=True, text=True, stdin=subprocess.PIPE, |
| 75 | + stdout=subprocess.PIPE) as proc: |
| 76 | + try: |
| 77 | + out, _ = proc.communicate(input=in_text, timeout=timeout) |
| 78 | + except subprocess.TimeoutExpired: |
| 79 | + logging.error( |
| 80 | + "Filter command failed to terminate after %ds: '%s'", |
| 81 | + timeout, shell_command) |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | + with open(out_file, "w") as handle: |
| 85 | + print(out, file=handle) |
| 86 | + |
| 87 | + if proc.returncode: |
| 88 | + logging.error( |
| 89 | + "Filter command '%s' exited with code %d. Stdin of" |
| 90 | + " the command is saved in %s, stdout is saved in %s.", |
| 91 | + shell_command, proc.returncode, in_file, out_file) |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + return out |
0 commit comments