Skip to content

Commit

Permalink
remove old method for writing to build script
Browse files Browse the repository at this point in the history
update docstrings in few methods
  • Loading branch information
shahzebsiddiqui committed Oct 23, 2024
1 parent 5a2dbc7 commit 20925d3
Showing 1 changed file with 29 additions and 101 deletions.
130 changes: 29 additions & 101 deletions buildtest/builders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ def execute_run(self, cmd, timeout):
return command

def execute_post_run_script(self):

"""This method will execute the post run script which is invoked after test is complete. This is called
if ``post_run`` is defined in buildspec.
"""
if os.path.exists(self.post_run_script):
post_run = BuildTestCommand(self.post_run_script)
post_run.execute()
Expand All @@ -389,7 +391,9 @@ def execute_post_run_script(self):
self._display_output_content(error, title="Start of Post Run Error")

def _display_output_content(self, output, title, show_last_lines=10):
"""This method will display content of output or error results.
"""This method will display content of output or error results. The ``output`` is content of file to display. A title
is displayed at top which can be customized via ``title``. We can configure number of lines to display from end of file via
```show_last_lines``.
Args:
output (str): Output content to display
Expand All @@ -406,10 +410,29 @@ def _display_output_content(self, output, title, show_last_lines=10):
show_last_lines=show_last_lines,
)

def _display_test_content(self, filepath, title) -> None:
"""Display content of test file, given a path to file and title to display.
Args:
filepath (str): Path to file to display content
title (str): Title to display before content
"""

if "test" in self.display:
print_file_content(
file_path=filepath,
title=f"[blue]{self}[/]: {title}",
lexer="bash",
theme="monokai",
)

def handle_run_result(self, command_result, timeout):
"""This method will handle the result of running test. If the test is successful we will record endtime,
copy output and error file to test directory and set state to complete. If the test fails we will retry the test based on retry count.
If the test fails after retry we will mark test as failed.
Args:
command_result (BuildTestCommand): An instance object of BuildTestCommand
timeout (int): Timeout value for test run
"""
launch_command = command_result.get_command()
self.logger.debug(f"Running Test via command: {launch_command}")
Expand Down Expand Up @@ -562,18 +585,11 @@ def _write_build_script(
) -> None:
"""Write the content of build script."""
lines = self._generate_build_script_lines(modules, modulepurge, unload_modules)
write_file(self.build_script, "\n".join(lines))
lines = "\n".join(lines)
write_file(self.build_script, lines)
self._set_execute_perm(self.build_script)
self._copy_build_script_to_test_root()

def _display_test_content(self, filepath, title) -> None:
if "test" in self.display:
print_file_content(
file_path=filepath,
title=f"[blue]{self}[/]: {title}",
lexer="bash",
theme="monokai",
)
self.metadata["buildscript_content"] = lines

def _generate_build_script_lines(
self, modules: List[str], modulepurge: bool, unload_modules: List[str]
Expand Down Expand Up @@ -636,90 +652,6 @@ def _copy_build_script_to_test_root(self) -> None:
self.build_script = dest
self.metadata["build_script"] = self.build_script

def _write_build_script1(self, modules=None, modulepurge=None, unload_modules=None):
"""This method will write the content of build script that is run for when invoking
the builder run method. Upon creating file we set permission of builder script to 755
so test can be run.
"""

lines = ["#!/bin/bash"]

trap_msg = """
# Function to handle all signals and perform cleanup
function cleanup() {
echo "Signal trapped. Performing cleanup before exiting."
exitcode=$?
echo "buildtest: command '$BASH_COMMAND' failed (exit code: $exitcode)"
exit $exitcode
}
# Trap all signals and call the cleanup function
trap cleanup SIGINT SIGTERM SIGHUP SIGQUIT SIGABRT SIGKILL SIGALRM SIGPIPE SIGTERM SIGTSTP SIGTTIN SIGTTOU
"""
lines.append(trap_msg)
lines += self._set_default_test_variables()
lines.append("# source executor startup script")

if modulepurge:
lines.append("module purge")

if unload_modules:
lines.append("# Specify list of modules to unload")
for module in unload_modules.split(","):
lines.append(f"module unload {module}")

if modules:
lines.append("# Specify list of modules to load")
for module in modules.split(","):
lines.append(f"module load {module}")

lines += [
f"source {os.path.join(BUILDTEST_EXECUTOR_DIR, self.executor, 'before_script.sh')}"
]

lines.append("# Run generated script")
# local executor
if self.is_local_executor():
lines += [" ".join(self._emit_command())]
elif self.is_container_executor():
lines += self.get_container_invocation()
# batch executor
else:
launcher = self.buildexecutor.executors[self.executor].launcher_command(
numprocs=self.numprocs, numnodes=self.numnodes
)
lines += [" ".join(launcher) + " " + f"{self.testpath}"]

lines.append("# Get return code")

# get returncode of executed script which is retrieved by '$?'
lines.append("returncode=$?")

lines.append("# Exit with return code")
lines.append("exit $returncode")

lines = "\n".join(lines)
write_file(self.build_script, lines)
self.metadata["buildscript_content"] = lines
self.logger.debug(f"Writing build script: {self.build_script}")
self._set_execute_perm(self.build_script)

# copying build script into test_root directory since stage directory will be removed
dest = os.path.join(self.test_root, os.path.basename(self.build_script))
shutil.copy2(self.build_script, dest)
self.logger.debug(f"Copying build script to: {dest}")

self.build_script = dest
self.metadata["build_script"] = self.build_script

if "test" in self.display:
print_file_content(
file_path=self.build_script,
title=f"[blue]{self}[/]: Start of Build Script",
lexer="bash",
theme="monokai",
)

def _write_post_run_script(self):
"""This method will write the content of post run script that is run after the test is complete.
The post run script is used to perform cleanup operations after test is complete.
Expand Down Expand Up @@ -753,19 +685,15 @@ def _write_test(self):

# Implementation to write file generate.sh
lines = []

lines += self.generate_script()

lines = "\n".join(lines)

self.logger.info(f"Opening Test File for Writing: {self.testpath}")

write_file(self.testpath, lines)

self.metadata["test_content"] = lines

self._set_execute_perm(self.testpath)
# copy testpath to run_dir
# copy testpath to root test directory
shutil.copy2(
self.testpath, os.path.join(self.test_root, os.path.basename(self.testpath))
)
Expand Down

0 comments on commit 20925d3

Please sign in to comment.