From 2fb8d3891e6eb2bd175f43e9304ae15c478c1ff9 Mon Sep 17 00:00:00 2001 From: Dor Harpaz Date: Thu, 19 Nov 2026 17:14:15 +0200 Subject: [PATCH 01/11] refactor update_outputs into functions, and fix its bugs - split the scripts into functions, and move helpers into utils/ and patch_notebook/ - validate venv and mode before touching git / pip - fix the venv check, which never failed - fix `>/dev/stdout` truncating logs that are redirected to a file - list the notebooks that failed at the end of the run - fix `update_links.py --force` crashing Co-Authored-By: Claude Opus 5.5 --- .../{ => patch_notebook}/injected_cells.py | 0 .../{ => patch_notebook}/update_links.py | 8 +- .internal/update_outputs/update_notebooks.sh | 253 +++++++++++------- .../update_outputs/update_single_notebook.sh | 188 ++++++------- .internal/update_outputs/utils/common.sh | 44 +++ .../{ => utils}/fake_browser.sh | 0 6 files changed, 282 insertions(+), 211 deletions(-) rename .internal/update_outputs/{ => patch_notebook}/injected_cells.py (100%) rename .internal/update_outputs/{ => patch_notebook}/update_links.py (92%) create mode 100644 .internal/update_outputs/utils/common.sh rename .internal/update_outputs/{ => utils}/fake_browser.sh (100%) diff --git a/.internal/update_outputs/injected_cells.py b/.internal/update_outputs/patch_notebook/injected_cells.py similarity index 100% rename from .internal/update_outputs/injected_cells.py rename to .internal/update_outputs/patch_notebook/injected_cells.py diff --git a/.internal/update_outputs/update_links.py b/.internal/update_outputs/patch_notebook/update_links.py similarity index 92% rename from .internal/update_outputs/update_links.py rename to .internal/update_outputs/patch_notebook/update_links.py index bb12d6e3d..fc0c4d20d 100755 --- a/.internal/update_outputs/update_links.py +++ b/.internal/update_outputs/patch_notebook/update_links.py @@ -52,19 +52,19 @@ def validate_links_list( ) -> bool: # verify list length if len(old_links) != len(new_links): - print("Error: Link count mismatch in force mode") + print("Error: Link count mismatch") return False if force: # verify cell index - old_indices = [i for i, _ in old_links] - new_indices = [i for i, _ in new_links] + old_indices = [cell_index for cell_index, _, _ in old_links] + new_indices = [cell_index for cell_index, _, _ in new_links] if old_indices != new_indices: print("Error: Link cell indices mismatch") return False # verify link pattern - if not all(LINK_PATTERN.match(link) for _, link in new_links): + if not all(LINK_PATTERN.match(link) for _, _, link in new_links): print("Error: Not all new links match expected pattern") return False diff --git a/.internal/update_outputs/update_notebooks.sh b/.internal/update_outputs/update_notebooks.sh index 7b3895732..e4ec87dd9 100755 --- a/.internal/update_outputs/update_notebooks.sh +++ b/.internal/update_outputs/update_notebooks.sh @@ -3,129 +3,174 @@ # all this script does is wrap the line: # find . -type f -name "*.ipynb" | xargs -P3 -I{} jupyter nbconvert --to notebook --execute --inplace {} -if [[ " $* " == *" --help "* ]]; then - echo "usage: $0" - echo " plus, there are some environment variables which you can set" - echo " UPDATER_SILENCE_PIP - can be set to '1' or 'true'" - echo " UPDATER_MAX_THREADS - any number (recommending 1, 2, or 3)" - echo " UPDATER_MODE - can be set to 'test_small', 'test_algorithms', 'test_all'" - echo " UPDATER_FOLDER - can be set to a folder, on which we run 'find ." - echo - echo "example usage:" - echo "UPDATER_MAX_THREADS=1 UPDATER_SILENCE_PIP=1 UPDATER_FOLDER=algorithms/algebraic .internal/update_outputs/update_notebooks.sh" +SCRIPT_DIR="$(realpath "$(dirname "$0")")" +source "$SCRIPT_DIR/utils/common.sh" + +MAX_THREADS=${UPDATER_MAX_THREADS:-3} # note: there's a max of 3 running jobs per user, so we limit the threads to 3 +MODE=${UPDATER_MODE:-} +FOLDER=${UPDATER_FOLDER:-} + +print_help() { + cat <' + UPDATER_MODE - can be set to 'test_small', 'test_algorithms', 'test_all' + UPDATER_MAX_THREADS - any number (recommending 1, 2, or 3). default: 3 + UPDATER_LINKS_ONLY - only update the platform links, keeping the rest of the outputs. default: true + UPDATER_TRACK_TIME - print when each notebook starts and ends. default: true + UPDATER_SILENCE_GIT - silence 'git pull'. default: false + UPDATER_SILENCE_PIP - silence 'pip install'. default: false + booleans can be set to '1' / 'true' or '0' / 'false' (case-insensitive) + +example usage: +UPDATER_MAX_THREADS=1 UPDATER_SILENCE_PIP=1 UPDATER_FOLDER=algorithms/algebraic .internal/update_outputs/update_notebooks.sh + +UPDATER_MAX_THREADS=1 UPDATER_SILENCE_PIP=true UPDATER_TRACK_TIME=false UPDATER_MODE=test_small .internal/update_outputs/update_notebooks.sh + +UPDATER_MAX_THREADS=1 UPDATER_SILENCE_PIP=1 UPDATER_LINKS_ONLY=0 UPDATER_FOLDER=algorithms/algebraic .internal/update_outputs/update_notebooks.sh +EOF +} + +print_section() { echo - echo "UPDATER_MAX_THREADS=true UPDATER_SILENCE_PIP=true UPDATER_TRACK_TIME=false UPDATER_MODE=test_small .internal/update_outputs/update_notebooks.sh" echo - echo "UPDATER_MAX_THREADS=1 UPDATER_SILENCE_PIP=1 UPDATER_LINKS_ONLY=0 UPDATER_FOLDER=algorithms/algebraic .internal/update_outputs/update_notebooks.sh" - exit 0 -fi - -# -# 1) Initialization - `git pull` + create-new-branch -# -echo "======= Init =======" -cd "$(git rev-parse --show-toplevel)" - -if [ "$UPDATER_SILENCE_GIT" = "true" ] || [ "$UPDATER_SILENCE_GIT" = "1" ]; then - out_git="/dev/null" -else - out_git="/dev/stdout" -fi - -git checkout main -git pull >"$out_git" - -echo -git checkout -b "updating_notebooks_$(date '+%Y.%m.%d_%H.%M')" -echo - -# is outside venv; return 0 = outside venv; return 1 = inside venv -python -c "import sys; sys.exit(not hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))" -if [ $? -eq 0 ]; then - echo "Please enter venv" - exit 1 -fi - - -echo "[*] Updating pip" -if [ "$UPDATER_SILENCE_PIP" = "true" ] || [ "$UPDATER_SILENCE_PIP" = "1" ]; then - out_pip="/dev/null" -else - out_pip="/dev/stdout" -fi -start=$(date +%s) - -python -m pip install -U -r requirements.txt -r requirements_notebooks.txt >"$out_pip" - -end=$(date +%s) -elapsed=$((end - start)) -# format hh:mm:ss -printf -v duration "%02d:%02d:%02d" $((elapsed/3600)) $((elapsed%3600/60)) $((elapsed%60)) -printf "[*] Updating pip - done : took %s\n" "$duration" + echo "======= $1 =======" +} # -# 2) Update the notebooks +# 1) Initialization - validations, `git pull` + create-new-branch, update pip # -echo -echo -echo "======= Updating notebooks =======" - -current_folder="$(realpath "$(dirname "$0")")" - -# Set default values -MAX_THREADS=${UPDATER_MAX_THREADS:-3} # note: there's a max of 3 running jobs per user, so we limit the threads to 3 -MODE=${UPDATER_MODE:-} -FOLDER=${UPDATER_FOLDER:-} +assert_inside_venv() { + # `sys.prefix` differs from `sys.base_prefix` only inside a venv (`python -m venv` / `uv venv`) + if ! python -c "import sys; sys.exit(sys.prefix == sys.base_prefix)"; then + echo "Please enter venv" >&2 + return 1 + fi +} + +# sets `search_root` and `name_pattern`, by which we `find` the notebooks to update +resolve_notebooks_scope() { + if [[ -n "$FOLDER" ]]; then + search_root="$FOLDER" + name_pattern="*.ipynb" + return 0 + fi -# Determine the list of files to process -if [[ -n "$FOLDER" ]]; then - FIND_CMD="find \"$FOLDER\" -type f -name \"*.ipynb\"" -else case "$MODE" in test_small) - FIND_CMD='find . -type f -name "*bernstein*.ipynb"' + search_root="." + name_pattern="*bernstein*.ipynb" ;; test_algorithms) - FIND_CMD='find algorithms/ -type f -name "*.ipynb"' + search_root="algorithms/" + name_pattern="*.ipynb" ;; test_all) - FIND_CMD='find . -type f -name "*.ipynb"' + search_root="." + name_pattern="*.ipynb" ;; *) - echo "Error: Either UPDATER_FOLDER must be set or UPDATER_MODE must be one of: test_small, test_algorithms, test_all." - exit 1 + echo "Error: Either UPDATER_FOLDER must be set or UPDATER_MODE must be one of: test_small, test_algorithms, test_all." >&2 + return 1 ;; esac -fi +} + +create_branch() { + local out_git=/dev/stdout + is_true "$UPDATER_SILENCE_GIT" false && out_git=/dev/null + + git checkout main + git pull >>"$out_git" + + echo + git checkout -b "updating_notebooks_$(date '+%Y.%m.%d_%H.%M')" + echo +} -if [ "$UPDATER_LINKS_ONLY" = "false" ] || [ "$UPDATER_LINKS_ONLY" = "0" ]; then - links_only_flag="" -else - links_only_flag="--links-only" -fi +update_requirements() { + local out_pip=/dev/stdout + is_true "$UPDATER_SILENCE_PIP" false && out_pip=/dev/null -# Execute the command -eval "$FIND_CMD" | xargs -P"$MAX_THREADS" -I{} "$current_folder/update_single_notebook.sh" "{}" "$links_only_flag" + echo "[*] Updating pip" + local start end + start=$(date +%s) + + python -m pip install -U -r requirements.txt -r requirements_notebooks.txt >>"$out_pip" + + end=$(date +%s) + printf "[*] Updating pip - done : took %s\n" "$(format_duration $((end - start)))" +} + +# +# 2) Update the notebooks +# +list_notebooks() { + find "$search_root" -type f -name "$name_pattern" +} + +update_notebooks() { + local links_only_flag="" + is_true "$UPDATER_LINKS_ONLY" true && links_only_flag="--links-only" + + # a failing notebook is recorded (`|| echo`), rather than failing xargs, so that the rest keep running + # shellcheck disable=SC2016 # the single-quoted `$N` are expanded by the inner `bash -c` + list_notebooks | xargs -P"$MAX_THREADS" -I{} \ + bash -c '"$0" "$1" "$2" || echo "$1" >>"$3"' \ + "$SCRIPT_DIR/update_single_notebook.sh" {} "$links_only_flag" "$FAILED_NOTEBOOKS_FILE" +} + +print_failed_notebooks() { + if [[ -s "$FAILED_NOTEBOOKS_FILE" ]]; then + echo + echo "[!] Failed to update $(wc -l <"$FAILED_NOTEBOOKS_FILE") notebook(s):" + sort "$FAILED_NOTEBOOKS_FILE" | sed 's/^/ /' + fi +} # # 3) Commit the changes + open PR # -echo -echo -echo "======= Creating PR =======" -# 1. Restore all .synthesis_options.json files to discard their changes -find . -name "*.synthesis_options.json" -exec git restore {} + -# 2. Add all .ipynb files recursively -find . -name "*.ipynb" -exec git add {} + -# 3. Commit the staged .ipynb files -git commit -m "Updating notebooks output" - -if [ $? -ne 0 ]; then - # running twice so that we'd include the pre-commit updates - find . -name "*.ipynb" -exec git add {} + - git commit -m "Updating notebooks output" -fi - - -gh pr create --fill --label "Updating output" -gh pr view --web +commit_notebooks() { + find . -name "*.ipynb" -exec git add {} + + git commit -m "Updating notebooks output" +} + +commit_and_open_pr() { + # discard the changes to the .synthesis_options.json files + find . -name "*.synthesis_options.json" -exec git restore {} + + + # if pre-commit edited the files, the first commit fails, and the second one includes those edits + commit_notebooks || commit_notebooks + + gh pr create --fill --label "Updating output" + gh pr view --web +} + +main() { + if has_flag --help "$@"; then + print_help + exit 0 + fi + + print_section "Init" + cd "$(git rev-parse --show-toplevel)" || exit 1 + # validate before making any changes + assert_inside_venv || exit 1 + resolve_notebooks_scope || exit 1 + create_branch + update_requirements + + print_section "Updating notebooks" + FAILED_NOTEBOOKS_FILE="$(mktemp)" + trap 'rm -f "$FAILED_NOTEBOOKS_FILE"' EXIT + update_notebooks + + print_section "Creating PR" + commit_and_open_pr + + print_failed_notebooks +} + +main "$@" diff --git a/.internal/update_outputs/update_single_notebook.sh b/.internal/update_outputs/update_single_notebook.sh index 50994d136..b414219d2 100755 --- a/.internal/update_outputs/update_single_notebook.sh +++ b/.internal/update_outputs/update_single_notebook.sh @@ -1,106 +1,88 @@ #!/bin/bash -# -# Parse arguments -# -is_links_only=false -for arg in "$@"; do - if [[ "$arg" == "--links-only" ]]; then - is_links_only=true - break +# usage: update_single_notebook.sh [--links-only] +# exits with non-zero if the notebook failed to execute, or if its links failed to update + +SCRIPT_DIR="$(realpath "$(dirname "$0")")" +PATCH_DIR="$SCRIPT_DIR/patch_notebook" +source "$SCRIPT_DIR/utils/common.sh" + +parse_args() { + notebook_path="$(realpath "$1")" + notebook_dir="$(dirname "$notebook_path")" + + is_links_only=false + has_flag --links-only "$@" && is_links_only=true +} + +assert_python3_kernel_exists() { + if ! jupyter kernelspec list | tail -n +2 | awk '{print $1}' | grep -xq "python3"; then + echo "Kernel python3 not found" >&2 + return 1 + fi +} + +# in links-only mode, we execute a temporary copy, and later take only the links from it +prepare_notebook() { + if [[ "$is_links_only" == true ]]; then + executed_notebook_path="$notebook_path.temp.ipynb" + cp "$notebook_path" "$executed_notebook_path" + else + executed_notebook_path="$notebook_path" + fi + + "$PATCH_DIR/injected_cells.py" add "$executed_notebook_path" +} + +# returns the exit code of `jupyter nbconvert` +execute_notebook() { + local out=/dev/stdout + is_true "$UPDATER_TRACK_TIME" true || out=/dev/null + + local start end status + start=$(date +%s) + printf "[*] Starting at %s : \"%s\"\n" "$(date)" "$notebook_path" >>"$out" + + ( + cd "$notebook_dir" && + jupyter nbconvert --to notebook --execute --inplace "$(basename "$executed_notebook_path")" --ExecutePreprocessor.kernel_name=python3 + ) + status=$? + + end=$(date +%s) + printf "[*] Ended at %s : \"%s\" : took %s\n" "$(date)" "$notebook_path" "$(format_duration $((end - start)))" >>"$out" + + return $status +} + +# usage: finalize_notebook +# returns non-zero if the execution failed, or if the links failed to update +finalize_notebook() { + local status=$1 + + "$PATCH_DIR/injected_cells.py" remove "$executed_notebook_path" + + if [[ "$is_links_only" == true ]]; then + if [[ $status -eq 0 ]]; then + "$PATCH_DIR/update_links.py" "$notebook_path" "$executed_notebook_path" || status=$? + else + echo "NOT updating links for ${notebook_path} since 'jupyter nbconvert' failed with exit code ${status}" + fi + + rm "$executed_notebook_path" fi -done - -# -# Parse file paths -# -notebook_relative_path="$1" -notebook_path="$(realpath "$notebook_relative_path")" - -notebook_dir=$(dirname "$notebook_path") -notebook_name=$(basename "$notebook_path") - - -current_folder="$(realpath "$(dirname "$0")")" - - -# -# Preparations before executing notebook -# - -# disable popping browser for `show(qprog)` -export BROWSER="$current_folder/fake_browser.sh" - -# trigger circuit creation in backend -export OPENVSCODE="some-dummy-value" - -# validations, before starting copy -if ! jupyter kernelspec list | tail -n +2 | awk '{print $1}' | grep -xq "python3"; then - echo "Kernel python3 not found" >&2 - exit 1 -fi - -# -# Editing notebook before execution -# - -# (maybe) make a copy -if [[ "$is_links_only" == true ]]; then - notebook_copy_path="$notebook_path.temp.ipynb" - notebook_copy_name=$(basename "$notebook_copy_path") - cp "$notebook_path" "$notebook_copy_path" -else - notebook_copy_path="$notebook_path" - notebook_copy_name="$notebook_name" -fi - -# hook edit notebook -"$current_folder/injected_cells.py" add "$notebook_copy_path" - -# -# Executing -# - -# (maybe) track timing -if [ "$UPDATER_TRACK_TIME" = "false" ] || [ "$UPDATER_TRACK_TIME" = "0" ]; then - out="/dev/null" -else - out="/dev/stdout" -fi - -# record start time -start=$(date +%s) -printf "[*] Starting at %s : \"%s\"\n" "$(date)" "$notebook_path" >"$out" - -# execute -pushd "$notebook_dir" > /dev/null -jupyter nbconvert --to notebook --execute --inplace "$notebook_copy_name" --ExecutePreprocessor.kernel_name=python3 -status=$? -popd > /dev/null - -# record end time -end=$(date +%s) -elapsed=$((end - start)) - -# format hh:mm:ss -printf -v duration "%02d:%02d:%02d" $((elapsed/3600)) $((elapsed%3600/60)) $((elapsed%60)) -printf "[*] Ended at %s : \"%s\" : took %s\n" "$(date)" "$notebook_path" "$duration" >"$out" - -# -# Post execution -# - -"$current_folder/injected_cells.py" remove "$notebook_copy_path" - -# -# if links-only - then merge the diff -# -if [[ "$is_links_only" == true ]]; then - if [ $status -eq 0 ]; then - "$current_folder/update_links.py" "$notebook_path" "$notebook_copy_path" - else - echo "NOT updating links for ${notebook_path} since 'jupyter nbconvert' failes with exit code ${status}" - fi - - rm "$notebook_copy_path" -fi + + return $status +} + +main() { + parse_args "$@" + assert_python3_kernel_exists || exit 1 + export_notebook_execution_env + + prepare_notebook + execute_notebook + finalize_notebook $? +} + +main "$@" diff --git a/.internal/update_outputs/utils/common.sh b/.internal/update_outputs/utils/common.sh new file mode 100644 index 000000000..94fac1982 --- /dev/null +++ b/.internal/update_outputs/utils/common.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# helpers shared by the update_outputs scripts (sourced, not executed) + +UTILS_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")" + +# usage: is_true +# "true" / "1" are true, "false" / "0" are false (case-insensitive), anything else (including empty) falls back to +is_true() { + case "${1,,}" in + true | 1) return 0 ;; + false | 0) return 1 ;; + *) [[ "$2" == true ]] ;; + esac +} + +# usage: has_flag +has_flag() { + local flag=$1 + shift + + local arg + for arg in "$@"; do + if [[ "$arg" == "$flag" ]]; then + return 0 + fi + done + return 1 +} + +# usage: format_duration -> hh:mm:ss +format_duration() { + local elapsed=$1 + printf "%02d:%02d:%02d" $((elapsed / 3600)) $((elapsed % 3600 / 60)) $((elapsed % 60)) +} + +# environment for executing notebooks unattended +export_notebook_execution_env() { + # disable popping browser for `show(qprog)` + export BROWSER="$UTILS_DIR/fake_browser.sh" + + # trigger circuit creation in backend + export OPENVSCODE="some-dummy-value" +} diff --git a/.internal/update_outputs/fake_browser.sh b/.internal/update_outputs/utils/fake_browser.sh similarity index 100% rename from .internal/update_outputs/fake_browser.sh rename to .internal/update_outputs/utils/fake_browser.sh From 46bee8e82fc5705e8147ab12f526cd8d284ccb6a Mon Sep 17 00:00:00 2001 From: Dor Harpaz Date: Thu, 19 Nov 2026 17:14:16 +0200 Subject: [PATCH 02/11] move update_outputs helpers into utils, and wrap commands with time_it / silence_if - split utils into common / git / python / jupyter helpers - add as_bool, silence_if and time_it, and normalize the env-vars once at the top - drop the `cd` before nbconvert, which already runs the kernel in the notebook's folder - stop if creating the branch fails, rather than updating on top of a wrong base Co-Authored-By: Claude Opus 5.5 --- .internal/update_outputs/update_notebooks.sh | 101 +++++------------- .../update_outputs/update_single_notebook.sh | 43 ++------ .internal/update_outputs/utils/common.sh | 62 +++++++++-- .internal/update_outputs/utils/git_utils.sh | 38 +++++++ .../update_outputs/utils/jupyter_utils.sh | 34 ++++++ .../update_outputs/utils/python_utils.sh | 21 ++++ 6 files changed, 181 insertions(+), 118 deletions(-) create mode 100644 .internal/update_outputs/utils/git_utils.sh create mode 100644 .internal/update_outputs/utils/jupyter_utils.sh create mode 100644 .internal/update_outputs/utils/python_utils.sh diff --git a/.internal/update_outputs/update_notebooks.sh b/.internal/update_outputs/update_notebooks.sh index e4ec87dd9..71ffaa6f5 100755 --- a/.internal/update_outputs/update_notebooks.sh +++ b/.internal/update_outputs/update_notebooks.sh @@ -5,10 +5,15 @@ SCRIPT_DIR="$(realpath "$(dirname "$0")")" source "$SCRIPT_DIR/utils/common.sh" +source "$SCRIPT_DIR/utils/git_utils.sh" +source "$SCRIPT_DIR/utils/python_utils.sh" MAX_THREADS=${UPDATER_MAX_THREADS:-3} # note: there's a max of 3 running jobs per user, so we limit the threads to 3 MODE=${UPDATER_MODE:-} FOLDER=${UPDATER_FOLDER:-} +IS_LINKS_ONLY=$(as_bool is_true "$UPDATER_LINKS_ONLY" true) +SHOULD_SILENCE_GIT=$(as_bool is_true "$UPDATER_SILENCE_GIT" false) +SHOULD_SILENCE_PIP=$(as_bool is_true "$UPDATER_SILENCE_PIP" false) print_help() { cat <&2 - return 1 - fi -} - # sets `search_root` and `name_pattern`, by which we `find` the notebooks to update resolve_notebooks_scope() { if [[ -n "$FOLDER" ]]; then @@ -77,42 +65,17 @@ resolve_notebooks_scope() { esac } -create_branch() { - local out_git=/dev/stdout - is_true "$UPDATER_SILENCE_GIT" false && out_git=/dev/null - - git checkout main - git pull >>"$out_git" - - echo - git checkout -b "updating_notebooks_$(date '+%Y.%m.%d_%H.%M')" - echo -} - -update_requirements() { - local out_pip=/dev/stdout - is_true "$UPDATER_SILENCE_PIP" false && out_pip=/dev/null - - echo "[*] Updating pip" - local start end - start=$(date +%s) - - python -m pip install -U -r requirements.txt -r requirements_notebooks.txt >>"$out_pip" - - end=$(date +%s) - printf "[*] Updating pip - done : took %s\n" "$(format_duration $((end - start)))" -} - -# -# 2) Update the notebooks -# list_notebooks() { find "$search_root" -type f -name "$name_pattern" } update_notebooks() { - local links_only_flag="" - is_true "$UPDATER_LINKS_ONLY" true && links_only_flag="--links-only" + local links_only_flag + if [[ "$IS_LINKS_ONLY" == true ]]; then + links_only_flag="--links-only" + else + links_only_flag="" + fi # a failing notebook is recorded (`|| echo`), rather than failing xargs, so that the rest keep running # shellcheck disable=SC2016 # the single-quoted `$N` are expanded by the inner `bash -c` @@ -129,46 +92,40 @@ print_failed_notebooks() { fi } -# -# 3) Commit the changes + open PR -# -commit_notebooks() { - find . -name "*.ipynb" -exec git add {} + - git commit -m "Updating notebooks output" -} - -commit_and_open_pr() { - # discard the changes to the .synthesis_options.json files - find . -name "*.synthesis_options.json" -exec git restore {} + - - # if pre-commit edited the files, the first commit fails, and the second one includes those edits - commit_notebooks || commit_notebooks - - gh pr create --fill --label "Updating output" - gh pr view --web -} - main() { if has_flag --help "$@"; then print_help exit 0 fi + # + # 1) Initialization - validations, `git pull` + create-new-branch, update pip + # print_section "Init" cd "$(git rev-parse --show-toplevel)" || exit 1 # validate before making any changes assert_inside_venv || exit 1 resolve_notebooks_scope || exit 1 - create_branch - update_requirements + silence_if "$SHOULD_SILENCE_GIT" create_branch_from_main "updating_notebooks_$(date '+%Y.%m.%d_%H.%M')" || exit 1 + time_it "Updating pip" silence_if "$SHOULD_SILENCE_PIP" pip_install_requirements requirements.txt requirements_notebooks.txt + + # + # 2) Update the notebooks + # print_section "Updating notebooks" FAILED_NOTEBOOKS_FILE="$(mktemp)" trap 'rm -f "$FAILED_NOTEBOOKS_FILE"' EXIT update_notebooks + # + # 3) Commit the changes + open PR + # print_section "Creating PR" - commit_and_open_pr + # discard the changes to the .synthesis_options.json files + git_restore_by_name "*.synthesis_options.json" + commit_by_name "*.ipynb" "Updating notebooks output" + open_pr "Updating output" print_failed_notebooks } diff --git a/.internal/update_outputs/update_single_notebook.sh b/.internal/update_outputs/update_single_notebook.sh index b414219d2..49a72d6ea 100755 --- a/.internal/update_outputs/update_single_notebook.sh +++ b/.internal/update_outputs/update_single_notebook.sh @@ -6,27 +6,17 @@ SCRIPT_DIR="$(realpath "$(dirname "$0")")" PATCH_DIR="$SCRIPT_DIR/patch_notebook" source "$SCRIPT_DIR/utils/common.sh" +source "$SCRIPT_DIR/utils/jupyter_utils.sh" parse_args() { notebook_path="$(realpath "$1")" - notebook_dir="$(dirname "$notebook_path")" - - is_links_only=false - has_flag --links-only "$@" && is_links_only=true -} - -assert_python3_kernel_exists() { - if ! jupyter kernelspec list | tail -n +2 | awk '{print $1}' | grep -xq "python3"; then - echo "Kernel python3 not found" >&2 - return 1 - fi + is_links_only=$(as_bool has_flag --links-only "$@") } # in links-only mode, we execute a temporary copy, and later take only the links from it prepare_notebook() { if [[ "$is_links_only" == true ]]; then - executed_notebook_path="$notebook_path.temp.ipynb" - cp "$notebook_path" "$executed_notebook_path" + executed_notebook_path="$(copy_notebook "$notebook_path")" || return 1 else executed_notebook_path="$notebook_path" fi @@ -34,28 +24,7 @@ prepare_notebook() { "$PATCH_DIR/injected_cells.py" add "$executed_notebook_path" } -# returns the exit code of `jupyter nbconvert` -execute_notebook() { - local out=/dev/stdout - is_true "$UPDATER_TRACK_TIME" true || out=/dev/null - - local start end status - start=$(date +%s) - printf "[*] Starting at %s : \"%s\"\n" "$(date)" "$notebook_path" >>"$out" - - ( - cd "$notebook_dir" && - jupyter nbconvert --to notebook --execute --inplace "$(basename "$executed_notebook_path")" --ExecutePreprocessor.kernel_name=python3 - ) - status=$? - - end=$(date +%s) - printf "[*] Ended at %s : \"%s\" : took %s\n" "$(date)" "$notebook_path" "$(format_duration $((end - start)))" >>"$out" - - return $status -} - -# usage: finalize_notebook +# usage: finalize_notebook # returns non-zero if the execution failed, or if the links failed to update finalize_notebook() { local status=$1 @@ -80,8 +49,8 @@ main() { assert_python3_kernel_exists || exit 1 export_notebook_execution_env - prepare_notebook - execute_notebook + prepare_notebook || exit 1 + time_it "\"$notebook_path\"" execute_notebook_inplace "$executed_notebook_path" finalize_notebook $? } diff --git a/.internal/update_outputs/utils/common.sh b/.internal/update_outputs/utils/common.sh index 94fac1982..174ce7097 100644 --- a/.internal/update_outputs/utils/common.sh +++ b/.internal/update_outputs/utils/common.sh @@ -1,8 +1,6 @@ #!/bin/bash -# helpers shared by the update_outputs scripts (sourced, not executed) - -UTILS_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")" +# general helpers for the update_outputs scripts (sourced, not executed) # usage: is_true # "true" / "1" are true, "false" / "0" are false (case-insensitive), anything else (including empty) falls back to @@ -28,17 +26,63 @@ has_flag() { return 1 } +# usage: variable=$(as_bool ) +# prints "true" if the command succeeds, and "false" otherwise +# e.g. `is_links_only=$(as_bool has_flag --links-only "$@")` +as_bool() { + if "$@"; then + echo true + else + echo false + fi +} + +# usage: silence_if +# runs the command, discarding its stdout if true +silence_if() { + local should_silence=$1 + shift + + if [[ "$should_silence" == true ]]; then + "$@" >/dev/null + else + "$@" + fi +} + # usage: format_duration -> hh:mm:ss format_duration() { local elapsed=$1 printf "%02d:%02d:%02d" $((elapsed / 3600)) $((elapsed % 3600 / 60)) $((elapsed % 60)) } -# environment for executing notebooks unattended -export_notebook_execution_env() { - # disable popping browser for `show(qprog)` - export BROWSER="$UTILS_DIR/fake_browser.sh" +# usage: time_it