Skip to content

Commit

Permalink
added implementation for run.sh. Changed setup scripts to now also cr…
Browse files Browse the repository at this point in the history
…eate venv. spelling in clean and run (#40)
  • Loading branch information
VanLaareN authored Dec 13, 2024
1 parent b675ad9 commit 4cc73b3
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 20 deletions.
42 changes: 24 additions & 18 deletions FileGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,29 @@ def generate_README(self) -> None:
except Exception as generalExcpetion:
self.logger.error(f"An unexpected error occurred: {generalExcpetion}")

def generate_run(self) -> None:
"""Generates the run.sh file"""
#TODO
pass

def generate_clean(self) -> None:
"""Generates the clean.sh file."""

def _generate_static_files(self, target: Path, name: str) -> None:
"""Generate static files from templates
:param target: target file path
:param name: name of the function"""
try:
tempalte_clean_sh = Path(__file__).parent / "templates" / "template_clean.sh"
self.logger.info("Reading in the template file for clean.sh")
tempalte = Path(__file__).parent / "templates" / f"template_{name}"
self.logger.info(f"Reading in the template file for {name}")

# Check if the template file exists
if not tempalte_clean_sh.exists():
raise FileNotFoundError(f"Template file not found: {tempalte_clean_sh}")
if not tempalte.exists():
raise FileNotFoundError(f"Template file not found: {tempalte}")

# Read the template content
template_content = tempalte_clean_sh.read_text(encoding="utf-8")

# Set the target for the clean.sh
target = self.structure.clean
template_content = tempalte.read_text(encoding="utf-8")

self.logger.info(f"Writing the template to the target: {str(target)}")

# Write content to the target file
with open(target, 'w', encoding="utf-8") as README:
README.write(template_content)
with open(target, 'w', encoding="utf-8") as template:
template.write(template_content)

self.logger.success(f"Successfully written clean.sh content to: {str(target)}")
self.logger.success(f"Successfully written {name} content to: {str(target)}")

except FileNotFoundError as fileNotFoundException:
self.logger.error(f"File not found: {fileNotFoundException}")
Expand All @@ -125,6 +120,16 @@ def generate_clean(self) -> None:
except Exception as generalExcpetion:
self.logger.error(f"An unexpected error occurred: {generalExcpetion}")
pass

def generate_run(self) -> None:
"""Generates the run.sh file"""
self._generate_static_files(target=self.structure.run,
name="run.sh")

def generate_clean(self) -> None:
"""Generates the clean.sh file."""
self._generate_static_files(target=self.structure.clean,
name="clean.sh")

def generate_adapter_config(self, target_participant: str) -> None:
"""Generates the adapter-config.json file."""
Expand Down Expand Up @@ -156,4 +161,5 @@ def generate_adapter_config(self, target_participant: str) -> None:
fileGenerator.generate_precice_config()
fileGenerator.generate_README()
fileGenerator.generate_clean()
fileGenerator.generate_run()
fileGenerator.generate_adapter_config(target_participant="Calculix")
2 changes: 2 additions & 0 deletions setup_scripts/install_dependencies.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cd setup_scripts/
git submodule update --init
python -m venv venv
.\venv\Scripts\activate
pip install -r ../requirements.txt
2 changes: 2 additions & 0 deletions setup_scripts/install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cd setup_scripts/
git submodule update --init
python -m venv venv
source venv/bin/activate
pip install -r ../requirements.txt
4 changes: 2 additions & 2 deletions templates/template_clean.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# -------------------------------------------------------------------
# Script Name: cleanup.sh
# Script Name: clean.sh
# Description: Deletes all files and directories in the current directory
# except for the hardcoded preserved files.
# Preserved files:
Expand All @@ -10,7 +10,7 @@
# - run.sh
# - config/adapter-config.json
# - config/precice-config.xml
# Usage: ./cleanup.sh [--dry-run]
# Usage: ./clean.sh [--dry-run]
# -------------------------------------------------------------------

# Exit immediately if a command exits with a non-zero status
Expand Down
92 changes: 92 additions & 0 deletions templates/template_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/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."

0 comments on commit 4cc73b3

Please sign in to comment.