()
| 95 | |
| 96 | |
| 97 | def main(): |
| 98 | parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) |
| 99 | sub = parser.add_subparsers(dest="command", required=True) |
| 100 | s = sub.add_parser("suites") |
| 101 | s.add_argument("answer") |
| 102 | f = sub.add_parser("filter") |
| 103 | f.add_argument("answer") |
| 104 | f.add_argument("--lane", required=True) |
| 105 | f.add_argument("--out", required=True) |
| 106 | f.add_argument("--optional-out") |
| 107 | args = parser.parse_args() |
| 108 | if args.command == "suites": |
| 109 | # LF on every host, like the files below: a Windows python would |
| 110 | # otherwise write CRLF. |
| 111 | sys.stdout.reconfigure(newline="\n") |
| 112 | with open(args.answer, encoding="utf-8") as handle: |
| 113 | print("\n".join(suites(json.load(handle)))) |
| 114 | return 0 |
| 115 | for stale in (args.out, args.optional_out): |
| 116 | if stale and os.path.exists(stale): |
| 117 | os.remove(stale) |
| 118 | try: |
| 119 | with open(args.answer, encoding="utf-8") as handle: |
| 120 | answer = json.load(handle) |
| 121 | except (OSError, ValueError) as error: |
| 122 | return run_all(f"unreadable answer ({error})") |
| 123 | if not isinstance(answer, dict): |
| 124 | return run_all("the answer is not an object") |
| 125 | code, lines = lane_filter(answer, args.lane) |
| 126 | if code in (NARROWED, NOTHING): |
| 127 | with open(args.out, "w", encoding="utf-8", newline="\n") as handle: |
| 128 | handle.write("".join(line + "\n" for line in lines)) |
| 129 | if args.optional_out: |
| 130 | optional = conditional_suites(answer, args.lane) if code == NARROWED else [] |
| 131 | with open(args.optional_out, "w", encoding="utf-8", newline="\n") as handle: |
| 132 | handle.write("".join(suite + "\n" for suite in optional)) |
| 133 | return code |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
no test coverage detected