|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import platform |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +def init_cmd_args(): |
| 7 | + import argparse |
| 8 | + parser = argparse.ArgumentParser() |
| 9 | + parser.add_argument( |
| 10 | + "--running-type", |
| 11 | + required=True, |
| 12 | + choices=["threads", "processes"], |
| 13 | + help="Specify the execution mode. Choose 'threads' for multithreading or 'processes' for multiprocessing." |
| 14 | + ) |
| 15 | + parser.add_argument( |
| 16 | + "--additional-mpi-args", |
| 17 | + required=False, |
| 18 | + default="", |
| 19 | + help="Additional MPI arguments to pass to the mpirun command (optional)." |
| 20 | + ) |
| 21 | + args = parser.parse_args() |
| 22 | + _args_dict = vars(args) |
| 23 | + return _args_dict |
| 24 | + |
| 25 | +def get_project_path(): |
| 26 | + script_path = Path(__file__).resolve() # Absolute path of the script |
| 27 | + script_dir = script_path.parent # Directory containing the script |
| 28 | + return script_dir.parent |
| 29 | + |
| 30 | +def source_script(script_path): |
| 31 | + command = f"source {script_path} && env" |
| 32 | + result = subprocess.run(command, shell=True, capture_output=True, text=True) |
| 33 | + if result.returncode == 0: |
| 34 | + # Parse the output environment variables |
| 35 | + env_vars = {} |
| 36 | + for line in result.stdout.splitlines(): |
| 37 | + if '=' in line: |
| 38 | + key, value = line.split("=", 1) |
| 39 | + env_vars[key] = value |
| 40 | + return env_vars |
| 41 | + else: |
| 42 | + print(f"Failed to source script: {script_path}") |
| 43 | + return {} |
| 44 | + |
| 45 | +def setup_env(): |
| 46 | + ocv_script_path = "build/ppc_opencv/install/bin/setup_vars_opencv4.sh" |
| 47 | + if os.path.isfile(Path(get_project_path()) / ocv_script_path): |
| 48 | + _executable_path = Path(get_project_path()) / "build/bin" |
| 49 | + env_vars = source_script(Path(get_project_path()) / ocv_script_path) |
| 50 | + else: |
| 51 | + _executable_path = Path(get_project_path()) / "install/bin" |
| 52 | + env_vars = source_script(Path(_executable_path) / "setup_vars_opencv4.sh") |
| 53 | + |
| 54 | + os.environ.update(env_vars) |
| 55 | + return _executable_path |
| 56 | + |
| 57 | +def run_exec_file(command): |
| 58 | + result = subprocess.run(command, shell=True, env=os.environ) |
| 59 | + if result.returncode != 0: |
| 60 | + raise Exception(f"Subprocess return {result.returncode}.") |
| 61 | + |
| 62 | +def get_common_gtest_settings(): |
| 63 | + return "--gtest_also_run_disabled_tests --gtest_repeat=10 --gtest_recreate_environments_when_repeating --gtest_color=0" |
| 64 | + |
| 65 | +def run_threads(): |
| 66 | + print(123) |
| 67 | + |
| 68 | +def run_common(install_bin_dir): |
| 69 | + valgrind_settings = "--error-exitcode=1 --leak-check=full --show-leak-kinds=all" |
| 70 | + |
| 71 | + if platform.system() == "Linux" and not os.environ.get("ASAN_RUN"): |
| 72 | + run_exec_file(f"valgrind {valgrind_settings} {Path(install_bin_dir) / 'core_func_tests'}") |
| 73 | + run_exec_file(f"valgrind {valgrind_settings} {Path(install_bin_dir) / 'ref_func_tests'}") |
| 74 | + |
| 75 | + run_exec_file(f"{Path(install_bin_dir) / 'core_func_tests'} {get_common_gtest_settings()}") |
| 76 | + run_exec_file(f"{Path(install_bin_dir) / 'ref_func_tests'} {get_common_gtest_settings()}") |
| 77 | + |
| 78 | +def run_processes(install_bin_dir, additional_mpi_args): |
| 79 | + proc_count = os.environ.get("PROC_COUNT") |
| 80 | + if proc_count is None: |
| 81 | + raise EnvironmentError("Required environment variable 'PROC_COUNT' is not set.") |
| 82 | + |
| 83 | + if not os.environ.get("ASAN_RUN"): |
| 84 | + run_exec_file(f"mpirun {additional_mpi_args} -np {proc_count} {Path(install_bin_dir) / 'all_func_tests'} {get_common_gtest_settings()}") |
| 85 | + run_exec_file(f"mpirun {additional_mpi_args} -np {proc_count} {Path(install_bin_dir) / 'mpi_func_tests'} {get_common_gtest_settings()}") |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + args_dict = init_cmd_args() |
| 89 | + executable_path = setup_env() |
| 90 | + run_common(executable_path) |
| 91 | + if args_dict["running_type"] == "threads": |
| 92 | + run_threads() |
| 93 | + elif args_dict["running_type"] == "processes": |
| 94 | + run_processes(executable_path, args_dict["additional_mpi_args"]) |
| 95 | + else: |
| 96 | + raise Exception("running-type is wrong!") |
0 commit comments