Skip to content

Commit

Permalink
Utility: Introduce command repeater script
Browse files Browse the repository at this point in the history
Introduce a command repeater script to help run a set of commands
repeatedly.
  • Loading branch information
bkhouri committed Jan 16, 2025
1 parent 0c6acf3 commit 7cade49
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions Utilities/repeat_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash -e

cmd=(
"git log -n1"
# Insert additional command to repeat
"time swift test --parallel"
)
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DRY_RUN_COMMAND=""
NUM_ITERATIONS=200


function printUsage() {
echo ""
echo "Execute a set of commands repeatedly. The script must be modified to include"
echo "the commands to be repeated"
echo ""
echo "${0} [OPTIONS]"
echo ""
echo "Options:"
echo " -n | --num-iterations <NUM> The number of iterations to runs the set of commands (Default: ${NUM_ITERATIONS})"
echo " -l | --logs-dir <DIR> The directory to store the logs files "
echo " --dryrun When set, echo the commands that will be executed"
echo ""
}

while [[ $# > 0 ]]; do
case $1 in
--num-iterations|-n)
shift
NUM_ITERATIONS=${1}
shift
;;
--logs-dir|-l)
shift
ROOT_LOG_DIR="${1}"
echo "override root log dir: ${ROOT_LOG_DIR}"
mkdir -p "${ROOT_LOG_DIR}"
shift
;;
--help|-h)
printUsage
exit 1
;;
--dry-run)
DRY_RUN_COMMAND=$(command -v echo)
shift
;;
*)
# unknown option
echo "Unknown Option: $1"
printUsage
exit 1
;;
esac
done


# Ensure the root log directory is set and defined
if [ -z "${ROOT_LOG_DIR}" ] ; then
custom_run=$(date -u +'%Y%m%dT%H%M%S%Z')
ROOT_LOG_DIR=$(mktemp -d "/tmp/swiftpm_repeater_script_${custom_run}")
fi
failed_dir="${ROOT_LOG_DIR}/failed"

caffeinate_cmd=$(command -v caffeinate)
prefix_command="${DRY_RUN_COMMAND} ${caffeinate_cmd}"
function cleanup() {
echo ""
echo ""
echo "******************************************************"
echo "Root log directory : ${ROOT_LOG_DIR}"
echo "Failed log directory: ${failed_dir}"
}


trap cleanup EXIT
set -x
mkdir -p ${failed_dir}


# for num in $(seq -w 2);
echo "Executing : ${cmd}"
echo "Root Log Directory: ${ROOT_LOG_DIR}"
set -x
which swift
xcode-select -p
swift --version
set +x
for num in $(seq -w $NUM_ITERATIONS);
do
(
cd ${BASEDIR}
log_dir=${ROOT_LOG_DIR}/${num}
message="[${num}/${NUM_ITERATIONS}] executing and writing log to ${log_dir}"
echo "${message} ..."
mkdir -p ${log_dir}
test_log=${log_dir}/swift_test_console_${num}.txt
rm -rf ${test_log}
start_time=$(date +%s)
for ((i = 0; i < ${#cmd[@]}; i++))
do
full_cmd="${prefix_command} ${cmd[${i}]}"
echo "❯❯❯ Executing: ${full_cmd}" >> ${test_log}
# set -x
# caffeinate ${cmd} 2>&1 | tee -a "${test_log}"
${full_cmd} >> "${test_log}" 2>&1
rc=$?
# set +x
if [ ${rc} -ne 0 ] ; then
# if [ ${PIPESTATUS[0]} -ne 0 ] ; then
ln -s ${log_dir} ${failed_dir}
fi

done
end_time=$(date +%s)
echo "${message} completed in $(( ($end_time - $start_time) )) seconds"

sleep 0.5
)
done
echo "Done all ${NUM_ITERATIONS} interations"

0 comments on commit 7cade49

Please sign in to comment.