Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted for new run.sh #59

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions FileGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def _generate_README(self) -> None:
self._generate_static_files(target=self.structure.README,
name="README.md")

def _generate_run(self) -> None:
"""Generates the run.sh file"""
self._generate_static_files(target=self.structure.run,
def _generate_run(self, run_sh: Path) -> None:
"""Generates the run.sh file
:param run_sh: Path to the run.sh file"""
self._generate_static_files(target=run_sh,
name="run.sh")

def _generate_clean(self) -> None:
Expand Down Expand Up @@ -139,7 +140,9 @@ def generate_level_1(self) -> None:
for participant in participants:
target_participant = self.structure.create_level_1_structure(participant)
adapter_config = target_participant[1]
run_sh = target_participant[2]
self._generate_adapter_config(target_participant=participant, adapter_config=adapter_config)
self._generate_run(run_sh)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Takes topology.yaml files as input and writes out needed files to start the precice.")
Expand Down
8 changes: 4 additions & 4 deletions generation_utils/StructureHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def create_level_1_structure(self, participant: str) -> list[Path]:
self.logger.success(f"Created file: {adapter_config}")

# Create the run.sh file
run = participant_folder / "run.sh"
run.touch(exist_ok=True)
self.logger.success(f"Created file: {run}")
self.run = participant_folder / "run.sh"
self.run.touch(exist_ok=True)
self.logger.success(f"Created file: {self.run}")

return [participant_folder, adapter_config, run]
return [participant_folder, adapter_config, self.run]
except Exception as create_participant_folder_exception:
self.logger.error(f"Failed to create folder/file for participant: {participant_folder}. Error: {create_participant_folder_exception}")

Expand Down
110 changes: 19 additions & 91 deletions templates/template_run.sh
Original file line number Diff line number Diff line change
@@ -1,92 +1,20 @@
#!/bin/bash

# -------------------------------------------------------------------
# Script Name: run.sh
# Description: Takes two Python script files as arguments and starts them.
# Usage: ./run.sh path/to/script1.py path/to/script2.py
# -------------------------------------------------------------------

# Exit immediately if a command exits with a non-zero status
set -e

# Function to display usage information
usage() {
echo "Usage: $0 path/to/script1.py path/to/script2.py"
echo "Example: $0 ./script1.py ./script2.py"
exit 1
}

# Check if exactly two arguments are provided
if [ "$#" -ne 2 ]; then
echo "Error: Exactly two arguments are required."
usage
fi

# Assign arguments to variables
SCRIPT1="$1"
SCRIPT2="$2"

# Function to check if a file exists and is a Python script
check_script() {
local script="$1"
if [ ! -f "$script" ]; then
echo "Error: File '$script' does not exist."
exit 1
fi
if [[ "$script" != *.py ]]; then
echo "Warning: File '$script' does not have a .py extension."
fi
}

# Check both scripts
check_script "$SCRIPT1"
check_script "$SCRIPT2"

# Determine which python command to use
# Prefer 'python3' if available
if command -v python3 >/dev/null 2>&1; then
PYTHON_CMD="python3"
elif command -v python >/dev/null 2>&1; then
PYTHON_CMD="python"
else
echo "Error: Python is not installed or not in PATH."
exit 1
fi

echo "Using Python interpreter: $PYTHON_CMD"

# Define log file names based on script names
SCRIPT1_LOG="$(basename "$SCRIPT1" .py).log"
SCRIPT2_LOG="$(basename "$SCRIPT2" .py).log"

# Start the first Python script in the background and redirect output
echo "Starting '$SCRIPT1'..."
$PYTHON_CMD "$SCRIPT1" > "$SCRIPT1_LOG" 2>&1 &
PID1=$!
echo "'$SCRIPT1' started with PID $PID1. Output redirected to '$SCRIPT1_LOG'."

# Start the second Python script in the background and redirect output
echo "Starting '$SCRIPT2'..."
$PYTHON_CMD "$SCRIPT2" > "$SCRIPT2_LOG" 2>&1 &
PID2=$!
echo "'$SCRIPT2' started with PID $PID2. Output redirected to '$SCRIPT2_LOG'."

# Wait for both scripts to finish
echo "Waiting for both Python scripts to finish..."
wait $PID1
STATUS1=$?
if [ $STATUS1 -ne 0 ]; then
echo "Error: '$SCRIPT1' exited with status $STATUS1."
else
echo "'$SCRIPT1' completed successfully."
fi

wait $PID2
STATUS2=$?
if [ $STATUS2 -ne 0 ]; then
echo "Error: '$SCRIPT2' exited with status $STATUS2."
else
echo "'$SCRIPT2' completed successfully."
fi

echo "Both Python scripts have completed."
#
# run.sh Script
#
# This is a template. You need to implement it yourself.
#
# If you are trying to launch a Python script, you need to add:
#
# python path/to/file.py
#
# Example: https://github.com/precice/tutorials/blob/develop/flow-over-heated-plate/solid-dunefem/run.sh
#
# Note: In the example `run.sh` file, most setup steps (such as creating and activating a virtual environment
# or installing dependencies) have already been completed.
# Therefore, you may only need to add:
#
# python path/to/file.py
#

set -e # Exit immediately if any command fails
Loading