From 3f74619dda38317607c211d238911265f2efb0fe Mon Sep 17 00:00:00 2001 From: jfrery Date: Tue, 19 Mar 2024 12:33:26 +0100 Subject: [PATCH] push local refacto --- script/make_utils/run_use_case_examples.sh | 211 ++++++++++----------- 1 file changed, 97 insertions(+), 114 deletions(-) diff --git a/script/make_utils/run_use_case_examples.sh b/script/make_utils/run_use_case_examples.sh index 8561477bb6..11a9a2fa73 100755 --- a/script/make_utils/run_use_case_examples.sh +++ b/script/make_utils/run_use_case_examples.sh @@ -1,130 +1,113 @@ #!/usr/bin/env bash set -e -CML_DIR=$(pwd) -USE_CASE_REL_DIR="use_case_examples" -USE_CASE_DIR="${CML_DIR}/${USE_CASE_REL_DIR}" +current_dir=$(pwd) +use_case_dir_name="use_case_examples" +use_case_dir="${current_dir}/${use_case_dir_name}" -if [ ! -d "$USE_CASE_DIR" ]; then - echo "This script must be run in the Concrete ML source root where the '$USE_CASE_REL_DIR' directory is present" - exit 1 -fi - -echo "Running notebooks with PIP installed Concrete ML" +check_directory_exists() { + if [ ! -d "$1" ]; then + echo "Error: '$use_case_dir_name' directory must be present in the Concrete ML source root." + exit 1 + fi +} -if git ls-files --others --exclude-standard | grep -q "${USE_CASE_REL_DIR}"; then - echo "This script must be run in a clean clone of the Concrete ML repo" - echo "This directory has untracked files in ${USE_CASE_REL_DIR}" - echo "You can LIST all untracked files using: " - echo - # shellcheck disable=SC2028 - echo " git ls-files --others --exclude-standard | grep ${USE_CASE_REL_DIR}" - echo - echo "You can REMOVE all untracked files using: " - echo - # shellcheck disable=SC2028 - echo " git ls-files --others --exclude-standard | grep ${USE_CASE_REL_DIR} | xargs -0 -d '\n' --no-run-if-empty rm" - echo - exit 1 -fi - -if [[ -z "${USE_CASE}" ]]; then - # shellcheck disable=SC2207 - LIST_OF_USE_CASES=($(find "$USE_CASE_DIR/" -mindepth 1 -maxdepth 2 -type d | grep -v checkpoints)) -else - LIST_OF_USE_CASES=("${USE_CASE}") - if [ ! -d "${USE_CASE}" ]; then - echo "The use case specified to be executed, ${USE_CASE}, does not exist" - exit 1 - fi -fi - -declare -a success_examples -declare -a failed_examples - -for EXAMPLE in "${LIST_OF_USE_CASES[@]}" -do - EXAMPLE_NAME=$(basename "${EXAMPLE}") - - if [ ! -f "${EXAMPLE}/Makefile" ]; then - continue +check_clean_git_status() { + if git ls-files --others --exclude-standard | grep -q "$1"; then + echo "Error: This script must be run in a clean clone of the Concrete ML repo." + echo "Untracked files detected in $1." + echo "List untracked files with: git ls-files --others --exclude-standard | grep $1" + echo "Remove untracked files with: git clean -fdx $1" + exit 1 fi +} - echo "*** Processing example ${EXAMPLE_NAME}" +setup_virtualenv() { + local venv_path="/tmp/virtualenv_$1" + echo "Setting up virtual environment for $1..." + rm -rf "$venv_path" # Ensure a clean environment + python3 -m venv "$venv_path" + source "${venv_path}/bin/activate" + echo "Virtual environment created at $venv_path." +} - # Setup a new venv - VENV_PATH="/tmp/virtualenv_${EXAMPLE_NAME}" - if [ -d "$VENV_PATH" ]; then - echo " - VirtualEnv already exists, deleting the old one" - rm -rf "$VENV_PATH" - fi - python3 -m venv "$VENV_PATH" - echo " - VirtualEnv created at $VENV_PATH" - # shellcheck disable=SC1090,SC1091 - source "${VENV_PATH}/bin/activate" - # Install Concrete ML - cd "$CML_DIR" +install_concrete_ml() { pip install -U pip setuptools wheel - pip install -e . - hresult=$? - if [ $hresult -ne 0 ]; then - echo "Could not install Concrete ML in the virtualenv, see /tmp/log_cml_pip_${EXAMPLE_NAME}" - rm -rf "$VENV_PATH" - failed_examples+=("$EXAMPLE_NAME") - continue - fi - echo " - Concrete ML installed in $VENV_PATH" + pip install -e . || return 1 + echo "Concrete ML installed." +} - # Install example requirements - cd "$EXAMPLE" +install_requirements() { if [ -f "requirements.txt" ]; then - pip install -r requirements.txt - hresult=$? - if [ $hresult -ne 0 ]; then - echo "Could not install example requirements in the virtualenv, see /tmp/log_reqs_${EXAMPLE_NAME}" - rm -rf "$VENV_PATH" - failed_examples+=("$EXAMPLE_NAME") - continue - fi - echo " - Requirements installed in $VENV_PATH" + pip install -r requirements.txt || return 1 + echo "Requirements installed." + fi +} + +run_example() { + local example_dir=$1 + local example_name=$(basename "$example_dir") + + if [ ! -f "${example_dir}/Makefile" ]; then + return fi - + + echo "*** Processing example $example_name ***" + setup_virtualenv "$example_name" + cd "$current_dir" || return + install_concrete_ml || return + cd "$example_dir" || return + install_requirements || return + set +e - # Strip colors from the error output before piping to the log files - # Swap stderr and stdout, all output of jupyter execution is in stderr - # The information about time spent running the notebook is in stdout - # The following will pipe the stderr to the regex so that it - # ends up in the log file. - # The timing shows in the terminal - USE_CASE_DIR=$USE_CASE_DIR make 3>&2 2>&1 1>&3- | tee /dev/tty | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' - - # Neet to check the result of execution of the make command (ignore the results - # of the other commands in the pipe) - hresult="${PIPESTATUS[0]}" + USE_CASE_DIR=$use_case_dir make 3>&2 2>&1 1>&3- | tee /dev/tty | perl -pe 's/\e[^\[\]]*\[.*?[a-zA-Z]|\].*?\a//g' + local result="${PIPESTATUS[0]}" set -e - if [ "$hresult" -ne 0 ]; then - echo "Error while running example ${EXAMPLE_NAME}" - failed_examples+=("$EXAMPLE_NAME") + if [ "$result" -ne 0 ]; then + echo "Error while running example $example_name." + failed_examples+=("$example_name") else - success_examples+=("$EXAMPLE_NAME") + success_examples+=("$example_name") fi - # Remove the virtualenv - rm -rf "$VENV_PATH" -done - -# Print summary -echo -echo "Summary:" -echo "Successes: ${#success_examples[@]} examples" -for example in "${success_examples[@]}"; do - echo " - $example" -done -echo "Failures: ${#failed_examples[@]} examples" -for example in "${failed_examples[@]}"; do - echo " - $example" -done - -# Exit with a failure status if there are any failures -if [[ ${#failed_examples[@]} -gt 0 ]]; then - exit 1 -fi + deactivate + rm -rf "/tmp/virtualenv_$example_name" +} + +print_summary() { + echo + echo "Summary:" + echo "Successes: ${#success_examples[@]}" + for example in "${success_examples[@]}"; do + echo " - $example" + done + echo "Failures: ${#failed_examples[@]}" + for example in "${failed_examples[@]}"; do + echo " - $example" + done +} + +main() { + check_directory_exists "$use_case_dir" + check_clean_git_status "$use_case_dir_name" + + declare -a success_examples + declare -a failed_examples + + if [[ -z "${USE_CASE}" ]]; then + LIST_OF_USE_CASES=($(find "$use_case_dir/" -mindepth 1 -maxdepth 2 -type d | grep -v checkpoints | sort)) + else + LIST_OF_USE_CASES=("${use_case_dir}/${USE_CASE}") + fi + + for use_case in "${LIST_OF_USE_CASES[@]}"; do + run_example "$use_case" + done + + print_summary + + if [ ${#failed_examples[@]} -ne 0 ]; then + exit 1 + fi +} + +main "$@" \ No newline at end of file