From 9cb88d1fe9af549f59d895025925f40ad702c94b Mon Sep 17 00:00:00 2001 From: Janggun Lee Date: Thu, 27 Feb 2025 14:13:05 +0900 Subject: [PATCH] Move install_csmith into a separate script --- tests/fuzz.py | 53 ++------------------------------------- tests/install_smith.py | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 51 deletions(-) create mode 100644 tests/install_smith.py diff --git a/tests/fuzz.py b/tests/fuzz.py index b6d7777..320ad50 100644 --- a/tests/fuzz.py +++ b/tests/fuzz.py @@ -14,6 +14,8 @@ import re import random from pathlib import Path +from install_smith import install_csmith + REPLACE_DICT = { "volatile ": "", "static ": "", @@ -51,57 +53,6 @@ CSMITH_DIR = "csmith-2.3.0" SKIP_TEST = 102 -def execute_command(command, cwd=None): - try: - process = subprocess.Popen( - command, shell=True, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - stdout, stderr = process.communicate() - - if process.returncode != 0: - raise subprocess.CalledProcessError( - process.returncode, command, stderr.decode("utf-8") - ) - print(stdout.decode("utf-8")) - return True - except subprocess.CalledProcessError as e: - print(f"Error executing command: {e.cmd}") - print(e.output) - return False - except Exception as e: - print(f"Unexpected error: {str(e)}") - return False - - -def install_csmith(): - """ - Installation based on the provided in the github repos - of the package. Make sure to have sudo privileges. - """ - usr_bin_path = "/usr/local/bin/csmith" - usr_inc_path = "/usr/local/include/" # cmake dumps the include files here. - if os.path.exists(usr_bin_path): - return usr_bin_path, usr_inc_path - - csmith_dir = os.path.join(os.getcwd(), "csmith") - if not os.path.exists(csmith_dir): - if not execute_command( - "git clone https://github.com/csmith-project/csmith.git" - ): - raise Exception("Unable to clone the Csmith repository") - - if not execute_command("sudo apt install -y g++ cmake m4", cwd=csmith_dir): - raise Exception("Unable to install dependencies") - - cmake_command = f"cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ ." - if not execute_command(cmake_command, cwd=csmith_dir): - raise Exception("Unable to run cmake.") - - if not execute_command("make && sudo make install", cwd=csmith_dir): - raise Exception("Unable to install.") - return usr_bin_path, usr_inc_path - - def generate(tests_dir, bin_path, seed, easy): """Feeding options to built Csmith to randomly generate test case. diff --git a/tests/install_smith.py b/tests/install_smith.py new file mode 100644 index 0000000..a66aeba --- /dev/null +++ b/tests/install_smith.py @@ -0,0 +1,56 @@ +import subprocess, os + + +def execute_command(command, cwd=None): + try: + process = subprocess.Popen( + command, shell=True, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + stdout, stderr = process.communicate() + + if process.returncode != 0: + raise subprocess.CalledProcessError( + process.returncode, command, stderr.decode("utf-8") + ) + print(stdout.decode("utf-8")) + return True + except subprocess.CalledProcessError as e: + print(f"Error executing command: {e.cmd}") + print(e.output) + return False + except Exception as e: + print(f"Unexpected error: {str(e)}") + return False + + +def install_csmith(): + """ + Installation based on the provided in the github repos + of the package. Make sure to have sudo privileges. + """ + usr_bin_path = "/usr/local/bin/csmith" + usr_inc_path = "/usr/local/include/" # cmake dumps the include files here. + if os.path.exists(usr_bin_path): + return usr_bin_path, usr_inc_path + + csmith_dir = os.path.join(os.getcwd(), "csmith") + if not os.path.exists(csmith_dir): + if not execute_command( + "git clone https://github.com/csmith-project/csmith.git" + ): + raise Exception("Unable to clone the Csmith repository") + + if not execute_command("sudo apt install -y g++ cmake m4", cwd=csmith_dir): + raise Exception("Unable to install dependencies") + + cmake_command = f"cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ ." + if not execute_command(cmake_command, cwd=csmith_dir): + raise Exception("Unable to run cmake.") + + if not execute_command("make && sudo make install", cwd=csmith_dir): + raise Exception("Unable to install.") + return usr_bin_path, usr_inc_path + + +if __name__ == "__main__": + install_csmith()