|
5 | 5 |
|
6 | 6 | import argparse
|
7 | 7 | import importlib
|
8 |
| -import os |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import time |
| 11 | +from pathlib import Path |
9 | 12 |
|
10 |
| -from . import get_ttp_names |
| 13 | +from . import get_ttp_list, get_ttp_names |
| 14 | +from .common import CURRENT_OS |
11 | 15 |
|
12 |
| -parser = argparse.ArgumentParser("rta") |
13 |
| -parser.add_argument("ttp_name") |
14 | 16 |
|
15 |
| -parsed_args, remaining = parser.parse_known_args() |
16 |
| -ttp_name, _ = os.path.splitext(os.path.basename(parsed_args.ttp_name)) |
| 17 | +DELAY = 1 |
17 | 18 |
|
18 |
| -if ttp_name not in get_ttp_names(): |
19 |
| - raise ValueError("Unknown RTA {}".format(ttp_name)) |
20 | 19 |
|
21 |
| -module = importlib.import_module("rta." + ttp_name) |
22 |
| -exit(module.main(*remaining)) |
| 20 | +def run_all(): |
| 21 | + """Run a single RTA.""" |
| 22 | + errors = [] |
| 23 | + for ttp_file in get_ttp_list(CURRENT_OS): |
| 24 | + print(f"---- {Path(ttp_file).name} ----") |
| 25 | + p = subprocess.Popen([sys.executable, ttp_file]) |
| 26 | + p.wait() |
| 27 | + code = p.returncode |
| 28 | + |
| 29 | + if p.returncode: |
| 30 | + errors.append((ttp_file, code)) |
| 31 | + |
| 32 | + time.sleep(DELAY) |
| 33 | + print("") |
| 34 | + |
| 35 | + return len(errors) |
| 36 | + |
| 37 | + |
| 38 | +def run(ttp_name: str, *args): |
| 39 | + """Run all RTAs compatible with OS.""" |
| 40 | + if ttp_name not in get_ttp_names(): |
| 41 | + raise ValueError(f"Unknown RTA {ttp_name}") |
| 42 | + |
| 43 | + module = importlib.import_module("rta." + ttp_name) |
| 44 | + return module.main(*args) |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == '__main__': |
| 48 | + parser = argparse.ArgumentParser("rta") |
| 49 | + parser.add_argument("--ttp-name") |
| 50 | + parser.add_argument("--run-all", action="store_true") |
| 51 | + parser.add_argument("--delay", type=int, help="For run-all, the delay between executions") |
| 52 | + parsed_args, remaining = parser.parse_known_args() |
| 53 | + |
| 54 | + if parsed_args.ttp_name and parsed_args.run_all: |
| 55 | + raise ValueError(f"Pass --ttp-name or --run-all, not both") |
| 56 | + |
| 57 | + if parsed_args.run_all: |
| 58 | + exit(run_all()) |
| 59 | + else: |
| 60 | + rta_name = Path(parsed_args.run).stem |
| 61 | + exit(run(rta_name, *remaining)) |
0 commit comments