diff --git a/aidge_core/unit_tests/test_export.py b/aidge_core/unit_tests/test_export.py index dd9eda7a26b0e226357fe66792410f10aead6a91..6a78e912d6e87ff569bcb060d542803c66a737ef 100644 --- a/aidge_core/unit_tests/test_export.py +++ b/aidge_core/unit_tests/test_export.py @@ -15,6 +15,8 @@ import os import sys import subprocess import shutil +import queue +import threading def initFiller(model): @@ -42,6 +44,59 @@ def initFiller(model): pass +def enqueue_output(stream, queue): + for line in iter(stream.readline, ""): + queue.put(line) + stream.close() + + +def run_command(command : list[str], cwd : pathlib.Path): + """ + This function has the job to run a command and return stdout and stderr that are not shown + by subprocess : + + Arg: + command : written with the same syntax as subprocess.call + """ + process = subprocess.Popen( + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True + ) + + stdout_queue = queue.Queue() + stderr_queue = queue.Queue() + + stdout_thread = threading.Thread( + target=enqueue_output, args=(process.stdout, stdout_queue) + ) + stderr_thread = threading.Thread( + target=enqueue_output, args=(process.stderr, stderr_queue) + ) + stdout_thread.start() + stderr_thread.start() + + while ( + stdout_thread.is_alive() + or stderr_thread.is_alive() + or not stdout_queue.empty() + or not stderr_queue.empty() + ): + try: + stdout_line = stdout_queue.get_nowait() + yield stdout_line + except queue.Empty: + pass + + try: + stderr_line = stderr_queue.get_nowait() + yield stderr_line + except queue.Empty: + pass + + return_code = process.wait() + if return_code != 0: + raise subprocess.CalledProcessError(return_code, command) + + def clean_dir(dir: pathlib.Path) -> None: if not dir.is_dir(): print(f"Error : directory {dir} doesn't exist. Exiting clean_dir().") @@ -108,23 +163,43 @@ class test_export(unittest.TestCase): self.EXPORT_PATH / "main.cpp", ) - subprocess.check_call( - [ - "cmake", - str(self.EXPORT_PATH.absolute()), - "-DPYBIND=1", - f"-DCMAKE_INSTALL_PREFIX:PATH={install_path}", - ], - cwd=str(self.BUILD_DIR), - ) - subprocess.check_call( - ["cmake", "--build", "."], - cwd=str(self.BUILD_DIR), - ) - subprocess.check_call( - ["cmake", "--install", "."], - cwd=str(self.BUILD_DIR), - ) + ########################## + # CMAKE EXPORT + try: + for std_line in run_command( + [ + "cmake", + str(self.EXPORT_PATH.absolute()), + "-DPYBIND=1", + f"-DCMAKE_INSTALL_PREFIX:PATH={install_path}", + ], + cwd=str(self.BUILD_DIR), + ): + print(std_line, end="") + except subprocess.CalledProcessError as e: + print(f"An error occurred: {e}\nFailed to configure export.") + + ########################## + # BUILD EXPORT + try: + for std_line in run_command( + ["cmake", "--build", "."], + cwd=str(self.BUILD_DIR), + ): + print(std_line, end="") + except subprocess.CalledProcessError as e: + print(f"An error occurred: {e}\nFailed to build export.") + + ########################## + # INSTALL EXPORT + try: + for std_line in run_command( + ["cmake", "--install", "."], + cwd=str(self.BUILD_DIR), + ): + print(std_line, end="") + except subprocess.CalledProcessError as e: + print(f"An error occurred: {e}\nFailed to install export.") if __name__ == "__main__":