run_vadere_console_with_all_scenario_files.py 1.98 KB
Newer Older
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
# Use "vadere-console.jar", which is created by "mvn package", to run all
# scenario files under "VadereModelTests" subdirectory.
#
# Note: script contains some print statements so that progress can be tracked
# a little bit

# Wach out: call this script from root directory of project. E.g.
#
#   python Tools/my_script.py

import fnmatch
import os
import shutil
import subprocess

def find_scenario_files(path="VadereModelTests"):
    scenario_search_pattern = "*.scenario"
    scenario_files = []

    for root, dirnames, filenames in os.walk(path):
        for filename in fnmatch.filter(filenames, scenario_search_pattern):
            scenario_path = os.path.join(root, filename)
            scenario_files.append(scenario_path)

    print("Total scenario files: {}".format(len(scenario_files)))

    return scenario_files

def run_scenario_files_with_vadere_console(scenario_files, vadere_console="VadereGui/target/vadere-console.jar", scenario_timeout_in_sec=60):
    output_dir = "output"

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

35
36
37
38
    total_scenario_files = len(scenario_files)

    for i, scenario_file in enumerate(scenario_files):
        print("Running scenario file ({}/{}): {}".format(i + 1, total_scenario_files, scenario_file))
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        # Use timout feature, check return value and capture stdout/stderr to a PIPE (use completed_process.stdout to get it).
        completed_process = subprocess.run(args=["java", "-jar", vadere_console, scenario_file, output_dir],
                                       timeout=scenario_timeout_in_sec,
                                       check=True,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
        print("Finished scenario file: {}".format(scenario_file))

    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)

if __name__ == "__main__":
    scenario_files = find_scenario_files()
    run_scenario_files_with_vadere_console(scenario_files)