|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +################################################################################ |
| 17 | +# Checks the format of Jupyter notebook (.ipynb) files using the |
| 18 | +# TensorFlow Docs notebook format checker. |
| 19 | +# |
| 20 | +# Usage: |
| 21 | +# check/nbformat [--help] [--fix] |
| 22 | +# |
| 23 | +# By default, this script checks all .ipynb files tracked by git and exits |
| 24 | +# with a non-zero code if any notebook does not conform to the expected format. |
| 25 | +# |
| 26 | +# With '--fix', the script reformats the notebooks in place instead of just |
| 27 | +# reporting errors. |
| 28 | +################################################################################ |
| 29 | + |
| 30 | +set -euo pipefail |
| 31 | + |
| 32 | +declare -r usage="\ |
| 33 | +Usage: ${0##*/} [--help] [--fix] |
| 34 | +
|
| 35 | +Checks the format of Jupyter notebook (.ipynb) files in this repository using |
| 36 | +the TensorFlow Docs notebook format checker (tensorflow_docs.tools.nbformat). |
| 37 | +
|
| 38 | +With no arguments, notebooks are checked but not modified. Exits with a |
| 39 | +non-zero status code if any notebook fails the format check. |
| 40 | +
|
| 41 | +With --fix, the notebooks are reformatted in place and the script exits with |
| 42 | +status 0 if all notebooks were successfully processed. |
| 43 | +" |
| 44 | + |
| 45 | +# Get the working directory to the repo root. |
| 46 | +thisdir=$(dirname "${BASH_SOURCE[0]:?}") |
| 47 | +repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel) |
| 48 | +cd "${repo_dir}" |
| 49 | + |
| 50 | +# Parse arguments. |
| 51 | +opt_fix=0 |
| 52 | +for arg in "$@"; do |
| 53 | + case "${arg}" in |
| 54 | + -h | --help) |
| 55 | + echo "${usage}" |
| 56 | + exit 0 |
| 57 | + ;; |
| 58 | + --fix) |
| 59 | + opt_fix=1 |
| 60 | + ;; |
| 61 | + *) |
| 62 | + echo "Unknown argument: '${arg}'" >&2 |
| 63 | + echo "See '${0} --help' for usage." >&2 |
| 64 | + exit 1 |
| 65 | + ;; |
| 66 | + esac |
| 67 | +done |
| 68 | + |
| 69 | +# Find all tracked .ipynb files in the repository. |
| 70 | +IFS=$'\n' read -r -d '' -a notebooks < <( |
| 71 | + git ls-files '*.ipynb' |
| 72 | + printf "\0" |
| 73 | +) |
| 74 | + |
| 75 | +if [[ "${#notebooks[@]}" -eq 0 ]]; then |
| 76 | + echo "No .ipynb notebook files found." |
| 77 | + exit 0 |
| 78 | +fi |
| 79 | + |
| 80 | +echo "Found ${#notebooks[@]} notebook(s)." |
| 81 | + |
| 82 | +# Build the tensorflow_docs.tools.nbformat argument list. |
| 83 | +declare -a tf_args=() |
| 84 | +if (( opt_fix )); then |
| 85 | + echo "Running notebook format fixer..." |
| 86 | + tf_args=( "--fix" ) |
| 87 | +else |
| 88 | + echo "Running notebook format checker..." |
| 89 | +fi |
| 90 | + |
| 91 | +declare -a errors=() |
| 92 | +for notebook in "${notebooks[@]}"; do |
| 93 | + echo " Checking: ${notebook}" |
| 94 | + if ! python -m tensorflow_docs.tools.nbformat "${tf_args[@]}" "${notebook}"; then |
| 95 | + errors+=( "${notebook}" ) |
| 96 | + fi |
| 97 | +done |
| 98 | + |
| 99 | +echo |
| 100 | + |
| 101 | +if [[ "${#errors[@]}" -eq 0 ]]; then |
| 102 | + echo "All notebooks passed the format check." |
| 103 | + exit 0 |
| 104 | +else |
| 105 | + echo "The following notebooks failed the format check:" >&2 |
| 106 | + printf " %s\n" "${errors[@]}" >&2 |
| 107 | + if (( ! opt_fix )); then |
| 108 | + echo "" >&2 |
| 109 | + echo "Run 'check/nbformat --fix' to fix the notebooks automatically." >&2 |
| 110 | + fi |
| 111 | + exit 1 |
| 112 | +fi |
0 commit comments